views:

935

answers:

5

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!

A: 
paywindow.ShowDialog(this)
DaDa
whats this , huh ?
Adinochestva
the parent of the Dialog box
DaDa
i know , but its nonsense , its not related to question at all , just try it
Adinochestva
continue your way friend! it has worked to me in a similar case! i just gave my idea.
DaDa
This does not work. ShowDialog() forces a window to be topmost.
Jared Harley
+1  A: 

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

Adinochestva
I was pretty sure this was going to work, but I couldn't ever get the Form2_FormClosed or Form2_FormClosing events to fire.I did find an answer watching for form2 to be disposed in another SO question. Thanks!
Jared Harley
you should add Form2_FormClosed to Closed event of form2 ! anyway , good luck
Adinochestva
Not sure what I did the first time, but I double-checked the FormClosed event and it hadn't been implemented properly - this is the solution I need - thanks!
Jared Harley
+1  A: 

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);
  ...
}
JaredPar
The problem is that I am opening the form with ShowDialog() rather than Show(). According to MSDN/Google, ShowDialog() opens a modal form, requiring interaction before returning to the parent form. I think your method would work if I was opening the form with Show().
Jared Harley
A: 

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.

Jared Harley
A: 
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.

invalidusername