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
Thanks in advance.
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.
– Giovani Rodrigo
@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 eventValidating
, and tbm still exists a component calledErrorProvider
. Should either interest, just talk.– Paulo Ricardo
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 theTextBox
will give this annoying appearance problem. I wanted to change only the border color,backgroundColor
would be the background color ofTextBox
I think.– Giovani Rodrigo
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
.– Paulo Ricardo
So, it’s the first
TextBox
that I want but the problem is that as it is followed by a few moreTextBox
, if you compare you notice the difference. I will add more images.– Giovani Rodrigo
@pauloricardo, liked the answer, did not know this error of the icon, tomorrow I will try to implement in my projects.
– Alicia Tairini