好吧,我想要一个在数据有效时可以单击的按钮,在数据无效时不能单击的按钮。该代码是正确的,可以运行而没有任何问题,但是我不知道该在何时触发该代码?过去,我将此代码放在“受保护的重写无效WndProc”中,但是我在视觉上遇到了一些小故障,无法保存在其中。
if (FirstName.Text == null || LastName.Text == null || Email.Text == null || Password.Text == null || Password2.Text == null || Password.Text != Password2.Text || LastName.Text == "Last Name" && FirstName.Text == "First Name" && Email.Text == "Email")
{
RegisterDone.Text = " You missed something";
RegisterDone.Normalcolor = Color.FromArgb(64, 64, 64);
RegisterDone.OnHovercolor = Color.FromArgb(64, 64, 64);
RegisterDone.Activecolor = Color.FromArgb(64, 64, 64);
RegisterDone.Textcolor = Color.FromArgb(197, 161, 89);
RegisterDone.OnHoverTextColor = Color.FromArgb(197, 161, 89);
RegisterDone.Cursor = Cursors.No;
RegisterDone.Iconimage = Image.FromFile(@"C:\Users\gamin\Desktop\Icons\Forbidden.png");
}
if (FirstName.Text != null && LastName.Text != null && Email.Text != null && Password.Text == Password2.Text && LastName.Text != "Last Name" && FirstName.Text != "First Name" && Email.Text != "Email")
{
RegisterDone.Text = " Register right now";
RegisterDone.Normalcolor = Color.FromArgb(4, 41, 50);
RegisterDone.OnHovercolor = Color.FromArgb(4, 41, 50);
RegisterDone.Activecolor = Color.FromArgb(4, 41, 50);
RegisterDone.Textcolor = Color.FromArgb(197, 161, 89);
RegisterDone.OnHoverTextColor = Color.FromArgb(197, 161, 89);
RegisterDone.Cursor = Cursors.Hand;
RegisterDone.Iconimage = Image.FromFile(@"C:\Users\gamin\Desktop\Icons\Valid.png");
}
答案 0 :(得分:2)
您应该将此代码放在FormLoad上:
FirstName.TextChanged += TextBox_TextChanged;
LastName.TextChanged += TextBox_TextChanged;
Email.TextChanged += TextBox_TextChanged;
Password.TextChanged += TextBox_TextChanged;
Password2.TextChanged += TextBox_TextChanged;
并将该代码在表单中的任何位置作为单独的函数进行编码:
private void TextBox_TextChanged(object sender, EventArgs e)
{
if (FirstName.Text == null || LastName.Text == null || Email.Text == null || Password.Text == null || Password2.Text == null || Password.Text != Password2.Text || LastName.Text == "Last Name" && FirstName.Text == "First Name" && Email.Text == "Email")
{
RegisterDone.Text = " You missed something";
RegisterDone.Normalcolor = Color.FromArgb(64, 64, 64);
RegisterDone.OnHovercolor = Color.FromArgb(64, 64, 64);
RegisterDone.Activecolor = Color.FromArgb(64, 64, 64);
RegisterDone.Textcolor = Color.FromArgb(197, 161, 89);
RegisterDone.OnHoverTextColor = Color.FromArgb(197, 161, 89);
RegisterDone.Cursor = Cursors.No;
RegisterDone.Iconimage = Image.FromFile(@"C:\Users\gamin\Desktop\Icons\Forbidden.png");
}
if (FirstName.Text != null && LastName.Text != null && Email.Text != null && Password.Text == Password2.Text && LastName.Text != "Last Name" && FirstName.Text != "First Name" && Email.Text != "Email")
{
RegisterDone.Text = " Register right now";
RegisterDone.Normalcolor = Color.FromArgb(4, 41, 50);
RegisterDone.OnHovercolor = Color.FromArgb(4, 41, 50);
RegisterDone.Activecolor = Color.FromArgb(4, 41, 50);
RegisterDone.Textcolor = Color.FromArgb(197, 161, 89);
RegisterDone.OnHoverTextColor = Color.FromArgb(197, 161, 89);
RegisterDone.Cursor = Cursors.Hand;
RegisterDone.Iconimage = Image.FromFile(@"C:\Users\gamin\Desktop\Icons\Valid.png");
}
}
另一件事是您说您的代码有效,但是我不确定任何textbox.Text可以为null。 也许您需要将其更改为
textbox.Text == "";
或者如果您仍然想防止使用null:
string.IsNullOrWhiteSpace(textbox.Text);
答案 1 :(得分:0)
//向要检查的所有文本框注册事件
LastName.TextChanged += new EventHandler(textbox_TextChanged);
FirstName.TextChanged += new EventHandler(textbox_TextChanged);
private void textbox_TextChanged(object sender, EventArgs e)
{
//call your validation code here.
}
如果要“丢失焦点”,“ KeyUp”等,可以使用其他事件。