1

我希望在未使用 try and catch 检查复选框 R1P1 时弹出一个消息框。但我不知道在 () 中放入什么内容?

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (R1P1.Checked) 
            {
                string Plats1 = "R1P1"; 
                TxtP.Text = Plats1;  
            }
            else 
            {
                TxtP.Text = null; 
            }
         }
        catch (???) 
        {
            MessageBox.Show("Hey");
        }

我试过 catch (R1P1.Checked == false;) 但它不起作用。在程序中,您有一堆票务系统的复选框,当您不检查其中任何一个但仍然单击继续时,我希望该复选框出现。

4

2 回答 2

3

在这种情况下,try{}catch{}块中根本没有任何意义。

为什么要捕获异常?

您已经知道何时不检查 - 只需调用您的else子句中的消息框:

if (R1P1.Checked) 
{
    TxtP.Text = "R1P1";  
}
else 
{
    TxtP.Text = ""; 
    MessageBox.Show("Hey");
}
于 2013-05-12T11:41:52.293 回答
2

try catch 有什么用?这行不通:

private void button2_Click(object sender, EventArgs e)
{

        if (R1P1.Checked) 
        {
            string Plats1 = "R1P1"; 
            TxtP.Text = Plats1;  
        }
        else 
        {
            TxtP.Text = null; 
             MessageBox.Show("Hey");
        }
 }

好点乔恩!AFAIK{}定义一个具有自己范围的代码块:因此,当您点击右括号时,通常在它们之间声明的任何内容都将不再存在。这条线Plats1从未使用过,在其他地方也看不到。

使其相同:

private void button2_Click(object sender, EventArgs e)
{
        if (R1P1.Checked) 
        {
            TxtP.Text = "R1P1";  
            //anything declared here (i.e. a new variable)
        }//is gone by here
        else 
        {
            TxtP.Text = null;
            MessageBox.Show("Hey");
        }
 }
于 2013-05-12T11:41:53.520 回答