locked
Show new modal form RRS feed

  • Question

  • Hey, what is the difference between:

    Dim Blah As New BlahForm
    BlahForm.ShowDialog()

    And

    BlahForm.ShowDialog()

    I mean I need to show a new modal form, but to call it in one command/line, may I still use 2nd one?

    Saturday, September 26, 2020 10:22 PM

All replies

  • Creating a new instance of a form then use ShowDialog on a standard form, everything that needs to be initialized does while not use a new instance everything is not guaranteed to be initialized. 

    You should never use the second example and see no reason to use the second example.

    Either use the first example or the following.

    Using f As New Form1
        f.ShowDialog()
    End Using

     

    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, September 26, 2020 11:06 PM
  • You should never use the second example and see no reason to use the second example.

    Thanks dear Karen for helping out :)

    The problem's threading:

    If InvokeRequired Then
        Invoke(Sub() xxx)
    Else
        Dim Blah As New BlahForm
        Blah.ShowDialog()
    End If

    Didn't know how to do it:

    1st:
    Invoke(Sub() Dim Blah As New BlahForm)
    Invoke(Sub() Blah.ShowDialog())

    or 2nd:
    Dim Blah As New BlahForm
    Invoke(Sub() Blah.ShowDialog())

    or Else?

    Thanks Mam.

    Sunday, September 27, 2020 6:04 AM
  • First off since I do a lot of async work in C# I have a language extension shown below converted to VB.NET. The extension method is in a global class project that is not part of the current Visual Studio solution, added in via reference add, browse . . .

    Public Module ControlExtensions
        <Extension>
        Public Function InvokeIfRequired(Of T As Control)(control As T, action As Action(Of T)) As IAsyncResult
    
            If control.InvokeRequired Then
                Try
                    Return control.BeginInvoke(New Action(Of T, Action(Of T))(AddressOf InvokeIfRequired), New Object() {control, action})
                Catch ex As Exception
                    Return Nothing
                End Try
            Else
                action(control)
                Return Nothing
            End If
        End Function
    End Module

    Then to use it, in this case a ListBox but works with any control, never tried it with a form (until just now) but that should not make a difference.

    ListBox1.InvokeIfRequired(
        Sub(listBox)
            listBox.Items.Add($"{Now.ToString("hh:mm:ss")}")
            ListBox1.SelectedIndex = ListBox1.Items.Count - 1
        End Sub)

    With the form

    Dim Blah As New BlahForm
    Blah.InvokeIfRequired(
        Sub(blahForm)
            If blahForm.ShowDialog() = DialogResult.OK Then
                MessageBox.Show("OK")
            Else
                MessageBox.Show("Cancel")
            End If
        End Sub)


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, September 27, 2020 12:20 PM
  • Well that's a bit different, I found this on stackoverflow but does not trigger my form load events:

    Me.Invoke(New Action(Of System.Windows.Forms.IWin32Window)(AddressOf BlahForm.ShowDialog), Me)

    Don't know if it's the same as:

    Invoke(Sub() ShutDownForm.ShowDialog())

    The same when I use this:

    Dim Blah As New BlahForm
    Invoke(Sub() Blah.ShowDialog())

    My form load events won't fire! :(

    ---

    Currently only this works for me (I need to show new instance of modal form)

    Invoke(Sub() BlahForm.ShowDialog())


    • Edited by OSVBNET Sunday, September 27, 2020 2:43 PM
    Sunday, September 27, 2020 2:42 PM
  • Looks like you need to either look at my recommendations or buckle down with what you have found and figure it out as I have nothing else to offer as I have no more time beings I have a weekly blog entry to get done and out for the day shortly.

    Good luck :-)


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, September 27, 2020 3:07 PM