none
C# Windows Form's ShowDialog()

    Question

  • Hi, I am new to C#.Net. I am developing C#.Net windows application. I have used different forms. To switch from one form to another I have used Form's .ShowDialog() property. If I clicked any of control (Say Button) of Form, then form will automatically get closed. But for some forms, it will work fine. But some will automatically get closed. Why? I didn't know the cause?

    The form code is like that :

            private void lblArray_Click(object sender, System.EventArgs e)
            {
                Label lblClicked = new Label();
                lblClicked = (Label) sender;

                switch (lblClicked.Name)
                {                
                    case "lblEmployeeDetails":
                    {
                        frmEmpInfo oFrm = new frmEmpInfo();
                        oFrm.ShowDialog();
                        break;
                    }           

                    case "lblExit":
                    {
                        this.Close();
                        break;
                    }
                }
            }      


    Does anybody know solution? Thanks in Advance.
    Thursday, October 02, 2008 2:55 PM

Answers

All replies

  • Your question is very unclear.  Looks to me that your form will indeed close if you click the "Exit" label.  That's what your code does anyway.

    Thursday, October 02, 2008 6:45 PM
  • A dialog captures a value and closes. You want to just .Show the form.

     

    A.D.T.

     

    Thursday, October 02, 2008 6:48 PM
  • Hi, nobugz. Sorry. But don't look at just 'lblExit' label. Purposefully I have closed the current form while clicking on lblExit. Here I am talking about 'lblEmployeeDetails' label. While clicking on Employee Details (i.e lblEmployeeDetails) the control goes to frmEmpInfo.cs., and when I hit any key on frmEmpInfo.cs (i.e Employee Information form), then control come back to the following code:

    frmEmpInfo oFrm = new frmEmpInfo();
    oFrm.ShowDialog();
    break;

    And execute the break statment. But for another form(Say frmLeaveRemain), it work fine. I mean, control stops at oFrm.ShowDialog();, and when I close the frmLeaveRemain purposefully (using this.close() method) then and then only break will executed. Why?

    Hope you are understanding. Is any help from your side? Thanks. 

    Friday, October 03, 2008 6:32 AM
  • Hi A.D.T, Thanks for reply. It will work with .Show(). But problem with .Show() is that, it can allow any background form for editing. Suppose, if you call Form2 from From1 using .Show(), then Form2 displays as I want. But it allow to edit Form1 also when Form2 is open. I need Form1 will open if and only if Form2 will close.

    Hope you are understanding. Is any method to solve this one? I am using C#.Net 1.1 version. 
    Friday, October 03, 2008 6:45 AM
  • Still very unclear, what is frmLeaveRemain?  Everything sounds like it is working like it should, ShowDialog() indeed "pauses" execution and doesn't return until the modal dialog is closed.  Show() behaves very differently.  There are several ways to close the dialog and get ShowDialog() to return.  Closing the owner form is one of them, I'd guess frmLeaveRemain fits that bill.

    The reason ShowDialog() behaves this way is to make it easy to use dialogs in your code.  You want to ask the user a question, call ShowDialog() and you'll get the answer.
    Friday, October 03, 2008 10:05 AM
  • Hi  nobugz. I am not getting you. I have a winform which I'm opening with Frm.ShowDialog(). But, when I click on  any button (e.g ADD Button ) its  closing my form also.

    How I can stop such form behavior? Now it is clear?

    Friday, October 03, 2008 11:19 AM
  • Just use .Show() instead of .ShowDialog()...

    Friday, October 03, 2008 11:57 AM
  • Hi Sreenath, Thanks for reply. It will work with .Show(). But problem with .Show() is that, it can allow any background form for editing. Suppose, if you call Form2 from From1 using .Show(), then Form2 displays as I want. But it allow to edit Form1 also when Form2 is open. I need Form1 will open if and only if Form2 will close.

    Hope you are understanding. Is any method to solve this one? I am using C#.Net 1.1 version. 
    Friday, October 03, 2008 1:43 PM
  •  Vijay Jadhav wrote:

    Hi Hans Passant. I am not getting you. I have a winform which I'm opening with Frm.ShowDialog(). But, when I click on  any button (e.g ADD Button ) its  closing my form also.

    How I can stop such form behavior? Now it is clear?

     

    In form closing event, just use e.cancel

     

    A.D.T.

    Friday, October 03, 2008 4:12 PM
  • Check the form's AcceptButton and CancelButton properties.  Other than that, any assignment to the DialogResult property would close the dialog.
    Friday, October 03, 2008 5:15 PM
  • Hi  A.D.T.,

    In form closing event, no such method for e.Cancel.

    My Sample code is :

            private void btnClose_Click(object sender, System.EventArgs e)
            {
                this.Close();
                /* e.  Cannot find method Cancel */
            }
    If I am wrong, reply with details.

    Thanks.

    Monday, October 06, 2008 6:19 AM
  • Form's AcceptButton and CancelButton properties sets to none. No assignment to DialogResult property.
    Monday, October 06, 2008 6:30 AM
  • Please post code of the class frmEmpInfo.cs
    It can help in fixing the problem.

    http://abansal.wordpress.com/
    Monday, October 06, 2008 7:11 AM
  • Sure. Do check ur Gmail account.
    Monday, October 06, 2008 7:21 AM
  • Does anybody know solution? Please reply.
    Wednesday, October 08, 2008 1:15 PM
  • Form_Closing event not button_Click

     

    e.Cancel

     

    A.D.T.

     

    Wednesday, October 08, 2008 4:47 PM
  • Does anyone know?
    Thursday, October 16, 2008 1:17 PM
  • You'll have to work with Amit, s/he's the only one that got your code.
    Thursday, October 16, 2008 2:24 PM
  • Hello Vijay Jadhav,

     

    The best idea is to use the "secondForm.Show();" command. If you don't want them to be able to do anything with the mainForm, run the following commands:

     

    Code Snippet

    foreach (Control ctrls in this.Controls)

    {

    ctrls.Enabled = false;

    }

     

     

    Run this loop again, but with 'false' being 'true' instead when you are done.

     

    Hope this helps!

    Thanks!

    chukrum47

    Thursday, October 16, 2008 9:12 PM
  • this.Enable = false would be a short-cut for that.  This thread looks pretty dead to me, private emails don't work.
    Thursday, October 16, 2008 9:29 PM
  • Does anyone know? Please help!

    Thanks in Advance.
    Monday, October 20, 2008 5:12 AM
  • Hi A.D.T. and All,
    Thanks man! You solved my problem!
    As you specified, in form closing event use,

    e.cancel = true;

    Code Snippet :
    Event Handler :
    this.Closing += new CancelEventHandler(Form_Closing);
    bool IsCloseClick = true;

           private void btnClose_Click(object sender, System.EventArgs e)
           {
                IsCloseClick = false;
                this.Close();
           }

            private void Form_Closing(object sender, CancelEventArgs e)
            {
                if(sender == this)
                {           
                    if(!IsCloseClick)
                    {
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = true;      
                    }
                }       
            }


    Work Gr8!
    Sorry nobugz, for Unclear problem definition. In future, I will try my best.
    Once again Thanks A. D. T. and all guys for being in touch. Finally, we got the solution.

    Thanks & Regard’s,
    Mr. Vijay L. Jadhav, India.
    Monday, October 20, 2008 11:27 AM
  • Go to the property dialog for the command buttons that are causing you problems and change the DialogResult property to "none". When changing the AcceptButton on the Form, .NET automatically sets the DialogResult Property on other buttons (although not consistent) to "Cancel". Quite strange behaviour....

    Regards,

    Milo
    • Proposed as answer by Ultimate V Wednesday, May 29, 2013 8:43 PM
    Monday, January 05, 2009 11:25 PM
  • Thanks Milo for reply.
    Friday, July 31, 2009 1:03 PM
  • Echo that, thank you very much
    Wednesday, November 25, 2009 11:20 PM
  • You are Welcome!
    "Knowledge Shared Is Knowledge Gained"
    Thursday, November 26, 2009 5:50 AM
  • Hi,

    This code below of form closing event ensures that the form gets closed only if user closes by clicking (X) button.

    Private Sub Myfrm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If e.CloseReason = CloseReason.UserClosing Then
             ' Do any operation if req
            Else
                e.Cancel = True
            End If
    End Sub

    Hope it helps.

    Thursday, November 26, 2009 11:47 AM