Search code examples
c#winformsshowshowdialogtopmost

Can I set ShowDialog() to not be topmost?


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!


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