简体   繁体   中英

get focus on first form

I have two forms form1 and form2. I navigate to form2 using a button in form1. In form2 I have a button control; on button click, I show messageBox. when messageBox comes then it also lost focus of form1 but I want that it should not lost focus of form1. I have no concern with form2.

I don't think there's an easy way to prevent MessageBox from taking focus, and thats because a MessageBox is a dialog. (dialogs take focus from the program until they being closed)

The only way I can think of is creating new form that looks like a MessageBox, and using it instead.

try this

    if(MessageBox.Show("something")==DialogResult.OK)
    {
      form1.Focus();
    }

Or

if(MessageBox.Show("something")==DialogResult.OK)
{
  form1.Select();
}

There's no way a message box doesn't show like a dialog. Like Eliran Pe'er said, you should make a Form like a messagebox with a label and a button and use it like this.

MessageForm form = new MessageForm.Show();

If you use ShowDialog it's going to be the same thing as MessageBox.

In your form 1 you can use TopMost property = true in order to keep it in front all the time no matter what. But this is going to keep your form on top of all other open programs.

Another workaround would be after messagebox is closed by the user (this is not a bad option) you can call form 1 to BringToFront(). To do this, you can pass the instance of form1 to the form2 in the Show method. Use that parameter in your form2 constructor.

Are you using ShowDialog() method or Show() method to show your form2? If you are using ShowDialog() method, modify it as Show(). Because ShowDialog() method will not allow you to change the focus to main form (form1), until you close the sub form (form2)

Make sure you are using method,

form2.Show()

to Display form2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM