我可以将 ShowDialog() 设置为不在最顶层吗?

发布于 2024-07-18 21:27:16 字数 702 浏览 9 评论 0原文

有没有办法将 ShowDialog() 设置为不在最顶层? 我查看了所有相关的 SO 问题,但没有一个完全符合我的情况。

我所做的是从 datagridview 按钮列打开一个新的 WinForm。 这个新表单从几个 SQLite 表中提取信息,并允许用户将信息添加到单击按钮的行。

我使用下面的代码打开 WinForm。 我使用 ShowDialog() 方法,这样我就可以判断用户是保存表单中的信息还是取消它。

Pay_Bill_Window paywindow = new Pay_Bill_Window(getClickedRowID);
if (paywindow.ShowDialog() == DialogResult.OK)
{
    FillPendingPaymentDataGrid(dbAccess.GetPendingBills());
}

我这样做是为了判断是否需要重新加载 datagridview 中的信息。

用户可以填写到窗口中的信息来自其他来源,例如 Web 浏览器,因此将表单置于所有应用程序之上并不理想。

有没有办法阻止窗口位于所有应用程序的顶部(我的一系列窗口中的最顶部就可以),或者有没有办法告诉用户在另一个表单上单击哪个按钮(基本上,使用 paywindow.Show() 并观察不同类型的回报)?

谢谢你的帮助!

Is there a way I can set a ShowDialog() to not be topmost? I've looked at all the related SO questions, and none quite matched my situation.

What I do is open a new WinForm from a datagridview button column. This new form pulls information from a few SQLite tables and allows the user to add information to the row the button was clicked.

I open the WinForm using the code below. I use the ShowDialog() method so I can tell if the user saves the information in the form or cancels it.

Pay_Bill_Window paywindow = new Pay_Bill_Window(getClickedRowID);
if (paywindow.ShowDialog() == DialogResult.OK)
{
    FillPendingPaymentDataGrid(dbAccess.GetPendingBills());
}

I do this so I can tell if I need to reload the information in the datagridview.

The information the user can fill into the window is from other sources, like a web browser, so having the form be on top of all applications is not ideal.

Is there a way I can stop the window from being on top of all applications (top-most in my series of windows is fine), or is there a way to tell what button a user clicks on another form (basically, using paywindow.Show() and watching for a different type of return)?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

流年里的时光 2024-07-25 21:27:16

默认情况下,表单不应该是“对于所有应用程序意义上的”TopMost。 如果您看到这种行为,很可能是因为该属性是在设计器中设置的。 在设计器中取消设置该属性或尝试以下操作。

using ( Form form = CreateMyForm() ) {
  form.TopMost = false;
  form.ShowDialog(parent);
  ...
}

By default a form should not be TopMost in the "for all applications sense". If you're seeing that behavior it's likely because the property was set in the designer. Either unset the property in the designer or try the following.

using ( Form form = CreateMyForm() ) {
  form.TopMost = false;
  form.ShowDialog(parent);
  ...
}
情绪操控生活 2024-07-25 21:27:16

使用这样的东西:
form1 :

  private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
            frm.FormIsClosing += frm_FormIsClosing;

        }

        void frm_FormIsClosing(object sender, DialogResult rsl)
        {
            if (rsl == System.Windows.Forms.DialogResult.Yes)
                MessageBox.Show("We got it");
        }

form2 :

   public delegate void IsClosing(object sender, DialogResult rsl);

        public event IsClosing FormIsClosing;


        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            FormIsClosing(this, System.Windows.Forms.DialogResult.Yes);
        }

然后关闭 form2 , FormIsClosing 触发,你可以在 from1 中捕获它;)

use something like this :
form1 :

  private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
            frm.FormIsClosing += frm_FormIsClosing;

        }

        void frm_FormIsClosing(object sender, DialogResult rsl)
        {
            if (rsl == System.Windows.Forms.DialogResult.Yes)
                MessageBox.Show("We got it");
        }

form2 :

   public delegate void IsClosing(object sender, DialogResult rsl);

        public event IsClosing FormIsClosing;


        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            FormIsClosing(this, System.Windows.Forms.DialogResult.Yes);
        }

then you close the form2 , FormIsClosing fires and you can catch it in from1 ;)

不一样的天空 2024-07-25 21:27:16

我还没有找到一种方法来防止表单在使用 ShowDialog() 显示时位于最顶层(这将表单显示为模式,我不知道如何覆盖它)。

然而,我找到了一种方法来观察第二种形式,当用 Show() 显示时,通过观察 这个 StackOverflow 问题

来自 Ian

Form2 form2 = null;

void button_click(object sender, EventArgs e) 
{     
    if(form2 == null)   
    {
        form2 = new Form2();
        form2.Disposed += new EventHandler(f_Disposed);
        form2.Show();
    }
}

void f_Disposed(object sender, EventArgs e) 
{
    form2 = null; 
}

据我所知,这不允许我了解为什么表单被关闭(就像DialogResult),因此每次表单关闭时都会触发该事件,这适合我的情况。

I haven't found a way to prevent the form from being top-most when shown with ShowDialog() (this shows the form as a modal, and I don't know how to override it).

However, I found a way to watch for the second form, when shown with Show(), by watching for the form.Disposed event in this StackOverflow question.

From Ian:

Form2 form2 = null;

void button_click(object sender, EventArgs e) 
{     
    if(form2 == null)   
    {
        form2 = new Form2();
        form2.Disposed += new EventHandler(f_Disposed);
        form2.Show();
    }
}

void f_Disposed(object sender, EventArgs e) 
{
    form2 = null; 
}

As far as I can tell, this doesn't allow me to see why the form was closed (like with DialogResult), so the event fires every time the form closes, which is okay for my situation.

末骤雨初歇 2024-07-25 21:27:16
paywindow.ShowDialog(this)

应该管用。 附加参数告诉父级如何,并将使其分层在父级之上,但不是最顶层。 最顶层意味着即使您激活另一个窗口,该窗口也会保留在桌面上。 这不是 ShowDialog 的默认行为。 正如 JaredPar 提到的,将表单/窗口设置为 TopMost=false 为 true。

paywindow.ShowDialog(this)

should work. the additional parameter tells how the parent is and will make it layer on top of the parent, but not topmost. Topmost means the window will stick around on your desktop even when you Activate another window. That's not default behavior for a ShowDialog. As JaredPar mentioned, set your form/window to TopMost=false to be true.

他不在意 2024-07-25 21:27:16
paywindow.ShowDialog(this)
paywindow.ShowDialog(this)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文