【问题标题】:Getting value from radiobutton for textbox in C#从 C# 中的文本框的单选按钮获取值
【发布时间】:2015-01-09 17:15:37
【问题描述】:

我有一个 C# 表单应用程序。我需要在文本框中获取单选按钮值。我知道这些方法是私有的,我更改了公共但它没有用。我需要按下按钮获取 value1、value2、value3 和 value4。谁能告诉我在文本框中获取值的方法....

private void groupBox1_Enter(object sender, EventArgs e)
    {
        double value1;
        if (radioButton1.Checked)
            value1 = 0.9;
        else if (radioButton2.Checked)
            value1 = 0.8;
        else if (radioButton3.Checked)
            value1 = 0.7;
        else if (radioButton4.Checked)
            value1 = 0.3;
        else if (radioButton5.Checked)
            value1 = 0.5;
        else
            MessageBox.Show("Oda Tipi girilmedi.");
    }

    private void groupBox2_Enter(object sender, EventArgs e)
    {
        double value2;
        if (radioButton6.Checked)
            value2 = 1;
        else if (radioButton7.Checked)
            value2 = 0.8;
        else if (radioButton8.Checked)
            value2 = 0.6;
        else
            MessageBox.Show("İzolasyon Tipi girilmedi.");
    }

    private void groupBox3_Enter(object sender, EventArgs e)
    {
        double value3;
        if (radioButton9.Checked)
            value3 = 0.9;
        else if (radioButton10.Checked)
            value3 = 1;
        else
            MessageBox.Show("Cam Tipi girilmedi.");

    }

    private void groupBox4_Enter(object sender, EventArgs e)
    {
        double value4;
        if (radioButton11.Checked)
            value4 = 1;
        else if (radioButton12.Checked)
            value4 = 0.9;
        else
            MessageBox.Show("Formül katsayısı girilmedi.");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox5.Text=Convert.ToString(value1*value2*value3*value4*(Convert.ToDouble(textBox2.Text))*(Convert.ToDouble(textBox3.Text))*(Convert.ToDouble(textBox4.Text)));

    }

【问题讨论】:

    标签: c# textbox radio-button


    【解决方案1】:

    您可以将变量(value1 等)移动到类范围,或者将所有内容放在 @SteveDanner 建议的 button1_Click 事件处理程序中,或者您可以编写更通用的解决方案。如果您创建更多选项(RadioButtons),它可以很容易地扩展。

    // Store values for each RadioButton in a dictionary.
    private Dictionary<RadioButton, double> values = 
        new Dictionary<RadioButton, double>();
    
    private Dictionary<GroupBox, string> messages = 
        new Dictionary<GroupBox, string>();
    
    public Form1()
    {
          InitializeComponent();
    
          // Associate values with radio buttons.
          values[radioButton1] = 0.9;
          // repeat the same for others...
    
          // Associate values messages with group boxes.
          messages[groupBox1] = "Oda Tipi girilmedi.";
          // repeat the same for others...
    }
    
    #region GroupBox.Enter event handlers.
    
    private void groupBox1_Enter(object sender, EventArgs e)
    {
        RadioButton radioButton = GetSelectedRadioButton(sender as GroupBox);
        if (radioButton == null)
        {
            MessageBox.Show(messages[sender as GroupBox]);
        }
    }
    
    // Here you can either repeat the same for other group boxes 
    // or simply assign this event hander to all of them. 
    // It will get the right message for each group.
    
    #endregion
    
    // Gets the selected radio button from the specified group.
    private void RadioButton GetSelectedRadioButton(GroupBox groupBox)
    {
        RadioButton radioButton = 
            groupBox
            .Controls
            .OfType<RadioButton>()
            .Where(rb => rb.Checked)
            .FirstOrDefault();
        return radioButton;
    }
    
    // Gets selected value from the specified group.
    private double GetSelectedValue(GroupBox groupBox)
    {
        RadioButton radioButton = GetSelectedRadioButton(groupBox);
        if (radioButton == null)
        {
            // Nothing selected yet.
            return double.NaN;
        }
        else
        {
            // Get the value from the dictinary.
            return values[radioButton];
        }
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        // Get the selected values.
        double value1 = GetSelectedValue(groupBox1);
        double value2 = GetSelectedValue(groupBox2);
        double value3 = GetSelectedValue(groupBox3);
        double value4 = GetSelectedValue(groupBox4);
    
        // Check other values in the same way.
        if (double.IsNaN(value1))
        {
            MessageBox.Show(message[groupBox1]);
        }
    
        textBox5.Text = Convert.ToString(
        value1 
        * value2 
        * value3 
        * value4 
        * (Convert.ToDouble(textBox2.Text)) 
        * (Convert.ToDouble(textBox3.Text)) 
        * (Convert.ToDouble(textBox4.Text)));
    
    }
    

    【讨论】:

    • @orcanyedal 如果您将来有更多单选按钮,您可能会考虑使用更通用的解决方案。查看更新的代码。
    【解决方案2】:

    问题是从RadioButtons 创建的值是方法处理程序的局部变量。您需要删除您的 groupBox_Enter 处理程序并像这样处理 button1_Click 事件:

    private void button1_Click(object sender, EventArgs e)
    {
        double value1;
        if (radioButton1.Checked)
            value1 = 0.9;
        else if (radioButton2.Checked)
            value1 = 0.8;
        else if (radioButton3.Checked)
            value1 = 0.7;
        else if (radioButton4.Checked)
            value1 = 0.3;
        else if (radioButton5.Checked)
            value1 = 0.5;
        else
        {
            MessageBox.Show("Oda Tipi girilmedi.");
            return; //not sure if this is what you want here?
        }
    
        double value2;
        if (radioButton6.Checked)
            value2 = 1;
        else if (radioButton7.Checked)
            value2 = 0.8;
        else if (radioButton8.Checked)
            value2 = 0.6;
        else
        {
            MessageBox.Show("Izolasyon Tipi girilmedi.");
            return; //not sure if this is what you want here?
        }
    
        double value3;
        if (radioButton9.Checked)
            value3 = 0.9;
        else if (radioButton10.Checked)
            value3 = 1;
        else
        {
            MessageBox.Show("Cam Tipi girilmedi.");
            return; //not sure if this is what you want here?
        }
    
    
        double value4;
        if (radioButton11.Checked)
            value4 = 1;
        else if (radioButton12.Checked)
            value4 = 0.9;
        else
        {
            MessageBox.Show("Formül katsayisi girilmedi.");
            return; //not sure if this is what you want here?
        }
    
        textBox5.Text=Convert.ToString(value1*value2*value3*value4*(Convert.ToDouble(textBox2.Text))*(Convert.ToDouble(textBox3.Text))*(Convert.ToDouble(textBox4.Text)));
    
    }
    

    【讨论】:

    • 它不工作,我需要从单选按钮获取值到按钮点击事件中......
    • 很高兴听到这个消息!如果这是修复它的原因,请考虑将其标记为答案。这样其他人可以在未来受益
    猜你喜欢
    • 1970-01-01
    • 2012-11-08
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多