Хочу реализовать проверку корректности всех входных данных в поля регистрации и после нажатия на кнопку Registration, чтобы в методе проверялись все входные данные на корректность и далее шёл переход на форму Login.
Сами проверки на корректность у меня есть, но хочу реализовать, чтобы при нажатии на кнопку вносились данные если все поля корректно заполнены и происходил переход на другую форму.
public partial class Registration : Form
{
public Registration()
{
InitializeComponent();
}
#region registrButton
private void registrationButton_Click(object sender, EventArgs e)
{
if (check == true)
{
using (ApplicationContext db = new ApplicationContext())
{
User user = new User();
user.Name = NameBox.Text;
user.Surname = surnameBox.Text;
user.Street = StreetBox.Text;
user.Phone = phoneMaskedBox.Text;
user.Town = TownBox.Text;
user.Email = EmailBox.Text;
user.Password = PasswordBox.Text;
db.Users.Add(user);
db.SaveChanges();
}
Login login = new Login();
login.Show();
this.Close();
}
else
{
MessageBox.Show("Check all dates");
}
}
#endregion
// bool value
#region checkvalues
bool check = false;
#endregion
// Boxes
#region nameBox
private void NameBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == (char)Keys.Back)
{
check = true;
}
else
{
check = false;
e.Handled = true;
hint.ToolTipTitle = "Input rejected - the layout is not English";
hint.Show("You can't enter a name using a different layout" +
"Press the key to hide this hint", NameBox, 0, -20, 5000);
}
}
private void NameBox_KeyDown(object sender, KeyEventArgs e)
{
hint.Hide(NameBox);
}
private void NameBox_TextChanged(object sender, EventArgs e)
{
if (((TextBox)sender).Text.Length == 1)
((TextBox)sender).Text = ((TextBox)sender).Text.ToUpper();
((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
}
#endregion
#region surnameBox
private void surnameBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == (char)Keys.Back)
{
check = true;
}
else
{
check = false;
e.Handled = true;
hint.ToolTipTitle = "Input rejected - the layout is not English";
hint.Show("You can't enter a name using a different layout" +
"Press the key to hide this hint", surnameBox, 0, -20, 5000);
}
}
private void surnameBox_KeyDown(object sender, KeyEventArgs e)
{
hint.Hide(surnameBox);
}
private void surnameBox_TextChanged(object sender, EventArgs e)
{
if (((TextBox)sender).Text.Length == 1)
((TextBox)sender).Text = ((TextBox)sender).Text.ToUpper();
((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
}
#endregion
#region emailBox
private void EmailBox_Validating(object sender, CancelEventArgs e)
{
string pattern = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
if (Regex.IsMatch(EmailBox.Text, pattern, RegexOptions.IgnoreCase))
{
check = true;
}
else
{
check = false;
hint.ToolTipTitle = "Input rejected - Attempt to enter forbidden characters";
hint.Show( "\nExample email adress: [email protected]" +
"\nPress the key to hide this hint", EmailBox, 0, 20, 5000);
}
}
private void EmailBox_KeyDown(object sender, KeyEventArgs e)
{
hint.Hide(EmailBox);
}
#endregion
#region phoneBox
private void phoneMaskedBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
if (phoneMaskedBox.MaskFull)
{
check = false;
hint.ToolTipTitle = "Input rejected - Too much data";
hint.Show("You can't enter any more data" +
"Press the key to hide this hint", phoneMaskedBox, 0, -20, 5000);
}
else if (e.Position == phoneMaskedBox.Mask.Length)
{
check = false;
hint.ToolTipTitle = "Input Rejected - End of Field";
hint.Show("You can't add extra characters to the end of this date field." +
"Press the key to hide this hint", phoneMaskedBox, 0, -20, 5000);
}
else
{
check = false;
hint.ToolTipTitle = "Input Rejected";
hint.Show("You can only add numeric characters (0-9) into this date field." +
"Press the key to hide this hint", phoneMaskedBox, 0, -20, 5000);
}
}
private void phoneMaskedBox_Validating(object sender, CancelEventArgs e)
{
if (phoneMaskedBox.TextLength == 12)
check = true;
else
check = false;
}
private void phoneMaskedBox_KeyDown(object sender, KeyEventArgs e)
{
hint.Hide(phoneMaskedBox);
}
#endregion
#region passwordBox
private void PasswordBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)Keys.Back)
{
check = true;
}
else
{
check = false;
e.Handled = true;
hint.ToolTipTitle = "Input rejected - the layout is not English or there was an attempt to enter forbidden characters";
hint.Show("You can't enter a name using a different layout" +
"\nCharacters are not allowed: !#$%&'*+-/=?^_`{|}~" +
"Press the key to hide this hint", PasswordBox, 0, -20, 5000);
}
}
private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
{
hint.Hide(PasswordBox);
}
private void PasswordBox_Validating(object sender, CancelEventArgs e)
{
check = false;
if (PasswordBox.TextLength < 6 || PasswordBox.TextLength > 24)
{
inputincorrect.SetError(PasswordBox, "The password must contain between 6 and 24 characters");
}
}
#endregion
#region townBox
private void TownBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == (char)Keys.Back)
{
check = true;
}
else
{
check = false;
e.Handled = true;
hint.ToolTipTitle = "Input rejected - the layout is not English";
hint.Show("You can't enter a name using a different layout" +
"Press the key to hide this hint", TownBox, 0, -20, 5000);
}
}
private void TownBox_KeyDown(object sender, KeyEventArgs e)
{
hint.Hide(TownBox);
}
private void TownBox_TextChanged(object sender, EventArgs e)
{
if (((TextBox)sender).Text.Length == 1)
((TextBox)sender).Text = ((TextBox)sender).Text.ToUpper();
((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
}
#endregion
#region streetBox
private void StreetBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == (char)Keys.Back)
{
check = true;
}
else
{
check = false;
e.Handled = true;
hint.ToolTipTitle = "Input rejected - the layout is not English";
hint.Show("You can't enter a name using a different layout" +
"Press the key to hide this hint", TownBox, 0, -20, 5000);
}
}
private void StreetBox_KeyDown(object sender, KeyEventArgs e)
{
hint.Hide(TownBox);
}
private void StreetBox_TextChanged(object sender, EventArgs e)
{
if (((TextBox)sender).Text.Length == 1)
((TextBox)sender).Text = ((TextBox)sender).Text.ToUpper();
((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
}
#endregion
#region confirmBox
private void confirmpassBox_TextChanged(object sender, EventArgs e)
{
if (confirmpassBox.Text != PasswordBox.Text)
{
check = false;
inputincorrect.SetError(confirmpassBox, "Password don't match");
}
else
{
check = true;
inputincorrect.Clear();
}
}
#endregion
}
Пытался закостылить через булеву переменную, но костыль не прошёл
Проект делаю на EF Core с использованием MS Sql
NameBox_TextChanged()
,surnameBox_TextChanged()
,TownBox_TextChanged()
,StreetBox_TextChanged
ваще одинаковые в принципе, можно же использовать всего один общий метод вместо четырех, тем более вы работаете не по имени текстбокса а черезsender
.при нажатии на кнопку вносились данные если все поля корректно заполнены
, а сейчас не заполняются поля что-ли?