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
Related
-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
As the title says, I can not get the logged user's information to form2. I'm using access database.
Here's my code:
Public Class Form1
Public id, pw, bal, nme As String
Private Sub CardBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles CardBindingNavigatorSaveItem.Click
Me.Validate()
Me.CardBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.KartDataSet)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
id = Card_IDTextBox.Text
pw = PasswordTextBox.Text
Dim login = CardTableAdapter.IDPW(id, pw)
If login Is Nothing Then
MsgBox("Check ID or PW")
Else
MsgBox("Welcome, now you are connecting.")
Form2.Show()
bal = Form2.BalanceTextBox.Text
nme = Form2.UserTextBox.Text
End If
End Sub
End Class
What am I doing wrong?
If the code in your comment ..
id = Form1.Card_IDTextBox.Text
pw = Form1.PasswordTextBox.Text
bal = Me.BalanceTextBox.Text
nme = Me.UserTextBox.Text
is in your form2.Load handler, when you call
Form2.Show()
the code above will never run. You need to move it into your Form2.Shown event. Like this ..
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
id = Form1.Card_IDTextBox.Text
pw = Form1.PasswordTextBox.Text
bal = Me.BalanceTextBox.Text
nme = Me.UserTextBox.Text
End Sub
The .Load event will only trigger when an instance of the Form2 class is created. Visual Studio create code behind the scenes that automatically creates a default instance of Form2 when the program is started. Of course at that point, your textboxes etc have no values in them, so any code like yours above will be useless in the Form2.Load handler :-)
i want to transfer values from one form to another...
This is the code for the MainPage
Public Property StringPass As String
Public Property NumPass As String
Private Sub MainPage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label7.Text = StringPass
TextBox3.Text = NumPass
End Sub
This is the code for the SecondPage
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OBJ As New MainPage
Dim NUM As New MainPage
OBJ.StringPass = TextBox1.Text
NUM.NumPass = TextBox6.Text
OBJ.Show()
NUM.Show()
Me.Hide()
End Sub
How can OBJ and NUM pass values to their respective textboxes but open in the same MainPage?
You are creating 2 forms and you only need one.
If you want to pass data to the next form you can assign those values just like you would with a normal class.
Make sure you have saved all forms as well.
Your naming convention is not the best here just FYI.
Here is an example with your code.
This is the code for the SecondPage
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OBJ As New MainPage
OBJ.Label7.Text = TextBox1.Text
OBJ.TextBox3.Text = TextBox6.Text
OBJ.Show()
Me.Hide()
End Sub
I want to use a array to help me switch between forms in a quiz that i have to create for a school assignment. When a question is correct it shows the correct form. I am using one button to go to the next form. There are 20 questions each with its own form. This is what i need:
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Me.Hide()
arrayforms(count).Show()
Thanks
First Collect all of your questionary forms into Array. Name your questionary forms as "Questionary1", "Questionary2" something like that, or set tag to you forms. Then find your form by index and create an instance and ShowDialog, that's it. Try following code.
Public Class Form1
Private forms(20) As Type
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourFormIndex = 3
Dim frm As Form = Activator.CreateInstance(forms(yourFormIndex))
frm.ShowDialog()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
Dim index As Integer = 1
For Each myType As Object In types
If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
If (myType.Name.ToString.StartsWith("Questionary")) Then
forms(index) = myType
index = index + 1
End If
End If
Next
End Sub
End Class
I have the problem to retreive the string from one form to another. here is my code: What's wrong with this?
Public Class Form3
Dim unit As String
Public itmname As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj1 As New Form4
itmname = tb1.Text
MessageBox.Show(itmname)
obj1.Label1.Text = itmname
obj1.Show()
End Sub
End Class
Public Class Form4
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Form3
MessageBox.Show("item name:" + .itmname)
Label1.Text = .itmname
End With
End Sub
End Class
You shouldn't have to do any special code in Form4 to set the value. you already set the textbox value from from3's button click event. Setting again is just overwriting it with a blank value from a newly instantiated form. Just clear all the code you have listed in Form4's load event and it should work.