Question

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!

Was it helpful?

Solution

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 ;)

OTHER TIPS

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);
  ...
}

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.

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.

paywindow.ShowDialog(this)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top