locked
Me.close and Me.Dispose when using Form.showdialog RRS feed

  • Question

  • Hi Everyone ,

    After i have searched on the web about Form.showdialog because i faced some problem with it ,

    I found an article that said 

    "Me.close is not enough to completely close the form so error occurs when try to open the form again with showdialog method and you must use Me.Dispose" really this article is so helpful it enables me to deal with showdialog method by 

    closing and disposing the form after i open it ..... But i think that is not reasonable .... I tried to put Me.Dispose in the formclosing event but i found that the form ignores it if i leave the form by any button(i.e go to another form by clicking a button) it works only when i use the 'X' button of the form and i'm making it disappear .... My technique now is 

       Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            'Me.Close()
            'Me.Dispose()
            ProductDetailsForm7.Close()
            ProductDetailsForm7.Dispose()
            ProductDetailsForm7.ShowDialog()
    
    End Sub
    

    i think that there is a simple method to deal with Showdialog method ?

    Or my technique is right ?

    Thanks in advance ............

     

    Regards From Amr_Aly

    Friday, November 16, 2018 5:15 PM

Answers

  • I think the answer is, no, that isn't how it is used.  When you use the .ShowDialog method the statement following the .ShowDialog will not be executed UNTIL the form is closed.

    When a Form is no longer needed call it's .Dispose method.  I am of the opinion that all objects should call .Dispose, if they have one, when no longer needed.

    edit:  with Using

            Dim dr As DialogResult
            Using ProdDetlsFrm7 As New ProductDetailsForm 'Using disposes of ProdDetlsFrm7 automatically
    
                dr = ProdDetlsFrm7.ShowDialog
                'work with result if needed...
                If dr = Windows.Forms.DialogResult.OK Then 'to get HERE ProdDetlsFrm7 had to have been closed
                ElseIf dr = Windows.Forms.DialogResult.Cancel Then
                End If
            End Using
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.



    • Edited by dbasnett Friday, November 16, 2018 6:59 PM
    • Marked as answer by Amr_Aly Friday, November 16, 2018 9:17 PM
    Friday, November 16, 2018 6:24 PM

All replies

  • Here is a pattern for showing another form as a Dialog

            'create an instance of ProductDetailsForm
            Dim ProdDetlsFrm7 As New ProductDetailsForm
            Dim dr As DialogResult
    
            dr = ProdDetlsFrm7.ShowDialog
            'to get here ProdDetlsFrm7 had to have been closed
    
            'work with result if needed...
            If dr = Windows.Forms.DialogResult.OK Then
            ElseIf dr = Windows.Forms.DialogResult.Cancel Then
            End If
    
            '... LASTLY
            ProdDetlsFrm7.Dispose()
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Friday, November 16, 2018 5:35 PM
  • Hi

    It is not clear what your question is.

    Here  is some code that may be of use but somehow I think I have the wrong idea as to what you want. Form1 only has Button1 and Form2 only has Label1 Each time Form2 is opened, it shows an incremented value in the Label1

    Images (Form1 and Form2)

    Form1 code (Form2 has no code)

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim f2 As New Form2
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static a As Integer = 1
        f2.Label1.Text = a.ToString
        f2.ShowDialog()
        a += 1
      End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Friday, November 16, 2018 5:49 PM
    Friday, November 16, 2018 5:48 PM
  • Thanks for you fast reply,

    I used your snippet code like this 

    Dim f7 As New ProductDetailsForm7 Dim dr As DialogResult dr = f7.ShowDialog If dr = DialogResult.OK Then f7.ShowDialog() ElseIf dr = DialogResult.Cancel Then f7.Close() End If f7.Dispose()

    '''''''''''''''''

    Dim dr As DialogResult
            dr = ProductDetailsForm7.ShowDialog
            If dr = DialogResult.OK Then
                ProductDetailsForm7.ShowDialog()
            ElseIf dr = DialogResult.Cancel Then
                ProductDetailsForm7.Close()
            End If
            ProductDetailsForm7.Dispose()

    Is that true ?

    I tried it with a new instance and without new instance, it works well in the two cases.

    Any differences between the two cases ?

    I think that is my strategy is true i think no difference between your snippet and mine,

    i'm just asking .... 

    thanks for helping me absorbing VB.Net well 

      

    Regards From Amr_Aly

    Friday, November 16, 2018 6:01 PM
  • Hi

    It is not clear what your question is.

    Here  is some code that may be of use but somehow I think I have the wrong idea as to what you want. Form1 only has Button1 and Form2 only has Label1 Each time Form2 is opened, it shows an incremented value in the Label1

    Images (Form1 and Form2)

    Form1 code (Form2 has no code)

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim f2 As New Form2
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static a As Integer = 1
        f2.Label1.Text = a.ToString
        f2.ShowDialog()
        a += 1
      End Sub
    End Class


    Regards Les, Livingston, Scotland



    Thanks for replying but you misunderstand me ..........  

    Mr dbasnet gave me what i'm searching for

    and i think that he understood me well 

    appreciate your post 


    Regards From Amr_Aly


    • Edited by Amr_Aly Friday, November 16, 2018 6:24 PM
    Friday, November 16, 2018 6:16 PM
  • I think the answer is, no, that isn't how it is used.  When you use the .ShowDialog method the statement following the .ShowDialog will not be executed UNTIL the form is closed.

    When a Form is no longer needed call it's .Dispose method.  I am of the opinion that all objects should call .Dispose, if they have one, when no longer needed.

    edit:  with Using

            Dim dr As DialogResult
            Using ProdDetlsFrm7 As New ProductDetailsForm 'Using disposes of ProdDetlsFrm7 automatically
    
                dr = ProdDetlsFrm7.ShowDialog
                'work with result if needed...
                If dr = Windows.Forms.DialogResult.OK Then 'to get HERE ProdDetlsFrm7 had to have been closed
                ElseIf dr = Windows.Forms.DialogResult.Cancel Then
                End If
            End Using
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.



    • Edited by dbasnett Friday, November 16, 2018 6:59 PM
    • Marked as answer by Amr_Aly Friday, November 16, 2018 9:17 PM
    Friday, November 16, 2018 6:24 PM
  • Actually since it sounds like you are after a DialogResult of Ok or not Ok you can use this pattern which disposes of the form via the Using statement

    Using f As New ProductDetailsForm7
        If f.ShowDialog = DialogResult.OK Then
            ' OK selected
        Else
            ' Not Ok
        End If
    End Using

    Or done this way via Try/Finally.

    Dim f As New ProductDetailsForm7
    Try
        If f.ShowDialog = DialogResult.OK Then
            ' OK selected
        Else
            ' Not Ok
        End If
    Finally
        f.Dispose()
    End Try
    Both perform the same.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, November 16, 2018 6:51 PM
  • I think the answer is, no, that isn't how it is used.  When you use the .ShowDialog method the statement following the .ShowDialog will not be executed UNTIL the form is closed.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    I'm sorry sir , i have an inquiry your code works well ,and also my code which is better and more fast ..................... Thanks for solving my issue

     

    Regards From Amr_Aly

    Friday, November 16, 2018 6:55 PM
  • Actually since it sounds like you are after a DialogResult of Ok or not Ok you can use this pattern which disposes of the form via the Using statement

    Using f As New ProductDetailsForm7
        If f.ShowDialog = DialogResult.OK Then
            ' OK selected
        Else
            ' Not Ok
        End If
    End Using

    Or done this way via Try/Finally.

    Dim f As New ProductDetailsForm7
    Try
        If f.ShowDialog = DialogResult.OK Then
            ' OK selected
        Else
            ' Not Ok
        End If
    Finally
        f.Dispose()
    End Try
    Both perform the same.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    I was just coming back to point at Using...

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Friday, November 16, 2018 6:58 PM
  • I think the answer is, no, that isn't how it is used.  When you use the .ShowDialog method the statement following the .ShowDialog will not be executed UNTIL the form is closed.

    When a Form is no longer needed call it's .Dispose method.  I am of the opinion that all objects should call .Dispose, if they have one, when no longer needed.

    edit:  with Using

            Dim dr As DialogResult
            Using ProdDetlsFrm7 As New ProductDetailsForm 'Using disposes of ProdDetlsFrm7 automatically
    
                dr = ProdDetlsFrm7.ShowDialog
                'work with result if needed...
                If dr = Windows.Forms.DialogResult.OK Then 'to get HERE ProdDetlsFrm7 had to have been closed
                ElseIf dr = Windows.Forms.DialogResult.Cancel Then
                End If
            End Using


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.



    Thanks sir,

    I prefer to use Using statement it's more comfortable 

    The right code shape is like this

    Using f2 As New StockForm2
                If f2.ShowDialog = DialogResult.OK Then
                    f2.ShowDialog()
                Else
                    f2.Close()
                End If
            End Using
    Many thanks ........ it works well now and i got a new information  
     


    Regards From Amr_Aly

    Friday, November 16, 2018 8:08 PM
  • Actually since it sounds like you are after a DialogResult of Ok or not Ok you can use this pattern which disposes of the form via the Using statement

    Using f As New ProductDetailsForm7
        If f.ShowDialog = DialogResult.OK Then
            ' OK selected
        Else
            ' Not Ok
        End If
    End Using

    Or done this way via Try/Finally.

    Dim f As New ProductDetailsForm7
    Try
        If f.ShowDialog = DialogResult.OK Then
            ' OK selected
        Else
            ' Not Ok
        End If
    Finally
        f.Dispose()
    End Try
    Both perform the same.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thanks Karen ,

    your post is so helpful i appreciate that

    many thanks for all


    Regards From Amr_Aly

    Friday, November 16, 2018 8:51 PM