我有ListBox
,Button
和RadioButton
。当我点击button
时,ListBox
会列出不同类型的饮品。
我想让用户选择饮品大小并显示价格。当用户检查large radio button
大尺寸价格时会显示出来。价格是与数据库的链接。
问题是当我选择单选按钮时,直到我再次点击饮品按钮才会显示价格。我希望在选中单选按钮时显示价格。
这是我的编码
private void signatureMilkTeaButton_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
string constring = "datasource=localhost;port=3306;username=root;password=000";
string Query = "select* from database.drinks where drinks_Type ='M';";
MySqlConnection connectDatabase = new MySqlConnection(constring);
MySqlCommand commandDataBase = new MySqlCommand(Query, connectDatabase);
MySqlDataReader myReader;
try
{
connectDatabase.Open();
myReader = commandDataBase.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString("drinks_Name");
listBox1.Items.Add(sName);
}
{
decimal MMPrice = myReader.GetDecimal("drinks_MPrice");
decimal MLPrice = myReader.GetDecimal("drinks_LPrice");
if (MediumButton.Checked == true )
{
textBox1.Text = MMPrice.ToString();
}
else if (largeButton.Checked == true)
{
textBox1.Text = MLPrice.ToString();
}
}*/
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:1)
使用radiobutton的CheckedChanged
事件:
radioButton.CheckedChanged += new System.EventHandler(radioButton_CheckedChanged);
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
//your code to show price
}
答案 1 :(得分:0)
您将单选按钮处理放在按钮单击事件中,因此您必须单击按钮才能触发_Click
方法内的所有内容。而是为signatureMilkTeaButton_Click
事件的外单选按钮创建单独的方法:
private void MediumButton_CheckedChanged(object sender, EventArgs e)
{
ShowPrice();
}
private void LargeButton_CheckedChanged(object sender, EventArgs e)
{
ShowPrice();
}
private void ShowPrice()
{
//...your database commands...
if (MediumButton.Checked)
textBox1.Text = "price1";
else if (LargeButton.Checked)
textBox1.Text = "price2";
}