获取所选的组合框索引

时间:2014-03-14 14:32:11

标签: c# winforms forms combobox

这是重新提出的问题,以便更好地解释并让您清楚地理解问题

我的问题是:我无法获得所选组合框索引的值(我在组合框中尝试了getter和setter函数,但没有成功,但是当我在文本框中尝试了getter和setter函数时,它成功了。下面这段代码在组合框中没有使用getter和setter函数,只是使用了传递值

我发布了混合图片(Login form, Wait form, Choices form and the database):

enter image description here

如果您看到上面的图片,我已在username上输入了passwordLogin form,并选择了LanguageEnglish ,当我点击Log on按钮时,Wait form出现,Wait form已经加载完毕,Choices form会出现,如果您看到,则会显示{ {1}},它与我在Welcome, Fuhans - Administrator中输入的usernamepassword正确匹配,但缺少某些内容,Login form,如果您可以看到LanguageChoices form组合框文字没有显示任何内容**(当我不使用Language时,Wait form显示中的Language组合框我在Choices form中选择了English EnglishLanguage Login form,此系统中的the database也是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(); } } } }

以下是我正在使用的代码:

登录表单代码

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

等待表单代码

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);
            }
        }

选择表单代码

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

        public static string CurrentLoggedInUserType
        {
            get;
            set;
        }
    }

吸气者和二传手

{{1}}

希望这个重新提出问题让你们明白我的问题是什么,并且可以帮助我。

谢谢!

你的回答非常感谢!

1 个答案:

答案 0 :(得分:1)

我认为将控件传递给其他表单并不是一个好主意。您可以使用英语和普通话作为两者来定义公共语言枚举,并根据您选择的内容将枚举传递到您的选择表单。