I can't retrieve logged user's information to form2 - vb.net

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 :-)

Related

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

VB .Net Pass value from loaded form

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

result of a modal form in vb.net

I create a form 'frmX' and i call it as a modal form :
res = frmX.ShowDialog()
This form has 3 buttons, Abort(3), Retry(4) and Ignore(5), but when the form opens, all the buttons on the first click return 2.
I don't know why this occurs--all of the buttons has their property DialogResult right.
*Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
btnIgnorar.DialogResult = DialogResult.Ignore
End Sub
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
btnAbortar.DialogResult = DialogResult.Abort
End Sub
Private Sub btnReintentar_Click(sender As Object, e As EventArgs) Handles btnReintentar.Click
btnReintentar.DialogResult = DialogResult.Retry
End Sub*
Can someone help me?
Could do with seeing a bit more context, but the following should do what I think you want:
Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
DialogResult = DialogResult.Ignore
Close
End Sub
This will close the dialog and return the associated result code to the caller. As to the original code, it seems a bit strange setting the values in the buttons click handlers?
The error comes from the fact that you set the DialogResult of the buttons. You must set the DialogResult of the form !
You actually have more than one option.
Option 1 : Set the Form.DialogResult
Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
Me.DialogResult = DialogResult.Ignore
End Sub
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
Me.DialogResult = DialogResult.Abort
End Sub
Private Sub btnReintentar_Click(sender As Object, e As EventArgs) Handles btnReintentar.Click
Me.DialogResult = DialogResult.Retry
End Sub
Option 2 : Set the Button.DialogResult
Public Sub New()
InitializeComponents()
'Your init code here
'...
'By setting the buttons DialogResults, you don't even have to handle the click events
btnIgnorar.DialogResult = DialogResult.Ignore
btnAbortar.DialogResult = DialogResult.Abort
btnReintentar.DialogResult = DialogResult.Retry
End Sub
'However, if you need to do some stuff before closing the form, you can
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
'Do some stuff
'You don't need the following line, as it will be done implicitly
'Me.DialogResult = DialogResult.Abort
End Sub

Visual Basic 2012: Passing variable from one form to another

I have two forms, the main (Main.vb) program window and a pop-up that appears when the program is started (getInitialBalance.vb). I need to get a value entered into the PopUp window from the popup window to the Main program. The relevant code is shown below:
getinitialbalance.vb
Public Class GetInitialBalance
Public initialBalance As Integer
Private Sub btnApplyInitialBal_Click(sender As Object, e As EventArgs) Handles btnApplyInitialBal.Click
Dim textinput As Integer = txtInitialBalance.Text
initialBalance = textinput
Me.Close()
End Sub
End Class
Main.vb
Public Class Main
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GetInitialBalance.ShowDialog()
End Sub
Dim localInitialBalance As Integer = GetInitialBalance.initialBalance
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(localInitialBalance)
End Sub
End Class
New up the GetInitialBalance form and then when the user clicks OK on the popup dialog, grab the value initialBalance from the reference to GetInitialBalance, like this:
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim popup As New GetInitialBalance
If popup.ShowDialog = Windows.Forms.DialogResult.OK Then
localInitialBalance = popup.initialBalance
End If
End Sub
Your entire code should look like this:
Public Class GetInitialBalance
Public initialBalance As Integer
Private Sub btnApplyInitialBal_Click(sender As Object, e As EventArgs) Handles btnApplyInitialBal.Click
initialBalance = Convert.ToInt32(textinput)
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
End Class
Public Class Main
Dim localInitialBalance As Integer
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim popup As New GetInitialBalance
If popup.ShowDialog = Windows.Forms.DialogResult.OK Then
localInitialBalance = popup.initialBalance
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(localInitialBalance)
End Sub
End Class

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