문제

I am validating input on textboxes on the FormClosing event of my Form and if the user decides to cancel closing, I set e.Cancel = true so as to stop the FormClosing event. I then try to set focus to the first textbox but this is not working.

private void AddDriver_Window_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this._Cancel(sender, e))
    {
        e.Cancel = true;
        this.textboxFirstName.Focus();
    }
}

If you are interested in seeing the ._Cancel method I asked an unrelated question earlier.

도움이 되었습니까?

해결책

I tried this one Check this is it work for you..

private void AddDriver_Window_FormClosing(object sender, FormClosingEventArgs e)
{
    if( DialogResult.Cancel == MessageBox.Show("Do you want to exit programm","Alert",MessageBoxButtons.OKCancel))
    {
        e.Cancel = true;
        textBox1.Focus();
    }
}

this will popup a message box and if click cancel then automatically focus on the textbox1.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top