Call form from form

kaveh rahimi 66 Reputation points
2021-05-26T12:09:06.67+00:00

Hi ,I want to call a form from another form.I've done my code as below.when I connect the USB there is a notification :Plugged in.Then I ok it and after a short time the below error apeares:
A problem caused the program to stop working correctly. windows will close the program and notify if a solution is available.

Imports System.Management
Imports Microsoft.Win32
Imports System.Windows.Forms

Public Class Form1

Dim WithEvents pluggedInWatcher As ManagementEventWatcher
Dim WithEvents pluggedOutWatcher As ManagementEventWatcher
Dim pluggedInQuery As WqlEventQuery
Dim pluggedOutQuery As WqlEventQuery

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Try
        pluggedInQuery = New WqlEventQuery
        pluggedInQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"
        pluggedInWatcher = New ManagementEventWatcher(pluggedInQuery)
        pluggedInWatcher.Start()

        pluggedOutQuery = New WqlEventQuery
        pluggedOutQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 3"
        pluggedOutWatcher = New ManagementEventWatcher(pluggedOutQuery)
        pluggedOutWatcher.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub pluggedInWatcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles pluggedInWatcher.EventArrived
    MsgBox("Plugged In")
    Dim frm2 As New form2
    frm2.Show()

    'Me.Dispose(True)

End Sub


Private Sub pluggedOutWatcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles pluggedOutWatcher.EventArrived
        MsgBox("Plugged Out")
    End Sub
End Class
Module module1
Sub main()
    Console.WriteLine("ok")
End Sub
Private Declare Function ch341opendevice Lib "ch341dll.dll" (ByVal iindex As Integer) As Integer

End Module
Please help me solve the problem.
Thanks

Developer technologies VB
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-05-26T13:49:59.767+00:00

    To show a form (from another form or anything actually) you use either Show or ShowDialog depending upon whether you want a modeless or modal form.

    The problem in your case is unrelated to this I believe. I believe the issue is one of threading. It isn't clear from the docs but it is pretty obvious that the EventArrived event is raised on a worker thread. Therefore the event handler cannot access any UI elements because it isn't on the UI thread. So I suspect that your form2 is potentially doing something that assumes it is running on the UI thread and fails. The exception details would probably help clarify this.

    To work around this issue you should marshal the event back to the UI thread and do your work there. The defacto simple way of doing this is something like this in the handler instead. Warning: My VB is rusty.

       If InvokeRequired  
          Invoke(ShowForm2)  
       Else  
          ShowForm2  
       End If  
    

    ShowForm2 is what is currently inside your event handler. It will be running on the UI thread and can do whatever UI stuff it needs. But note that if the UI is busy doing other work then this will stall the event handler and may cause some issues.


  2. Dewayne Basnett 1,381 Reputation points
    2021-06-02T13:30:09.01+00:00

    Try it like this

        Private Sub pluggedInWatcher_EventArrived(sender As Object,
                                                  e As EventArrivedEventArgs) Handles pluggedInWatcher.EventArrived
            ' MsgBox("Plugged In")
            Dim frm2 As form2
            If Me.InvokeRequired Then
                Me.Invoke(Sub()
                              frm2 = New(form2)
                              frm2.Show()
                          End Sub)
            Else
                frm2 = New(form2)
                frm2.Show()
            End If
            'Me.Dispose(True)
        End Sub
    
    0 comments No comments

  3. Michael Taylor 60,161 Reputation points
    2021-06-05T02:10:54.79+00:00

    System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.Delegate'.'

    Like I said, my VB is rusty. The error is an issue getting VB to recognize the function. Here's a working example of the code.

    Private Sub btnLoadForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnloadform.Click
        If InvokeRequired Then
            Invoke(New ControlInvokeDelegate(AddressOf ShowForm2))
        Else
            ShowForm2()
        End If
    End Sub
    
    Delegate Sub ControlInvokeDelegate()
    
    Private Sub ShowForm2()
        MsgBox("Plugged In")
        Dim frm2 As New Form2
        frm2.Show()
    End Sub
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.