0

I have a ListBox , Button and RadioButton. When I click a button, different kind of drink will list out on the ListBox.

I want let user select drinks size and show out the price. When user check large radio button large size price will show out. The price was link with the database.

The problem is when I select the radio button, the price won't show until I click the drinks button again. I want the price show when then radio button was checked.

Here is my coding

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 に答える