VB .Net Pass value from loaded form - vb.net

im currently made project with VB VS2012
i have 2 form,
form1 contains textbox and button, this button is to open form2.
in form2, i have 1 button. i want to set form1.textbot value when form2 button is clicked then close the form2.
form1 button to call form2
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog(Me)
End Sub
this is button form2
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Dim x As New form1
x.textbox.Text = "tes"
End Sub
i've tried ctype, directcast but its not working. by the way, this form1 is docked to a mainform. i tried to do the same but without form docked, it works, but when it docked, its not working.

Try if you are using default instances.
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Form1.textbox.Text = "tes"
Close()
End Sub
Did you really name the text box textbox? Descriptive names can be helpful.

What you're currently doing is creating a new instance of form1 with the line Dim x As New form1 - and that means you're not updating the original form1.
Since you are passing a reference to form1 to the Form2 dialog already, i.e. .ShowDialog(Me) then this would work:
'form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog(Me)
End Sub
'Form2
Public Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
CType(Me.Owner, form1).textbox.Text = "tes"
End Sub

i've found my solution
here's code on form 1
Private t1 As String = String.Empty
Private t2As String = String.Empty
Dim f As New form1()
If f.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
t1 = f.t1.ToString
t2 = f.t2.ToString
textbox.Text = t1
textbox2.Text = t2
f.Close()
End If
and here's on form2
Public t1 As String
Public t2 As String
Me.DialogResult = Windows.Forms.DialogResult.OK
If Me.DialogResult = Windows.Forms.DialogResult.OK Then
t1 = dgdriver.SelectedRows(0).Cells(1).Value.ToString
t2 = dgdriver.SelectedRows(0).Cells(0).Value.ToString
Me.Close()
End If

Related

Get values of one form into another form in Visual Basic

I am trying to get an integer value of one form (Form1) inside another form (Form2). I have tried to access it via the below code but not getting it. Can someone please tell me what I am doing wrong.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FinalPoints As New Form1
Label1.Text = FinalPoints.Label1.Text
End Sub
End Class
The problem: As you are showing your Form2 (Form2_Load is executed), a new Form1 is created. This newly created Form1 has NOT executed the Form1_Load function yet!
You would need to show the newly created FinalPoints (Form1) with FinalPoints.Show() like that:
Dim FinalPoints As New Form1
FinalPoints.Show()
Label1.Text = FinalPoints.Label1.Text
to let the Form1_Load function execute, which is then setting your FinalPoints.Label1.Text. But that would just opens a new Form1.
Also you can just get the public Points variable inside the Form2_Load like that (you also do not have to create a new Form1):
Label1.Text = Form1.Points
Alternatively: Just use a public variable inside Form2 and assign your value to it, before you show the form.
Public Class Form1
Public Points As Integer = 100
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Points.ToString
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FinalPoints As New Form2
FinalPoints.StringFromForm1 = Label1.Text
FinalPoints.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Public Property StringFromForm1 As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = StringFromForm1
End Sub
End Class
This line: Dim FinalPoints As New Form1 creates a new instance of Form1, but what you want is to refer to an existing instance of Form1. There are different techniques you can try. For example, overload the Show method of Form2.
Something like this:
Form1: pass the value to Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
Me.Hide()
f.Show(Points)
End Sub
Form2: fetch the value from caller (Form1)
Public Class Form2
Public Overloads Sub Show(ByVal Points As Integer)
Me.Label1.Text = Points.ToString
MyBase.Show()
End Sub
End Class

VB.net WindowsForm changing then [System.Data.MissingPrimaryKeyException]

-I created 2 forms after I use hide() and show() to switch between form.
-In the 2nd form, I create datatable (bond to datagrid) and primary key for datatable to use find().
-After I press [BACK] button form 2nd form to 1st form (use hide() and show() function) and come back to use 2nd form application again. the runtime error show this [System.Data.Missing.PrimaryKeyException: Table doesn't have a primary key]
1st Form
Public Class MainMenu
Public MainForm As MainMenu
Public AssetCheckForm As AssetCheck
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AssetCheckForm.Show()
MainForm.Hide()
End Sub
Public Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MainForm = New MainMenu()
AssetCheckForm = New AssetCheck()
End Sub End Class
2nd Form
Dim dtAsset As New DataTable("AssetTable")
Public Sub readData()
If readStatus = 0 Then
Try
Dim splits As String()
Using sr As StreamReader = New StreamReader(inputcsvname)
'read the first line for the table columns
splits = sr.ReadLine.Split(","c)
For i As Integer = 0 To UBound(splits)
dtAsset.Columns.Add(splits(i))
Next
'read the rest of the lines to add rows
Do While Not sr.EndOfStream
splits = sr.ReadLine.Split(","c)
dtAsset.Rows.Add(splits)
Loop
End Using
Catch ex As Exception
Finally
End Try
dtAsset_display = dtAsset.Copy()
totalcount.Text = getRowsCount(dtAsset_display)
dtAsset.Columns("AsstCode").Unique = True
dtAsset.PrimaryKey = New DataColumn() {dtAsset.Columns("AsstCode")}
'bind display part to DataGrid
DataGrid1.DataSource = dtAsset
Private Sub BackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonF4.Click
clearData()
MainMenu.MainForm.Show()
MainMenu.AssetCheckForm.Hide()
End Sub
I think I missed something about VB.net WinForm concept.
Does anyone know what this error means?
The fact that you are calling MainForm = New MainMenu() in MainForm it means you now have two instances of MainForm.
So then when you call MainMenu.MainForm.Show() you are showing your new instance, not the existing one.
You need to change this around so that you store the reference to the existing MainForm in your AssetCheckForm form.
Something like this:
Public Class MainMenu
Private AssetCheckForm As AssetCheckForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.AssetCheckForm = New AssetCheckForm
Me.AssetCheckForm.MainMenu = Me
Me.Hide()
Me.AssetCheckForm.Show()
End Sub
End Class
Public Class AssetCheckForm
Friend MainMenu As MainMenu
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.MainMenu.Show()
Me.Close()
End Sub
End Class

How to load a form again without exiting the application?

I have 2 form. Form1 and Form2. In form1 there's a button that will go to form2 if it is click. If I click that button if form1, form2 will load and integer a will become 1. If I click the button in form2, integer a will become 0 and it will back to form1. Since im in form1, if I click again the button in form1 it will go to form2 but form2 will not load again. Is theres a way to load again the form? Heres my example:
Form1:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
End Class
Form2:
Public Class Form2
Dim a As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
a = 1
MsgBox("load complete!!")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = 0
Me.Hide()
Form1.Show()
End Sub
End Class
Thanks in advance!
Form2's Load method only gets called once because that is the normal life cycle for a form. The load event only gets called once before the form is loaded for the first time.
From Form.Load Event:
Occurs before a form is displayed for the first time.
The solution depends on your needs. If you need to keep the state of the form even when it is hidden then you want to use the VisibleChanged event.
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Me.Visible Then
MsgBox("Visible changed")
End If
End Sub
If you don't need to keep the state then you can discard Form 2 and recreate it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Dim Form2 = New Form2()
Form2.Show()
End Sub
Dim a As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
a = 1
MsgBox("load complete!!")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = 0
Form1.Show()
Me.Close()
End Sub
Just set a new instance of Form2 before you load it.
Dim Form2 As New Form2
Form2 .Show()

How to pass Data from Parent Form to Child Form

I have 2 forms, Form1 is a parent form and Form2 is a child form. Both of them are set to show at the same time.....Form1's mid-container is set to true and has a button, Form2 has a text-box...I want it that if I press the button in Form1 something will appear in the text-box in Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
IsMdiContainer = True
Dim inv As New Form2
inv.MdiParent = Me
inv.Show()
inv.Location = New Point(15, 15)
End Sub
I tried clicking the button but nothing happened, I also tried the other way around...putting a button in Form2 and a text-box in Form1 and it works...
you will have to move the inv variable out of the form_load scope
Public Class Form1
Private inv As New Form2 'here
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
IsMdiContainer = True
inv.MdiParent = Me
inv.Show()
inv.Location = New Point(15, 15)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
inv.TextBox1.Text = "Hello World"
End Sub
End Class
Say you have a TextBox1 control on Form2 and a button on Form1 and upon clicking button on Form1, "Hello World" will appear on TextBox1 on Form2, just do it like this...
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.TextBox1.Text="Hello World"
End Sub

vb.net textbox not displaing value

In my frmMain class I have a textbox(txtCustomer) which populates from a database. I want to pass this value to another textbox in frmDepartment(txtDeptCustomer).
I am failing to see the logic of why the code I am using is not displaying a value in txtDeptCustomer. I can query the database ok with the variable, so the string is being passed through, but just not displaying in txtDeptCustomer. I would be grateful if someone could point out my error. Thanks
frmDepartment
Dim customer As Object = frmMain.txtCustomer.Text
This is passing correct value to db.
sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"
textbox txtDeptCustomer <--- NOT DISPLAYING VALUE
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
txtDeptCustomer.Text = CType(customer, String)
End Sub
Public Customer as String = Nothing
Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
Customer = Database Call
Dim frmDepartmentInstance as new frmDepartment
frmDepartment.ShowDialog(Me)
End Sub
Then in the Load event of frmDepartment you can say
txtDeptCustomer.Text = frmMain.Customer
Proof of concept: New Project. Two forms | Form 1 has a button and a textbox | Form2 just has textbox
Public Class Form1
Public Test As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Test = TextBox1.Text
Dim frm2 As New Form2
frm2.ShowDialog(Me)
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Form1.Test
End Sub
End Class
You must declare then as public your customer variable in frmDepartment, like:
Public customer as String
And in the button click from your frmMain you pass the value like:
frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()
And then in Loading your frmDepartment you have now the option of assigning customer to txtDeptCustomer like:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDepartment.Text = customer
End Sub