c# - 单选按钮和显示

标签 c# mysql textbox radio-button messagebox

我有一个 ListBoxButtonRadioButton 。当我单击按钮时,不同种类的饮料将在ListBox上列出。

我想让用户选择饮料尺寸并显示价格。当用户选中大单选按钮时,将显示大尺寸价格。价格与数据库链接。

问题是当我选择单选按钮时,只有再次单击饮料按钮后才会显示价格。我希望在选中单选按钮时显示价格。

这是我的编码

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);                
    }          
}

最佳答案

使用单选按钮的CheckedChanged事件:

radioButton.CheckedChanged += new System.EventHandler(radioButton_CheckedChanged);
private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        //your code to show price
    }

关于c# - 单选按钮和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17758263/

相关文章:

mysql - 最后一个条目 SQL 的 UPDATE 字段

mysql - Codeigniter 中的全局 MySQL 查询

wpf - 如何仅在XAML中设置自动对焦?

c# - 如何自动滚动到多行文本框的底部?

保留属性的 C# Typedef

c# - 为什么在内部类上 C# 动态找不到接口(interface)的方法?

mysql - SO Prisma GraphQL 错误 : Whoops. 看起来像内部服务器错误

mysql - 选择组合框后用 mysql 数据填充文本框

c# - 为什么在 WPF 命令的关闭中捕获自由变量会导致它无法运行?

c# - c#中的句子大小写格式