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.