Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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)
            {
                // Blokuj kontrolkę Username_TextChanged
                Username.Enabled = false;

                // Odblokuj kontrolkę ExistUserList_SelectedIndexChanged
                ExistUserList.SelectedIndexChanged -= ExistUserList_SelectedIndexChanged;
                ExistUserList.Enabled = true;
            }
        }

        private void UserNotExistRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (UserNotExistRadioButton.Checked)
            {
                // Odblokuj kontrolkę Username_TextChanged
                Username.Enabled = true;

                // Blokuj kontrolkę ExistUserList_SelectedIndexChanged
                ExistUserList.SelectedIndexChanged += ExistUserList_SelectedIndexChanged;
                ExistUserList.Enabled = false;
            }
        }

        //-----------------------------------------------------------------
        private void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Pobierz wybrany element z kontrolki ExistUserList
            string selectedText = ExistUserList.SelectedItem.ToString();

            // Ustaw wybrany tekst w kontrolce AllText2
            AllText2.Text = selectedText;
        }

        //--------------------------------------------------------------------

        private void UpdateAllText2()
        {

            string username = Username.Text;

            // Połącz teksty spacjami
            string allText = $"Nazwa użytkownika: {username}";

            // Ustaw połączony tekst w TextBoxie AllText
            AllText2.Text = allText;
        }

        //--------------------------------------------------------------------

        private void NextForm3_Click(object sender, EventArgs e)
        {
            string password = FirstPassword.Text;

            // Sprawdź, czy w haśle jest chociaż jedna liczba
            string password1 = FirstPassword.Text;
            bool containsDigit = false;

            foreach (char c in password1)
            {
                if (char.IsDigit(c))
                {
                    containsDigit = true;
                    break;
                }
            }

            // Sprawdź, czy w haśle jest chociaż jeden znak specjalny
            string password2 = FirstPassword.Text;
            bool containsSpecialCharacter = false;

            foreach (char c in password2)
            {
                if (!char.IsLetterOrDigit(c))
                {
                    containsSpecialCharacter = true;
                    break;
                }
            }

            // Sprawdź, czy hasło zawiera co najmniej jedną dużą literę
            string password3 = FirstPassword.Text;
            bool hasUpperCaseLetter = false;

            foreach (char character in password3)
            {
                if (char.IsUpper(character))
                {
                    hasUpperCaseLetter = true;
                    break;
                }
            }

            // Sprawdź, czy hasło zawiera co najmniej jedną dużą literę
            string password4 = FirstPassword.Text;
            bool hasLowerCaseLetter = false;

            foreach (char character in password4)
            {
                if (char.IsLower(character))
                {
                    hasUpperCaseLetter = true;
                    break;
                }
            }
            //............................................................................

// Sprawdź, czy przycisk radio jest zaznaczony i czy nie został wybrany element w UserList
            if (UserExistRadioButton.Checked)
            {
                if (ExistUserList.SelectedItem == null)
                {
                    // Wyświetl komunikat
                    MessageBox.Show("Wybierz istniejącą nazwę użytkownika", 
                    "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return; // Przerwij działanie metody, aby nie kontynuować
                }
            }

            // Sprawdzamy, czy przycisk radio UserNotExistRadioButton jest zaznaczony
            else if (UserNotExistRadioButton.Checked)
            {
                // Sprawdzamy, czy pole Username nie jest puste
                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")
                {
                    // Hasło jest niepoprawne, wyświetl komunikat
                    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:
C#
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.
C#
 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();
}
Posted
Updated 19-Sep-23 10:28am
v2

1 solution

C#
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();
}

Why do you have three conditions that each run exactly the same code? But beside that, in each case you create a local variable of Form3. So as soon as that block of code returns, the variable will go out of scope and be destroyed. You need to make a global Form3 variable so it does not get destroyed automatically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900