Performing actions on form1 from form2

67 views Asked by At

I have this sub on form1:

  Public Sub PlayMaskVideo(filePath As String)
      If File.Exists(filePath) Then
          If MaterialTabControl2.SelectedIndex = 0 Then Mask0Img.Visible = False

          If libVLC Is Nothing Then
              Core.Initialize()
              libVLC = New LibVLC()
          End If

          mediaPlayer = New MediaPlayer(libVLC)
          Mask0VLC.MediaPlayer = mediaPlayer

          media = New Media(libVLC, filePath, FromType.FromPath)
          media.AddOption("input-repeat=65535")
          mediaPlayer.Volume = 0
          mediaPlayer.Play(media)
      End If
  End Sub

This code works as expected if called on Form1, it plays a video in the vlcsharp player on the form.

What I'm trying to do is call it from form2 like this:

Form1.PlayMaskVideo(outputPreview)

I'm expecting the video to play in the form1 video player however nothing happens. I thought maybe its an issue with vlcsharp, but even this line doesn't execute:

If MaterialTabControl2.SelectedIndex = 0 Then Mask0Img.Visible = False

Am I missing something?

1

There are 1 answers

2
tnils25 On

Here's an example of how I accomplished the issue I was having. Instead of using form1.something = value I used an eventhandler. Like this:

' Form1.vb
Public Class Form1
    ' Declare an instance of Form2
    Private WithEvents form2 As Form2

    Private Sub btnOpenForm2_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
        ' Create an instance of Form2
        form2 = New Form2()

        ' Show Form2 as a dialog
        form2.ShowDialog()
    End Sub

    ' Event handler for the event raised in Form2
    Private Sub Form2ValueEntered(value As String) Handles form2.ValueEntered
        ' Update the text on Form1 with the value entered in Form2
        lblForm1.Text = "Value entered in Form2: " & value
    End Sub
End Class

Then on Form 2:

    ' Form2.vb
Public Class Form2
    ' Define an event that will be raised when a value is entered
    Public Event ValueEntered As Action(Of String)

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        ' Get the value entered by the user
        Dim enteredValue As String = txtValue.Text

        ' Raise the event and pass the entered value
        RaiseEvent ValueEntered(enteredValue)

        ' Close Form2
        Me.Close()
    End Sub
End Class

So when you enter a value on Form2 and click Submit, the value in the textbox will be sent to the Form1 label.