Error on form click object reference not set to an instance of an object - vb.net

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.

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

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

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

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

Object reference not set to an instance, public array with..Split(",")

i wonder what is wrong with the following vb.net code.
Public Class Form10
Public IDs() As String = TextBox1.Text.Split(",")
Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each id In IDs
MsgBox(id)
Next
End Sub
End Class
when i do
Form10.show()
i get an error "Object reference not set to an instance"
You have declared a field in your class to be initialized with a value from a control on the corresponding form that does not yet exist. The control is not initialized and loaded by the time your initializer on your field member is accessed, thus causing the error.
To keep the public IDs declaration, you could remove the initialization from the field declaration, then move the assignment to the Button1_Click event, as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
IDs=TextBox1.Text.Split(",")
' Do something interesting with IDs now...
End Sub
Public Class Form10
Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim IDs() As String = TextBox1.Text.Split(",")
Form1.somefunction(IDs)
End Sub
and in Form1
Public Sub somefunction(ByVal IDs() As String)
For Each id In IDs
MsgBox(id)
Next
End Sub