0

我有一个ListBox,ButtonRadioButton. 当我点击一个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);                
    }          
}
4

2 回答 2

1

使用单选按钮的CheckedChanged事件:

radioButton.CheckedChanged += new System.EventHandler(radioButton_CheckedChanged);
private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        //your code to show price
    }
于 2013-07-20T03:14:17.530 回答
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";
}
于 2013-07-20T03:24:17.230 回答