vb.net passing values out of forms - vb.net

I have a very simple windows forms setup. Form1 has a progress bar and a button on it, when clicked the button opens Form2 which also has a button on it that launches Form3. On Form3 is a button which I want to use to raise an event back to Form1.
To achieve this can I add an event handler on form1 that will listen for an event of the type raised in form3? Or do I have to pass references to form1 to form2 and then from form2 to form3?
Any advice on the best way to achieve this is greatly appreciated.
Many thanks

You'll want to add an event handler on each form that will "bubble-up" the event being thrown on the third form.
Public Class Form1
Private WithEvents form2 As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
form2.Show()
End Sub
Private Sub Form2_MyEvent() Handles form2.MyEvent
MessageBox.Show("We're back on Form1.")
End Sub
End Class
Public Class Form2
Private WithEvents form3 As New Form3
Public Event MyEvent()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
form3.Show()
End Sub
Private Sub Form3_MyEvent() Handles form3.MyEvent
RaiseEvent MyEvent()
End Sub
End Class
Public Class Form3
Public Event MyEvent()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent MyEvent()
End Sub
End Class

Related

send fist form1 textbox from second opened form2

i tried some codes but i didnt worked my want
how can i send first form's textbox from form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim F As New Form2
F.Show()
End Sub
second forms code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FRM As New Form1
FRM.Datagridview1.Rows.Add()
FRM.Datagridview1.rows(0).cells(0).value=1
End Sub
i press button1 but nothing happens
Actually, in the second form code, you create a new form1.
In vb.net you can try this in your second form : (without creating a new form1)
[first_form_name].Datagridview1.rows(0).cells(0).value=1

Error on form click object reference not set to an instance of an object

When I click form1 button 1 to show form2 but it show The error is:
Object reference not set to an instance of an object.
Form1:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2. Show()
Me.Hide()
End Sub
End Class
I suggest declaring it outside of the method, this way you can access it from other methods:
Private _Form2 as new Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me._Form2.Show()
Me.Hide()
End Sub
Try this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newform as New Form2
newform.Show()
Me.Hide()
End Sub
You can have more than one of the same form open, so you need to create an 'instance' of the form, before you tell it to show that instance.

Reacting to third-party application form events using vb.net

I would like to know how I should code my VB.net application to react to the form load event in a third-party application (also written in VB.net)
To test, I have created two basic programs, one with two forms (program A) and one (program B) that attempts to the listen to program A's appropriate form load event. I have tried using WithEvents but it does not get fired when Program's A second form loads.
Here is the code for Program A:
Public Class StartPage
Public WithEvents loadtimer As New System.Windows.Forms.Timer
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
loadtimer.Interval = 1000
loadtimer.Enabled = True
loadtimer.Start()
End Sub
Private Sub loadtimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles loadtimer.Tick
loadtimer.Stop()
SystemStatus.Show()
End Sub
End Class
Public Class SystemStatus
Inherits StartPage
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "This is the form that I want to listen for the load event"
Me.loadtimer.Enabled = False
End Sub
End Class
Here is the code for Program B:
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New Program_A.SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
End Sub
End Class
I am quite new when it comes to programming so maybe this is not even possible or I just haven't been understanding what I've been reading. In any case please enlighten me as to what my next course of action should be.
Thanks very much in advance,
theoleric
This will do what your asking.
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
' now this event will fire
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
' the load event will not fire until you call this
testlisten.Show()
End Sub
End Class

Event handling in VB.NET

In VB.NET, why isn't the following custom event not firing?
Public Class classes1
Public Event buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs)
Protected Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
attd.Show()
RaiseEvent buttonload(sender, e)
End Sub
End Class
Public Class attd
Dim WithEvents c1 As New classes1
Sub c1_buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles c1.buttonload
MsgBox("Event received")
End Sub
End Class
You are using your first form, classes1, to show your second form, attd, and then raise your CustomEvent. Then in your second form, attd, you are creating another instance of the first form, classes1, and then trying to attach your handler to that instance's event. They are not the same, so it will not fire.
It is really not clear exactly what you are trying to do. If you are just experimenting around with events you can try something like this.
Form1
Public Class Form1
Dim attd As Form2 = New Form2
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
attd.Show()
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddHandler attd.buttonload, AddressOf buttonLoadHandler
End Sub
Private Sub buttonLoadHandler(sender As Object, e As EventArgs)
MsgBox("Event received")
End Sub
End Class
Form2
Public Class Form2
Public Event buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent buttonload(sender, e)
End Sub
End Class
If you are just wanting to have your second Form respond to the First Forms Button Click try something like this.
Form1
Public Class Form1
Dim attd As Form2 = New Form2
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
attd.Show()
attd.showMessageBox()
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Form2
Public Class Form2
Public Sub showMessageBox()
MsgBox("Hello World")
End Sub
End Class

Open the same form more than once

Is it possible to open a form more than once?
button1
form2.show
Press button1
form2 opens up
press button1 again
another form2 opens up next to the old form2
If possible, can a button on Form1 kill all Form2 windows open?
Of course it's possible. Just dim two instances of the same form.
Public Class Form1
Private m_WindowList As New List(Of Form2)
Private Sub OpenWindowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenWindowButton.Click
OpenWindow()
End Sub
Private Sub CloseWindowsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseWindowsButton.Click
CloseWindows()
End Sub
Private Sub OpenWindowsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenWindowsButton.Click
Dim WindowCount As Int32
If Int32.TryParse(WindowCountTextBox.Text, WindowCount) Then
OpenWindows(WindowCount)
End If
End Sub
Private Sub OpenWindow()
Dim NewWindow As New Form2
m_WindowList.Add(NewWindow)
NewWindow.Show()
End Sub
Private Sub OpenWindows(ByVal Count As Int32)
For i = 1 To Count
OpenWindow()
Next
End Sub
Private Sub CloseWindows()
For Each Window In m_WindowList
Window.Close()
Window.Dispose()
Next
m_WindowList.Clear()
End Sub
End Class
Dim MyNewForm2 = New Form2
MyNewForm2.Show