如何在C#中将Windows窗体带到前面? [英] How do I bring a windows form to the front in C#?

查看:65
本文介绍了如何在C#中将Windows窗体带到前面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序 时,用户点击标签打开表单,表单打开背景中。我的意思是它没有出现在前面,但而是放在任务栏上。一旦用户取消该表格实例并再次点击标签,表单将在前景中打开。以下部分代码执行特定的工作。





  if (DataFormDlg.Instance.InvokeRequired)
{
DataFormDlg.Instance.BeginInvoke(
new ShowDataFormDelegate(ShowDataForm),pageId,timeout);
return ;
}

DataFormDlg.Instance.CurrentPageId = pageId;
DataFormDlg.Instance.Timeout = timeout;

if (!DataFormDlg.Instance.Visible)
DataFormDlg.Instance.ShowDialog();
else
DataFormDlg.Instance.Focus();





这里的DataFormDlg  从窗体中派生。





我尝试过:



我听说BringToFront()和Activate()可能会有所帮助但不确定如何使用它们。

解决方案

我唯一看到过这种情况的时候是您正在从Dialog表单创建并显示一个表单(使用.Show而不是.ShowDialog)。对话框将始终位于顶部,直至被解除。然后你新创建的表格将具有焦点并位于顶部。


你的代码看起来很可疑...



为什么会你从一个任意线程创建一个表单?



为什么模型表单已经可见?



什么是Instance属性(或者什么是 DataFormDlg 的类型?)。



调用的代码在哪里 DataFormDlg 构造函数?



在大多数应用程序中,您永远不需要使用 BringToFront( ) Activate() Focus() TopMost 。显然,你把简单的东西变得非常复杂。



通常,你只需做这样的事情就可以了:

  void  ParentForm_ShowDialogClicked( object  sender,EventArgs args)
{
< span class =code-keyword> using ( var form = new DataFormDlg() )
{
form.ShowDialog( this );如果在UserControl中, // 或所有者的ParentForm。
}
}


In my application when the user clicks on a label to open a form the form opens in the background.I mean it does not come to the front but is rather placed on taskbar. once the user cancels that instance of form and again clicks on the label,the form opens in foreground.Following portion of code does the specific work.



if (DataFormDlg.Instance.InvokeRequired)
       {
           DataFormDlg.Instance.BeginInvoke(
               new ShowDataFormDelegate(ShowDataForm), pageId, timeout);
           return;
       }

       DataFormDlg.Instance.CurrentPageId = pageId;
       DataFormDlg.Instance.Timeout = timeout;

       if (!DataFormDlg.Instance.Visible)
           DataFormDlg.Instance.ShowDialog();
       else
           DataFormDlg.Instance.Focus();



Here the DataFormDlg is derived from windows form.



What I have tried:

I have heard that BringToFront() and Activate() might help but not sure on how to use them.

解决方案

The only time I've seen this happen is when you're creating and showing a form (with .Show instead of .ShowDialog) from a Dialog form. The dialog will always be on top until it's dismissed. Then your newly created form will have the focus and be on top.


You code look suspicious...

Why would you create a form from an arbitrary thread?

Why a model form would be already visible?

What is Instance property (or what is the type of DataFormDlg?).

Where is the code that call DataFormDlg constructor?

In most application, you should never need to use BringToFront(), Activate(), Focus() or TopMost. Obviously, you make simple things very complex.

Usually, you simply do something like this and it works:

void ParentForm_ShowDialogClicked(object sender, EventArgs args)
{
    using (var form = new DataFormDlg())
    {
        form.ShowDialog(this);    // or ParentForm for owner if inside a UserControl.
    }
}


这篇关于如何在C#中将Windows窗体带到前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆