Handling multiple dialogs in VB.net - 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.

Related

how to change the value of a label in childForm from Another form button

need a little help i cant seem to change the value/text of a label when im using this method to open another form.
i have 3 form (form1, form2, form3) form1 is my main form which has a panel which i want the form 2 to appear/show(which i manage to do). then in form 2 i have a label and a button(to open form 3 using showdialog which is working). now in form 3 i have a button that will change the text of the label in form2 but its not working here the code.
form1
Public Class Form1
Dim myForm As New Form
Public Sub openChildForm(childform As Form)
If Not IsDBNull(myForm) Then myForm.Close() ' this will close active form
myForm = childform ' store childform in myform
childform.TopLevel = False
childform.FormBorderStyle = FormBorderStyle.None ' remove border to be put in panelChildForm
childform.Dock = DockStyle.Fill ' make childform dockstyle to fill to remove excess space if there are
Panel1.Controls.Add(childform) ' add childform as a control of panel1
Panel1.Tag = childform
childform.BringToFront()
childform.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
openChildForm(New Form2)
End Sub
End Class
form2
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.ShowDialog()
End Sub
End Class
form3
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Label1.Text = "NEW FORM 2"
End Sub
End Class
this is what it looks like
form2
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Form3.ShowDialog()
Label1.Text = My.Forms.Form3.str
End Sub
End Class
form3
Public Class Form3
Public str As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
str = "NEW FORM 2"
End Sub
End Class

How to hold reference to different forms with one variable

I have a Form (named Form1) which has a fixed size and some controls.
I created another Form (named Form2), which is a copy of Form1 with only difference being a different fixed size.
I created a Form "SharedForm", which holds subs and functions used by both forms (so I don't have to write them for each of them).
My problem is: I don't know how to keep reference for either form (only one at a time, ever).
If I declare FormRef variable as Form, I get an error that "label1 is not a member of form".
(Otherwise if I declare as Form1 or Form2 it works fine, but of course only for one form)
SharedForm looks like the following:
Public Class SharedForm
Public Shared FormRef As Form 'problem is here
Private Sub SharedForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FormRef = New Form2
FormRef.Show()
End Sub
Public Shared Sub Button1_Click(sender As Object, e As EventArgs)
FormRef.Label1.Text = "test"
End Sub
End Class
Form1 and Form2 are as so.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, AddressOf SharedForm.Button1_Click
End Sub
End Class
Just a class, not a Form.
Public Class EventCode
Public Shared Sub Button1Click(Sender As Form)
Dim frm As Form = Nothing
If Sender.Name = "Form1" Then
frm = DirectCast(Sender, Form1)
ElseIf Sender.Name = "Form2" Then
frm = DirectCast(Sender, Form2)
End If
frm.Controls("Label1").Text = "Hello World"
End Sub
End Class
In Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form1 shows "Hello World"
In Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EventCode.Button1Click(Me)
End Sub
The label on Form2 shows "Hello World"

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

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