C# 获取combobox的选定索引
这是一个重新制作的问题,以便得到更好的解释并让您清楚地理解问题 我的问题是:我无法获取组合框的选定索引的值(我在组合框上尝试了getter和setter函数,但没有成功,但是当我在文本框上尝试getter和setter函数时,它成功了。下面的代码没有在组合框上使用getter和setter函数,只是使用了pass值。C# 获取combobox的选定索引,c#,winforms,forms,combobox,C#,Winforms,Forms,Combobox,这是一个重新制作的问题,以便得到更好的解释并让您清楚地理解问题 我的问题是:我无法获取组合框的选定索引的值(我在组合框上尝试了getter和setter函数,但没有成功,但是当我在文本框上尝试getter和setter函数时,它成功了。下面的代码没有在组合框上使用getter和setter函数,只是使用了pass值。 Timer timer = new Timer(); private void Wait_Load(object sender, EventArgs e)
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);
}
}
我发布了混合图像(登录表单、等待表单、选择表单和数据库
):
如果您可以看到上面的图像,我已经在登录表单
上输入了用户名
和密码
,并且我已经选择了语言
到英语
,当我点击登录
按钮时,等待表单
会出现,当等待表单
已经完成加载时<代码>选择表单出现,如果您可以看到,有一个文本说欢迎,Fuhans-管理员
,它正确匹配,就像我在登录表单
中输入了用户名
和密码
,但是缺少了语言
,如果您可以在选择表单
,则语言
组合框文本不显示任何内容**(那时,当我不使用等待表单
时,选择表单
中的语言
组合框在登录表单
中选择英语
语言时,显示英语
,下面的图像也包含该系统的数据库
)
以下是我正在使用的代码:
登录表单代码:
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);
}
}
选择表单代码
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;
}
}
希望这个问题能让你们明白我的问题是什么,并能帮助我
谢谢大家!
非常感谢您的回答!我认为将控件传递给其他表单不是一个好主意。您可以定义一个公共语言枚举,将英语和汉语作为这两种语言,然后根据您选择的内容将枚举传递给您的选择表单。在我们知道等待表单是什么以及登录后发生了什么之前,无法回答表单。请再次检查问题,先生。谢谢你的意思是像getter和setter函数?或者枚举不同于getter和setter?如果你说它像getter和setter函数一样,我已经尝试过了,但没有成功(只针对组合框,但从文本框中它成功了)不可以。在某个地方,您使用英语和汉语定义了一个公共枚举。然后,当您调用第二个表单时,根据您选择的索引,您将相应的枚举传递给第二个表单。