0

I am working on Visual Basic with a Login_Form. I would like to pass a value from Login_Form to TableSelection. It works for the first time, but when I log out and login again. The TableSelection get the same value as the first time I passed.

Here's my code: idNumber is the public variable in the TableSelection

Private Sub LoginButton_Click(sender As Object, e As EventArgs) Handles LoginButton.Click
   Select Case EmployeeIDTextBox.Text.Substring(0, 1)
      Case 1
          MainScreen.Show()
      'Number starts with 2 takes user to waiter screen
      Case 2
          TableSelection.idNumber = EmployeeIDTextBox.Text.Substring(1, 1)
          TableSelection.Show()
   End Select
   Me.Hide()
End Sub

The login works with different EmployeeID, but the TableSelection does not update the ID

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
MTT
  • 49
  • 4
  • **I mean login again with different ID. So it should pass different ID. – MTT Apr 29 '19 at 15:34
  • 2
    I notice that there are some problems which would be shown to you by Visual Studio if you used [`Option Strict On`](https://stackoverflow.com/a/5076966/1115360). It could be that correcting them will make the program work as intended. – Andrew Morton Apr 29 '19 at 15:42
  • There are many errors when I turn on this option. There are many type conversions in my project so I should turn this off. Thanks Andrew! – MTT Apr 29 '19 at 15:47
  • It won't take long to correct all the type conversions once you get started :) – Andrew Morton Apr 29 '19 at 15:48
  • 1
    @MTT if you're going to continue to be lazy and write sloppy code, specifically with Strict Off, and not exercising proper methods, I cant quite say I am willing to continue putting the effort in to help you on your questions. – Mr. Tripodi Apr 29 '19 at 15:52
  • @MR. Tripodi, I created a new project and tested this Strict Option. I realize that when the Form2 is loaded for second time. Its load function is not called whether the Strict Option is turn on or off. The ID is passed correctly but my functions won't be called in ``` Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load```. – MTT Apr 29 '19 at 16:13
  • How many forms are there, what are there names, how are they invoked, and which of them has the code shown? Also which form is the startup form? If 'TableSelection' is a form the problem is that you need to create an instance of the form, then set idNumber and show that instance. – dbasnett Apr 29 '19 at 16:19

1 Answers1

1

One way to do this is to pass the integer into Form2 from Form1 as shown below:

Public Class Form1
  Dim IntFromForm1 As Integer = 2

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using Form2 As New Form2(IntFromForm1)
      Form2.ShowDialog()
    End Using
  End Sub
End Class

Public Class Form2
  Dim MyForm2Int As Integer

  Public Sub New(ByVal MyIntFromForm1 As Integer)
    InitializeComponent()
    MyForm2Int = MyIntFromForm1
  End Sub

  Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MsgBox(MyForm2Int)
  End Sub
End Class
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Mr. Tripodi
  • 809
  • 1
  • 6
  • 7
  • Note that "End Class" is part of the class (obviously) I have no idea why it is not part of the SO code block – Mr. Tripodi Apr 29 '19 at 16:28
  • Code blocks require a minimum four space indent. – LarsTech Apr 29 '19 at 16:34
  • Thank you so much, It works now. And new question arises for future learning. Say we have 3 Forms, Form1, Form2, Form3. I want to pass a value1 from Form1 to Form3, and a value2 from Form2 to Form3. We only create an instance of Form3 with one value passing at a time right? So how do we get value2 from Form2. – MTT Apr 29 '19 at 18:51
  • Instead of creating a new instance of Form2 and passing in the integer, you would simple create a new instance of Form3 and pass in the Int. – Mr. Tripodi Apr 29 '19 at 18:53
  • I'm sorry I accidentally press Enter while asking so the question did not come up fully. Assuming these two values have different types. – MTT Apr 29 '19 at 18:57
  • Then you would simply send the variable as Type X and declare it as type X in the Form (Class) you are passing it to... – Mr. Tripodi Apr 29 '19 at 19:07