Call Method C#

Asked

Viewed 119 times

0

Hello, I’m with a project in progress where I have an employee registration screen. I need to leave the edge of the TextBox red if there is something wrong with filling it out.
I tried in many ways and none gave the result I hoped.
I found this code on the Internet, but I don’t know how to call the method on 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 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 the condition are in the same class, excuse the lack of knowledge, but I am new in this language.

@EDIT With the code below I almost got the result I wanted, but it was not perfect as 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 above code
Image 1
inserir a descrição da imagem aqui

Image 2
inserir a descrição da imagem aqui

Thanks in advance.

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 form method containing the textBox1.

  1. I removed the Borders;
  2. I drew a rectangle exactly in place of the edges;

Image with other alternatives as reported in the comment: inserir a descrição da imagem aqui

  • This code I already tried to use, unfortunately the border gets shifted, I will edit the question and put a picture of the difference between the original and the border with this code. In addition to displacing apparently increases the field size.

  • @Giovanirodrigo, gluing two TextBoxs together, I could tell the difference. I’m going to make some changes and do some tests to see if I can get a satisfactory result. Before hand there are other possibilities,I use for example .backgroundColor at the event Validating, and tbm still exists a component called ErrorProvider. Should either interest, just talk.

  • I tested ErrorProvider and what I understand is just error icon, not interested me. I think regardless of the event I use, to change the color of the edge of the TextBox will give this annoying appearance problem. I wanted to change only the border color, backgroundColor would be the background color of TextBox I think.

  • I put image suggesting alternatives. is a very simple example, but you can use several ways to validate a field. examples: no Leave, Validating, Validated.

  • So, it’s the first TextBox that I want but the problem is that as it is followed by a few more TextBox, if you compare you notice the difference. I will add more images.

  • @pauloricardo, liked the answer, did not know this error of the icon, tomorrow I will try to implement in my projects.

Show 1 more comment

1

You have to use Paint’s Event Form. It must be something like that:

    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);
        }
    }
  • How does this code check if the field is empty? Because the way it is there, nothing happens.

  • could invalidate the form, to force the Paint. I don’t know which is better, do this or create a new Graphics. The solution demonstrated by Paulo Ricardo seems more appropriate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.