Open the same form more than once - vb.net

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

Related

How do you open an already disposed form in VB?

When I close Form2 it won’t open up again after.
I am using a button click to open up another form. Right now I just have:
Dim Form2 As New Form2
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
Form2.Show()
End Sub
Now whenever I close the form I cannot open it again.
Like this
Private MyForm2 As Form2
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
If MyForm2 Is Nothing OrElse MyForm2.IsDisposed Then
MyForm2 = New Form2
End If
MyForm2.Show()
End Sub

Open different forms on different button click in VB.NET

A noob query I have, is there any way to use a single command to open different forms on different button click events. I have 24 buttons in one form and will use these buttons to open 24 different forms.
So instead of doing it for 24 times as:
Private Sub BtnCh1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh1.Click
FormCh1.Show()
End Sub
Private Sub BtnCh2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh2.Click
FormCh2.Show()
End Sub
Private Sub BtnCh3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh3.Click
FormCh3.Show()
End Sub
Private Sub BtnCh4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh4.Click
FormCh4.Show()
End Sub
Can it be done with a single command?
In your form's load event add the forms in a List(Of Form)
Private list As List(Of Form)
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
list = New List(Of Form)
list.Add(New Form1())
'
'
'
list.Add(New Form24())
End Sub
Set your button's Tag property with the form's index and set them all to use the same click event:
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
list(CType(sender, Button).Tag).Show()
End Sub
Attach all the handlers to your method and then branch behaviour based on the Select Case:
Private Sub Button_Click_Handler(sender As Object, e As EventArgs) Handles Button66.Click, Button67.Click, Button68.Click
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case Button66.Name
Dim f1 As New Form1
f1.Show()
Case Button67.Name
Dim f2 As New Form2
f2.Show()
Case Button68.Name
Dim f3 As New Form3
f3.Show()
End Select
End Sub

How to interact with Listbox from Form2 throught thread from Form1

I tried to add items to Listbox in Form2 but noting can't be added, when I put listbox in same form where is thread it works good...Could someone help to make it work with Form2? Here is code:
Public Class Form1
Dim testthread As Threading.Thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Control.CheckForIllegalCrossThreadCalls = False
testthread = New Threading.Thread(AddressOf testira)
testthread.Start()
End Sub
Sub testira()
Form2.ListBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
End Class
Here's an example...
Public Class Form1
Private f2 As New Form2
Private Delegate Sub AddItemDelegate(ByVal item As String)
Private Delegate Function GetTextboxTextDelegate(ByVal TB As TextBox) As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testthread As New Threading.Thread(AddressOf testira)
testthread.Start()
End Sub
Sub testira()
Dim item As String = GetTextboxText(TextBox1)
AddItem(item)
End Sub
Private Function GetTextboxText(ByVal TB As TextBox) As String
If TB.InvokeRequired Then
Return TB.Invoke(New GetTextboxTextDelegate(AddressOf GetTextboxText), New Object() {TB})
Else
Return TB.Text
End If
End Function
Private Sub AddItem(ByVal item As String)
If Me.InvokeRequired Then
Me.Invoke(New AddItemDelegate(AddressOf AddItem), New Object() {item})
Else
If IsNothing(f2) OrElse f2.IsDisposed Then
f2 = New Form2
End If
f2.ListBox1.Items.Add(item)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If IsNothing(f2) OrElse f2.IsDisposed Then
f2 = New Form2
End If
f2.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

vb.net passing values out of forms

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