ComboBox1优先于单选按钮?

时间:2015-12-07 19:45:50

标签: c# winforms

如果我首先检查一个单选按钮或者我是否先检查组合框项目,怎样才能使它始终工作?目前只有在首先检查单选按钮时它才有效。谢谢。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if ( comboBox1.Text == "Test1" && radioButton1.Checked)
    {
        StreamReader sr = new StreamReader(@"my path");
        string str = sr.ReadToEnd();
        textBox1.Text = str;
    }
}

3 个答案:

答案 0 :(得分:1)

如果你想让测试成立,那么组合框文本是否为" Test1"或者检查readiobutton,你需要使测试成为OR而不是AND,如下所示:

if ( comboBox1.Text == "Test1" || radioButton1.Checked)

如果两个条件都需要为真,请尝试此(伪代码):

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ConditionFulfilled)
    {
        readThatPuppy();
    }
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (ConditionFulfilled)
    {
        readThatPuppy();
    }
}

private bool ConditionFulfilled()
{
    return (comboBox1.Text.Equals("Test1") && radioButton1.Checked;
}

private void  readThatPuppy()
{
    StreamReader sr = new StreamReader(@"my path");
    string str = sr.ReadToEnd();
    textBox1.Text = str;
}

答案 1 :(得分:1)

我猜这个事件只与组合框事件有关。您需要将此代码移动到一个公共函数中,并从ComboBox SelectedIndexChanged事件和radiobuttons更改事件中调用它。

答案 2 :(得分:0)

仅在选中单选按钮时才起作用的原因是因为您在事件处理程序中有这种情况。

尝试以下操作,看看它是否能满足您的需求。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if ( comboBox1.Text == "Test1" || radioButton1.Checked)
    {
        StreamReader sr = new StreamReader(@"my path");
        string str = sr.ReadToEnd();
        textBox1.Text = str;
    }
}