How do you open an already disposed form in VB? - vb.net

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

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

MDI Child form close on new open

How to close MDI Chid form when i want to open a new one.
On this way i open both of them but i want to close the previous when opening the new one.
Private Sub DostupniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DostupniToolStripMenuItem.Click
Dim frm As New FrmDostupniZaposlenici
frm.MdiParent = Me
frm.Show()
frm.WindowState = FormWindowState.Maximized
End Sub
Private Sub DodajToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DodajToolStripMenuItem.Click
Dim frm As New frmDodajZaposlenika
frm.MdiParent = Me
frm.Show()
frm.WindowState = FormWindowState.Maximized
End Sub
I have around 10 mdi child forms.
Edit :
New code. How to prevent to open form on form. Example i want to close all other mdi forms when new form is open.
On this way if i click on 4 buttons in toolstrip i got 4 forms opened. I don't want that. If i click on button 3 i want to close the previous form and load the form3.
Private Sub DostupniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DostupniToolStripMenuItem.Click
FrmDostupniZaposlenici.MdiParent = Me
FrmDostupniZaposlenici.Show()
FrmDostupniZaposlenici.WindowState = FormWindowState.Maximized
End Sub
Private Sub DodajToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DodajToolStripMenuItem.Click
frmDodajZaposlenika.MdiParent = Me
frmDodajZaposlenika.Show()
frmDodajZaposlenika.WindowState = FormWindowState.Maximized
End Sub
Private Sub IzmjeniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IzmjeniToolStripMenuItem.Click
frmIzmjenaZaposlenika.MdiParent = Me
frmIzmjenaZaposlenika.Show()
frmIzmjenaZaposlenika.WindowState = FormWindowState.Maximized
End Sub
Private Sub ObrisiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ObrisiToolStripMenuItem.Click
frmObrisiZaposlenika.MdiParent = Me
frmObrisiZaposlenika.Show()
frmObrisiZaposlenika.WindowState = FormWindowState.Maximized
End Sub
Simply loop through all the open MDI child forms and close them ...
For Each f As Form In Me.MdiChildren
f.Close()
Next
To close an opened child form first, enter the following codes in the click event of the menu item, just immediately after declaring the child form.
ActiveMdiChild.Close()

Changing the background color for Form2

i have two RadioButtons one for Light Blue and the other for Ghost White and a button to show the next Form (Form2)
i want to be able to check on a Radio Button and the backcolor of form2 changes to the checked Radio Button
this is what i have on my coding so far
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
Private Sub rbLightBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbLightBlue.CheckedChanged
If rbLightBlue.Checked Then
SecondForm.BackColor = (Color.LightBlue)
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGhostWhite.CheckedChanged
If rbGhostWhite.Checked Then
SecondForm.BackColor = (Color.GhostWhite)
End If
End Sub
The problem am having is making the background color change on Form2.
Any answer for this question will be very helpful.
I am not sure what you are doing, it probably has to do with how you are creating your SecondForm, this code does work, see if it helps you narrow it down.
Public Class Form1
Dim SecondForm As Form2 = New Form2
Private Sub rbLightBlue_CheckedChanged(sender As Object, e As EventArgs) Handles rbLightBlue.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.LightBlue
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(sender As Object, e As EventArgs) Handles rbGhostWhite.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.GhostWhite
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
End Class

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.

Values are not populating in the Form

Using VB.Net (Windows Application)
In the form (called as FirstForm), i am using textbox, add-form Button, search button.
When i click the add-form button, it will give the new form (same as FirstForm)
Code for adding new form
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim SecondForm As New FirstForm
SecondForm.Show()
End Sub
Search Button Code
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
If FirstForm.Focus = True Then
FirstForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
Else
Dim SecondForm As New FirstForm
SecondForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End If
End Sub
The above code is working, but If i am in second Form when i click the search button and selected the value, then the value is appearing in the FirstForm textbox, it is not appearing in the SecondForm textbox.
If SecondForm is showing, the selected Value should appear in the SecondForm textbox not in the FirstForm Textbox.
How to solve this issue.
Need Vb.net code Help
Use Me - Reference variable which hold ref. of current form.
Dim frm As FirstForm
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(frm) OrElse frm.IsDisposed Then
frm = New FirstForm
End If
frm.Show()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub
Using "me" will not solve the problem?? why are you referring to the form in a static way?
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub