Visual Basic 2012: Passing variable from one form to another - vb.net

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

Related

Second form is not loading properly on Visual Builder?

in the first form I have a text box where you can add your name, and then when you click Next window, the second form will pop up saying My name is : Your name . This is great, but how would I be able to change the name of the second form known as OtherForm to my name aswell so that when the second form pops up, on the top left corner my name would appear instead of OtherForm? Here is the code for the first and second form so far.
Main Form code:
Public Class MainForm
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
End Sub
Public Sub Label1_Click(sender As Object, e As EventArgs) Handles
WindowName.TextChanged
End Sub
Public Sub WindowName_TextChanged(sender As Object, e As EventArgs)
End Sub
Public Sub OffButton_Click(sender As Object, e As EventArgs) Handles
OffButton.Click
Me.Close()
End Sub
Public Sub NextButton_Click(sender As Object, e As EventArgs) Handles
NextButton.Click
OtherForm.NameLabel.Text = WindowName.Text
OtherForm.Show()
End Sub
End Class
Second form known as OtherForm
Public Class OtherForm
Public Sub CloseButton_Click(sender As Object, e As EventArgs) Handles
CloseButton.Click
Me.Close()
End Sub
Public Sub OtherForm_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
End Sub
Public Sub NameLabel_Click(sender As Object, e As EventArgs) Handles
NameLabel.Click
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles
Label1.Click
End Sub
End Class
Try
OtherForm.Text = WindowName.Text
to set the title of the form. Or you can set the text to whatever you would like. I'm not sure if you mean "my name" or the name you entered.
If you do mean "my name" then you can enter that in the form property for "Text" instead of in code as well.

Handling multiple dialogs in VB.net

I'm a little confused as to how I'm supposed to handle a chain of dialogs in my VB.net application. For example I have 4 forms, each one has a simple label and is subsequently called from the previous one as a dialog.
First form:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form2
Form2.ShowDialog(Me)
End Sub
End Class
Second form:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form3
frm.ShowDialog(Me)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim cls As New LabelFlash
cls.Flash()
End Sub
End Class
Third form:
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form4
frm.ShowDialog(Me)
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim cls As New LabelFlash
cls.Flash()
End Sub
End Class
Fourth form:
Public Class Form4
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cls As New LabelFlash
cls.Flash()
End Sub
End Class
And the class they all refer to just toggles the visibility of a label on each form:
Public Class LabelFlash
Sub Flash()
If Form1.Label1.Visible = False Then
Form1.Label1.Visible = True
Else
Form1.Label1.Visible = False
End If
If Form2.Label1.Visible = False Then
Form2.Label1.Visible = True
Else
Form2.Label1.Visible = False
End If
If Form3.Label1.Visible = False Then
Form3.Label1.Visible = True
Else
Form3.Label1.Visible = False
End If
If Form4.Label1.Visible = False Then
Form4.Label1.Visible = True
Else
Form4.Label1.Visible = False
End If
End Sub
End Class
Here's my question. When I only have forms 1 and 2 open and I click the button, the label toggle works fine. However, when I get to the third (and fourth) windows only the labels on form 1 and form 2 will toggle and not the third and fourth ones. Additionally, when I close the fourth window it also closes the third at the same time.
I'm sure there's something I'm missing here. Thanks for any help.

How to check if the return value its null vb.net

I start to make an application that automatic press a button on my webpage
the code its works perfect but how do i check if the return value its null?
This is my code
Public Class Form5
Dim CheckButton, skip_button As HtmlElement
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
skip_button = WebBrowser1.Document.GetElementById("skip_button")
CheckButton = WebBrowser1.Document.GetElementById("skip_button")
skip_button.InnerText = "skip_button" 'Replace testID by the ID you want
End Sub
End Class
You can check the result of GetElementById like the following:
Public Class Form5
Dim CheckButton, skip_button As HtmlElement
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
skip_button = WebBrowser1.Document.GetElementById("skip_button")
CheckButton = WebBrowser1.Document.GetElementById("skip_button")
If Not skip_button Is Nothing Then
skip_button.InnerText = "skip_button" 'Replace testID by the ID you want
End If
If Not CheckButton Is Nothing Then
'some code here
End If
End Sub
End Class

vb.net button visibility based on checkbox

So i have a form as below:
Public Class IPADSOFT
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
IPADSOFTTS.Show()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
Me.Hide()
End Sub
End Class
which has 3 checkboxes labeled IPADSOFTBOX1, IPADSOFTBOX2, IPADSOFTBOX3
So... i have another form as follows:
Public Class IPADSOFTTS
Private Sub onload()
If IPADSOFT.IPADSOFTBOX1.Checked Then
Button1.Visible = True
Button3.Visible = True
Button5.Visible = True
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Me.Hide()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
HOME.Show()
IPADSOFT.Hide()
Me.Hide()
End Sub
End Class
Now the idea is that all the buttons on that second form are set to visible-false and i want the page to check which checkboxes are checked on the last form and then make the required buttons on this form visible... but it isnt working
What am i doing wrong?? i apologise im very very new to vb.net
Open the second form with this
Dim newForm As New IPADSOFTTS With
{.MainForm = Me}
newForm .Show()
Set Public MainForm As IPADSOFT below the Public Class of second form
Then use in the Load event
if MainForm.IPADSOFTBOX1.Checked = true then
'Do whatever
End if

How to pass data from one form to multiple forms VB.net

My Form1 looks like this to get the value of textbox to pass the data to multiple forms.:
Public Class Form1
Public username As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
username = TextBox1.Text
Form2.Show()
Me.Close()
End Sub
End Class
At the second form, I used this code to show the data of username which was entered from the Form1.:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = "Hi " & Form1.username & "!"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.Show()
Me.Close()
End Sub
End Class
It worked perfectly. The data showed up in the second form. But in Form3, it only shows "Form1"
Form3 code:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label2.Text = Form1.username
End Sub
What should I do to make the data of username to show up on mutiple forms?
Do not use default instances of forms, create constructors for forms which takes username as parameter and then create forms explicitly
Public Class Form3
Private _username As String
Public Sub New(username As String)
_username = username
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label2.Text = _username
End Sub
End Class
Public Class Form2
Private _username As String
Public Sub New(username As String)
_username = username
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = $"Hi {_username}!"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim formThree As new Form3(_username)
formThree.Show()
Me.Close()
End Sub
End Class
Public Class Form1
Private _username As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim formThree As new Form3(TextBox1.Text)
formThree.Show()
Me.Close()
End Sub
End Class
With this approach you don't need to expose username as public property for Form1.