Kaoru
Kaoru

Reputation: 2883

Get the selected index of combobox

This is the re-make question in order to get the better explanation and to have you understand the question clearly

My problem is: I cannot get the value of selected index of combo box (I have tried the getter and setter function on combo box, but no success, but when I tried the getter and setter function on text box, it succeed. This code below not using the getter and setter function on combo box, just using the pass value

I posting the mixed images (Login form, Wait form, Choices form and the database):

enter image description here

If you can see the above image, I have entered the username and password on the Login form, and I have select the Language to English, and when I click Log on button, the Wait form appear and when the Wait form already finished loading, the Choices form appear and if you can see, there is text that says Welcome, Fuhans - Administrator, it matched properly like I entered the username and password back in the Login form, but something is missing, is the Language, if you can see in the Choices form, the Language combo box text not displaying anything **(Back then when I not using Wait form, the Language combo box in the Choices form display English as I select the English Language in the Login form and this image below also have the database for this system.

Here is the code that I am using:

Login form code:

Wait _wait = new Wait();
        string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\db1.accdb";
        bool isValidPassword = false;

        private void Login_Load(object sender, EventArgs e)
        {
           this.comboBox1.Items.Add("English");
            this.comboBox1.Items.Add("Mandarin");

            this.comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "SELECT * FROM [Member] WHERE [Username] = @Username";

                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    cmd.Parameters.Add("@Username", System.Data.OleDb.OleDbType.VarChar);
                    cmd.Parameters["@Username"].Value = this.textBox1.Text;

                    using (OleDbDataReader dReader = cmd.ExecuteReader())
                    {
                        if (dReader.Read())
                        {
                            _wait.ShowDialog();

                            UserInformation.Password = (string)dReader["Password"];

                            isValidPassword = BCrypt.CheckPassword(this.textBox2.Text, UserInformation.Password);

                            if (isValidPassword)
                            {
                                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                                sound.Play();

                                DialogResult _dialogResult = MessageBox.Show("Verified", "Congratulations", MessageBoxButtons.OK);

                                if (_dialogResult == DialogResult.OK)
                                {
                                    UserInformation.CurrentLoggedInUser = (string)dReader["Username"];
                                    UserInformation.CurrentLoggedInUserType = (string)dReader["UserType"];

                                    this.Hide();

                                    Choices _choices = new Choices();
                                    _choices.ShowDialog();

                                    this.Close();
                                }
                            }

                            else if (!isValidPassword)
                            {
                                MessageBox.Show("Not Verified", "Warning", MessageBoxButtons.OK);
                            }
                        }

                        dReader.Close();
                        conn.Close();
                    }
                }
            }
        }

Wait form code

Timer timer = new Timer();

        private void Wait_Load(object sender, EventArgs e)
        {
            timer.Interval = 2000;
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            this.Hide();
            this.Close();
        }

Choices form code

private Login _login;

        public Choices(Login _login)
            : this()
        {
            this._login = _login;
        }

        private void Choices_Load(object sender, EventArgs e)
        {
            this.label6.Text = UserInformation.CurrentLoggedInUser + " " + " " + "-" + " " + " " + UserInformation.CurrentLoggedInUserType;
            this.label6.ForeColor = System.Drawing.Color.White;

            if (_login.comboBox1.SelectedIndex == 0)
            {
                this.comboBox1.Text = "English";
            }

            else if (_login.comboBox1.SelectedIndex == 1)
            {
                this.comboBox1.Text = "Mandarin";
                this.button1.Location = new Point(155, 87);
                this.label1.Text = "";
                this.label2.Text = "";
                this.button2.Location = new Point(153, 163);
                this.label3.Text = "";
                this.button3.Location = new Point(135, 236);
            }
        }

Getter and setter

internal class UserInformation
    {
        public static string CurrentLoggedInUser
        {
            get;
            set;
        }

        public static string CurrentLoggedInUserType
        {
            get;
            set;
        }
    }

Hope this re-make question make you guys clear what my problem is and could help me.

Thank you!

Your answer much appreciated!

Upvotes: 0

Views: 309

Answers (1)

boiledham
boiledham

Reputation: 147

I don't think it's a good idea to pass controls to other forms. You could instead define a public Language enum with English and Mandarin as the two, and pass the enum to your Choices form based on what you have selected.

Upvotes: 1

Related Questions