How do I exit from a function?

前端 未结 7 1294
野的像风
野的像风 2021-02-18 20:18

i know that in vb.net you can just do Exit Sub

but i would like to know how do i exit a click event in a button?

here\'s my code:

pr         


        
7条回答
  •  渐次进展
    2021-02-18 20:49

    Use the return keyword.

    From MSDN:

    The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return the value of the optional expression. If the method is of the type void, the return statement can be omitted.

    So in your case, the usage would be:

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
        {
            return; //exit this event
        }
    }
    

提交回复
热议问题