C
C#2y ago
cgxlm

❔ Error Provider Windows Forms

im not sure how to get the error providers working
6 Replies
cgxlm
cgxlmOP2y ago
if (!passwordReg.IsMatch(password))
{
errorPassword.SetError(txtPassword, "");
errorPassword.Icon = Properties.Resources.CorrectIcon;
}
else
{
errorPassword.SetError(txtPassword, "Password is incorrect");
errorPassword.Icon = Properties.Resources.ErrorIcon;
}
if (!passwordReg.IsMatch(password))
{
errorPassword.SetError(txtPassword, "");
errorPassword.Icon = Properties.Resources.CorrectIcon;
}
else
{
errorPassword.SetError(txtPassword, "Password is incorrect");
errorPassword.Icon = Properties.Resources.ErrorIcon;
}
thats the code i have for it
TheBoxyBear
TheBoxyBear2y ago
What's the problem?
Noah
Noah2y ago
@cgxlm Your code looks mostly correct, but you need to set the error message to an empty string when the password is correct.
errorPassword.SetError(txtPassword, "Password is incorrect");
errorPassword.SetError(txtPassword, "Password is incorrect");
Wait U allready do Could you give more details about the issue you're experiencing?
cgxlm
cgxlmOP2y ago
when running the code, no matter if i put the details wrong or right just absolutely nothing happens
Pobiega
Pobiega2y ago
In what event are you running this code? Did you associate the provider with the control?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeErrorProviders();
}

private void InitializeErrorProviders()
{
numberErrorProvider.SetIconAlignment(numericUpDown1, ErrorIconAlignment.MiddleRight);
numberErrorProvider.SetIconPadding(numericUpDown1, 2);
}

private void numericUpDown1_Validated(object sender, EventArgs e)
{
if (numericUpDown1.Value < 10)
{
numberErrorProvider.SetError(numericUpDown1, "Too low.");
}
else if (numericUpDown1.Value >= 20)
{
numberErrorProvider.SetError(numericUpDown1, "Too high.");
}
else
{
numberErrorProvider.SetError(numericUpDown1, string.Empty);
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeErrorProviders();
}

private void InitializeErrorProviders()
{
numberErrorProvider.SetIconAlignment(numericUpDown1, ErrorIconAlignment.MiddleRight);
numberErrorProvider.SetIconPadding(numericUpDown1, 2);
}

private void numericUpDown1_Validated(object sender, EventArgs e)
{
if (numericUpDown1.Value < 10)
{
numberErrorProvider.SetError(numericUpDown1, "Too low.");
}
else if (numericUpDown1.Value >= 20)
{
numberErrorProvider.SetError(numericUpDown1, "Too high.");
}
else
{
numberErrorProvider.SetError(numericUpDown1, string.Empty);
}
}
}
I just made this as a sample and it works fine, winforms .NET 7 I first tried putting the SetIcon... code in InitializeComponent but it got overwritten, so I don't recommend that 😛
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server