简体   繁体   English

专注于第一个表格

[英]get focus on first form

I have two forms form1 and form2. 我有两个表单form1和form2。 I navigate to form2 using a button in form1. 我使用form1中的按钮导航到form2。 In form2 I have a button control; 在form2中我有一个按钮控件; on button click, I show messageBox. 点击按钮,我显示messageBox。 when messageBox comes then it also lost focus of form1 but I want that it should not lost focus of form1. 当messageBox出现时它也失去了form1的焦点,但我希望它不应该失去form1的焦点。 I have no concern with form2. 我不关心form2。

I don't think there's an easy way to prevent MessageBox from taking focus, and thats because a MessageBox is a dialog. 我不认为有一种简单的方法可以阻止MessageBox获得焦点,这就是因为MessageBox是一个对话框。 (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. 我能想到的唯一方法是创建一个看起来像MessageBox的新表单,然后使用它。

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. 就像Eliran Pe'er所说,你应该像一个带有标签和按钮的消息框一样制作一个Form,并像这样使用它。

MessageForm form = new MessageForm.Show();

If you use ShowDialog it's going to be the same thing as MessageBox. 如果使用ShowDialog,它将与MessageBox相同。

In your form 1 you can use TopMost property = true in order to keep it in front all the time no matter what. 在表单1中,您可以使用TopMost property = true ,以便无论如何都始终保持在最前面。 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(). 另一种解决方法是在用户关闭messagebox之后(这不是一个糟糕的选择),您可以将表单1调用到BringToFront()。 To do this, you can pass the instance of form1 to the form2 in the Show method. 为此,您可以将form1的实例传递给Show方法中的form2。 Use that parameter in your form2 constructor. 在form2构造函数中使用该参数。

Are you using ShowDialog() method or Show() method to show your form2? 您使用ShowDialog()方法或Show()方法来显示您的form2? If you are using ShowDialog() method, modify it as Show(). 如果您使用的是ShowDialog()方法,请将其修改为Show()。 Because ShowDialog() method will not allow you to change the focus to main form (form1), until you close the sub form (form2) 因为ShowDialog()方法不允许您将焦点更改为主窗体(form1),直到关闭子窗体(form2)

Make sure you are using method, 确保你使用的方法,

form2.Show()

to Display form2. 显示form2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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