Call Method C #

0

Hello, I have a project in progress where I have a staff registration screen. I need to leave the border of TextBox red if there is anything wrong in filling it up. I tried in many ways and none gave the result I expected. I found this code on the internet, but, I do not know how to call the method in the condition.

Method

private void DrawRectangle(Graphics g, Rectangle rect, float penWidth)
{
    using (Pen pen = new Pen(SystemColors.ControlDark, penWidth))
    {
        float shrinkAmount = pen.Width / 2;
        g.DrawRectangle(
            pen,
            rect.X + shrinkAmount,
            rect.Y + shrinkAmount,
            rect.Width - penWidth,
            rect.Height - penWidth);
    }
}

Condition if the field is empty.

if (txtNomeFuncionario.Text == string.Empty)
{
    MessageBox.Show("O campo Nome parece estar vazio.");
    txtNomeFuncionario.BorderStyle = BorderStyle.None;
    // Preciso chamar o método DrawRetangle aqui
}

The method and condition are in the same class, sorry for the lack of knowledge, but I'm new to this language.

@EDIT With the code below I almost got the result I wanted, but it did not look perfect like the original border.

txtNomeFuncionario.BorderStyle = BorderStyle.None;
this.CreateGraphics().DrawRectangle(new Pen(Color.Red, 2f),
    txtNomeFuncionario.Location.X,
    txtNomeFuncionario.Location.Y,
    txtNomeFuncionario.Width,
    txtNomeFuncionario.Height);

Result of the code above Image 1

Image2

Thank you in advance.

    
asked by anonymous 08.04.2018 / 23:01

2 answers

2

Try this:

private void button1_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(textBox1.Text))
    {
        textBox1.BorderStyle = BorderStyle.None;
        this.CreateGraphics().DrawRectangle(new Pen(Color.Red, 2.0f), textBox1.Location.X, textBox1.Location.Y, textBox1.Width, textBox1.Height);
    }            
}

Note that CreateGraphics() is a method of the form where textBox1 is located.

  • I removed the borders;
  • I drew a rectangle exactly in place of the borders;
  • Image with other alternatives as reported in the comment:

        
    09.04.2018 / 01:58
    1

    You have to use the Paint Event of Form . It should look something like this:

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (String.IsNullOrEmpty(txtNomeFuncionario.Text))
                DrawRectangle(e.Graphics, txtNomeFuncionario.DisplayRectangle, 1);
        }
    
        private void DrawRectangle(Graphics g, Rectangle rect, float penWidth)
        {
            using (Pen pen = new Pen(SystemColors.ControlDark, penWidth))
            {
                float shrinkAmount = pen.Width / 2;
                g.DrawRectangle(
                    pen,
                    rect.X + shrinkAmount,
                    rect.Y + shrinkAmount,
                    rect.Width - penWidth,
                    rect.Height - penWidth);
            }
        }
    
        
    09.04.2018 / 01:11