【问题标题】:Get the selected index of combobox获取组合框的选中索引
【发布时间】:2014-03-14 14:32:10
【问题描述】:

这是重新制作的问题,以便获得更好的解释并让您清楚地理解问题

我的问题是:我无法获取组合框选定索引的值(我在组合框上尝试了getter和setter函数,但没有成功,但是当我在文本框上尝试getter和setter函数时,它成功了. 下面这段代码没有在combo box上使用getter和setter函数,只是使用了pass值

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

如果你能看到上图,我在Login form上输入了usernamepassword,并且我选择了LanguageEnglish,当我点击Log on按钮时, Wait form 出现,当 Wait form 已经完成加载时,Choices form 出现,如果你能看到,有文字说 Welcome, Fuhans - Administrator,它匹配正确,就像我输入 usernamepassword 返回在Login form 中,但是缺少了一些东西,是Language,如果您可以在Choices form 中看到,Language 组合框文本没有显示任何内容**(当时我不使用Wait form 时,当我在Login form 中选择English Language 时,Choices form 中的Language 组合框将显示English,并且下图中的此系统也有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);
            }
        }

getter 和 setter

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

        public static string CurrentLoggedInUserType
        {
            get;
            set;
        }
    }

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

谢谢!

非常感谢您的回答!

【问题讨论】:

  • 在我们知道什么是等待表单以及登录表单后发生的情况之前无法回答。
  • 请先生再次检查问题。谢谢

标签: c# winforms forms combobox


【解决方案1】:

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

【讨论】:

  • 你的意思是像getter和setter函数?或者枚举与getter和setter不同?如果你说它和 getter 和 setter 函数一样,我已经尝试过了,但没有成功(仅适用于组合框,但从文本框中成功)
  • 没有。在某个地方,您可以用英语和普通话定义一个公共枚举。然后,当您调用第二个表单时,根据您选择的索引,您将适当的枚举传递给您的第二个表单。
猜你喜欢
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多