When I created
Form2.cs, I added two radio buttons
private void UserExistRadioButton_CheckedChanged(object sender, EventArgs e)
and
private void UserNotExistRadioButton_CheckedChanged(object sender, EventArgs e)
, my button
private void NextForm3_Click(object sender, EventArgs e)
doesn't open new Form. This is my code in
Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
public static string AllForm2 = "";
public Form2()
{
InitializeComponent();
}
public static string DataFromUsername { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
}
private void Username_TextChanged(object sender, EventArgs e)
{
string inputText = Username.Text;
string[] validUsernames = { "dhcp", "Dorota Gruzdas",
"Edyta Wandzlewicz", "Guest", "Karolina Domżał",
"Łukasz Kiczko", "Maciej Rzeszutek",
"Magdalena Skrzypińska", "Marek Cybowski",
"Marek Skrzypiński", "Mariusz Golubiński" };
if (validUsernames.Contains(inputText))
{
AllText2.Text = "użytkownik domenowy" + $"{inputText}";
UpdateAllText2();
}
else
{
AllText2.Text = "Inny użytkownik " + $"{inputText}";
UpdateAllText2();
}
}
private void AllText2_TextChanged(object sender, EventArgs e)
{
}
private void Back_Click(object sender, EventArgs e)
{
Form1 back1 = new Form1();
back1.Show();
this.Close();
}
private void UserExistRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (UserExistRadioButton.Checked)
{
Username.Enabled = false;
ExistUserList.SelectedIndexChanged -= ExistUserList_SelectedIndexChanged;
ExistUserList.Enabled = true;
}
}
private void UserNotExistRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (UserNotExistRadioButton.Checked)
{
Username.Enabled = true;
ExistUserList.SelectedIndexChanged += ExistUserList_SelectedIndexChanged;
ExistUserList.Enabled = false;
}
}
private void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedText = ExistUserList.SelectedItem.ToString();
AllText2.Text = selectedText;
}
private void UpdateAllText2()
{
string username = Username.Text;
string allText = $"Nazwa użytkownika: {username}";
AllText2.Text = allText;
}
private void NextForm3_Click(object sender, EventArgs e)
{
string password = FirstPassword.Text;
string password1 = FirstPassword.Text;
bool containsDigit = false;
foreach (char c in password1)
{
if (char.IsDigit(c))
{
containsDigit = true;
break;
}
}
string password2 = FirstPassword.Text;
bool containsSpecialCharacter = false;
foreach (char c in password2)
{
if (!char.IsLetterOrDigit(c))
{
containsSpecialCharacter = true;
break;
}
}
string password3 = FirstPassword.Text;
bool hasUpperCaseLetter = false;
foreach (char character in password3)
{
if (char.IsUpper(character))
{
hasUpperCaseLetter = true;
break;
}
}
string password4 = FirstPassword.Text;
bool hasLowerCaseLetter = false;
foreach (char character in password4)
{
if (char.IsLower(character))
{
hasUpperCaseLetter = true;
break;
}
}
if (UserExistRadioButton.Checked)
{
if (ExistUserList.SelectedItem == null)
{
MessageBox.Show("Wybierz istniejącą nazwę użytkownika",
"Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
else if (UserNotExistRadioButton.Checked)
{
if (string.IsNullOrEmpty(Username.Text))
{
MessageBox.Show("Musisz wpisać nazwę użytkownika",
"Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (Username.Text.Length < 3)
{
MessageBox.Show("Musisz wpisać 3 lub więcej liter w nazwie
użytkownika", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else if (ExistUserList.SelectedItem.ToString() == "Test")
{
if (FirstPassword.Text != "zaq1@WSX")
{
MessageBox.Show("Nieprawidłowe hasło");
}
}
else if (string.IsNullOrEmpty(FirstPassword.Text) &&
string.IsNullOrEmpty(SecondPassword.Text) &&
FirstPassword.Text != SecondPassword.Text && password.Length < 8)
{
MessageBox.Show("Musisz wpisać hasło." +
"Musisz powtórzyć hasło." +
"Hasło musi być takie same. " +
"Musisz wpisać 8 znaków w haśle", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (!containsDigit && !containsSpecialCharacter && !hasUpperCaseLetter && !hasLowerCaseLetter)
{
MessageBox.Show("Musisz wpisać chociażby jedną liczbę w haśle." +
"Musisz wpisać chociażby jeden znak specjalny w haśle" +
"Musisz wpisać chociażby jedną dużą literę w haśle" +
"Musisz wpisać chociażby jedną mniejszą literę w haśle", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if(UserExistRadioButton.Checked)
{
AllForm2 = AllText2.Text;
Form3 next3 = new Form3();
next3.Show();
this.Close();
}
else if (UserNotExistRadioButton.Checked)
{
AllForm2 = AllText2.Text;
Form3 next3 = new Form3();
next3.Show();
this.Close();
}
else
{
AllForm2 = AllText2.Text;
Form3 next3 = new Form3();
next3.Show();
this.Close();
}
}
}
}
}
This is my code in
Form3.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
AllText3.Text = Form1.AllForm1 + "\r\n" + Form2.AllForm2;
}
private void Back_Click(object sender, EventArgs e)
{
Form2 back2 = new Form2();
back2.Show();
this.Close();
}
private void AllText3_TextChanged(object sender, EventArgs e)
{
}
}
}
What I have tried:
I have tried to put one "
if
", one "
else if
" and one "
else
" to make sure it will work, but it still doesn't work.
if(UserExistRadioButton.Checked)
{
AllForm2 = AllText2.Text;
Form3 next3 = new Form3();
next3.Show();
this.Close();
}
else if (UserNotExistRadioButton.Checked)
{
AllForm2 = AllText2.Text;
Form3 next3 = new Form3();
next3.Show();
this.Close();
}
else
{
AllForm2 = AllText2.Text;
Form3 next3 = new Form3();
next3.Show();
this.Close();
}