Get button name in two different forms in VB - vb.net

I have 2 different forms which are form1 and form2. For the form1, I have 4 different buttons with the name btnSet1,btnSet2,btnSet3,btnSet4 and each button of form1 will link to the same form2
Form1
Private Sub btnSet1_Click(sender As Object,
e As EventArgs) Handles btnSet1.Click, btnSet2.Click, btnSet3.Click, btnSet4.Click
Select True
Case sender Is btnSet1
form2.Show()
Me.Hide()
Case sender Is btnSet2
form2.Show()
Me.Hide()
Case sender Is btnSet3
form2.Show()
Me.Hide()
Case sender Is btnSet4
form2.Show()
Me.Hide()
End Select
End Sub
For my form2, I have 4 more buttons. So how do I get my previous form1 button name(btnSet1/2/3/4) to my current form2 and write into form1.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button1.Click,Button2.Click, Button3.Click, Button4.Click
If (sender Is form1.btnSet1) Then
'How do I get the form1 btnSet1/2/3/4 if user click any button in form1
Select Case True
Case sender Is Button1 'form2 clicked button action
'bla...
Case sender Is Button2
'bla...
Case sender Is Button3
'bla...
Case sender Is Button4
'bla...
End Select
ElseIf (sender Is form1.btnSet2) Then
Select Case True
Case sender Is Button1 'form2 clicked button action
'bla...
Case sender Is Button2
'bla...
Case sender Is Button3
'bla...
Case sender Is Button4
'bla...
End Select
End If

There are a number of ways to solve this. First of all, let's get the generic sender object back to a button...
Dim ButtonSet = DirectCast(sender, Button)
Constructor
Add a parameter to Form2's constructor
Public Class Form2(ButtonSet as Button)
End Class
Then use it when you instantiate Form2
Dim SecondForm = New Form2(ButtonSet)
SecondForm.Show()
Pros: Quick 'n dirty
Cons: Assumes Form2 will always be called with a button set. Also breaks anywhere else you use Form2.
Static State
Create a 3rd class that will hold pertinent information.
Public Class SharedInfo
Public Static Property ButtonSet As Button
End Class
Then, in Form1 SharedInfo.ButtonSet = ButtonSet. Form2 can read SharedInfo.ButtonSet any time it wants to.
Pros: Extensible. You can keep adding forms (or variables) and they'll all see the same state.
Cons: Dangerously close to Global Variables, can make it easy to write code that is too tightly coupled. Possible to launch Form2 before the state has been set.
With a Dialog
If you're only opening Form2 to grab additional information, a dialog may be the way to go. Dialogs are used for Colour pickers, file save/open, etc... #Idle_Mind's answer sums this one up nicely.
Pros: Modal (Prevents Form1 from being modified while Form2 is visible)
Cons: Possibly overkill
With properties / other methods
Similar to the dialog approach except that the window isn't modal (you can use both windows at the same time).
Add the following to Form2
Public Property ButtonSet As Button
Then from Form1
Dim SecondForm = New Form2()
Form2.ButtonSet = ButtonSet
Form2.Show()
Pros: Readable, explicit, not hard to extend
Cons: Properties can be changed at any time. Is Form2 going to handle the ButtonSet changing? or only check once on startup? Depends on how exactly you want to use the Button Set in Form2. If you're only going to read it once on load, don't use properties.

According to your example I would do something like this:
Public Class Form1
Dim oForm2 As New Form2
Private Sub btnSet1_Click(sender As Object, e As EventArgs) Handles btnSet1.Click, btnSet2.Click, btnSet3.Click, btnSet4.Click
Select Case True
Case sender Is btnSet1
oForm2.oCallingButton = Me.btnSet1
oForm2.Show()
Me.Hide()
Case sender Is btnSet2
oForm2.oCallingButton = Me.btnSet2
oForm2.Show()
Me.Hide()
Case sender Is btnSet3
oForm2.oCallingButton = Me.btnSet3
oForm2.Show()
Me.Hide()
Case sender Is btnSet4
oForm2.oCallingButton = Me.btnSet4
oForm2.Show()
Me.Hide()
End Select
End Sub
End Class
Public Class Form2
Public oCallingButton As Button
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
If (oCallingButton Is Form1.btnSet1) Then
Select Case True
Case sender Is Button1 'form2 clicked button action
'bla...
Case sender Is Button2
'bla...
Case sender Is Button3
'bla...
Case sender Is Button4
'bla...
End Select
ElseIf (oCallingButton Is Form1.btnSet2) Then
Select Case True
Case sender Is Button1 'form2 clicked button action
'bla...
Case sender Is Button2
'bla...
Case sender Is Button3
'bla...
Case sender Is Button4
'bla...
End Select
End If
End Sub
End Class

I ~think~ I understand what you're after...
Here's my Form1:
Here's my Form2:
Code for Form1:
Public Class Form1
Private Sub DrinkSelection_Click(sender As Object, e As EventArgs) Handles btnCoke.Click, btnPepsi.Click, btnRootBeer.Click, btnSprite.Click
Dim btn As Button = DirectCast(sender, Button)
Dim f2 As New Form2(btn.Text)
If f2.ShowDialog = Windows.Forms.DialogResult.OK Then
ListBox1.Items.Add(btn.Text & ", " & f2.Selection)
End If
End Sub
End Class
Code for Form2:
Public Class Form2
Private _Selection As String = ""
Public ReadOnly Property Selection
Get
Return _Selection
End Get
End Property
Public Sub New(ByVal Drink As String)
InitializeComponent()
Me.Text = "Customize " & Drink
End Sub
Private Sub SelectionMade_Click(sender As Object, e As EventArgs) Handles btnNoIce.Click, btnLessIce.Click, btnNormalIce.Click, btnMoreIce.Click
_Selection = DirectCast(sender, Control).Text
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
End Class

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

(vb.net) how to exit sub when I click button which is in another form?

I'm learning for vb.net step by step. I have two forms and I would like to make a code that is, when I click Button1 which is in the other form, then my main sub exit. I have already searched in google, but I didn't find anything. How can I solve that line?
Public Class Form1
Public Sub Main()
Form2.ShowDialog()
If Form2.Button1_Click = True then '**This line is what I stucked**
Exit Sub
End if
End Sub
End Class
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
An example using DialogResult.
In Form1:
If Form2.ShowDialog = DialogResult.OK Then
' ... do something in here ...
End If
In Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.DialogResult = DialogResult.OK
End Sub
Note that if the user cancels the Form2 dialog without hitting the button then you'll get a result of Cancel back instead.

Changing Controls of a form inside a panel

I am facing a weird problem
I have 3 forms: MainForm, Form1, Form2
MainForm has 1 Panel: Panel1
Form1 has 1 Label: NameLbl and Button: ChangeBtn
Form2 has 1 textbox: NameTxt and Button: SaveBtn
I used the following code to open form1 inside Panel1 in mainform
Panel1.Controls.Clear()
Dim FormInPanel As New Form1()
FormInPanel.TopLevel = False
Panel1.Controls.Add(FormInPanel)
FormInPanel.Show()
On ChangeBtn.Click Form2 opens as showdialog
I want NameLbl.text to change to NameLbl.text when SaveBtn is clicked But normal code doesnt work.
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
Form1.NameLbl.text=NameTxt.text
End Sub
What Should I do? Any suggestions? Given that i need to open the forms in panels for certain reasons.
Please keep in mind that this is just an example. I have multiple controls in Form1 which i want to change on form2.SaveBtn.click
I have also tried this but it does nothing
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If c.Name="NameLbl" Then
c.Text = NameTxt.Text
End If
Next
End Sub
Please Somebody tell me how it do it!
Form2 doesn't seem to have any connection back to Form1 or MainForm. You'd need to raise an event from Form2 which MainForm handles or another class handles and can pass to MainForm
Edit:
Sorry, I've just seen how you're calling Form2. There are lots of ways to get a value back from Form2 after calling ShowDialog(). One is to create a property called Result and check Result if ShowDialog() == DialogResult.OK. Something like the following.
Public Class Form2
Inherits System.Windows.Forms.Form
Public Property Result() As String
Get
Return m_Result
End Get
Set
m_Result = Value
End Set
End Property
Private m_Result As String
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Public ChangeBtn As Button
Public NameLbl As Label
Public Sub New()
Me.ChangeBtn = New Button()
AddHandler Me.ChangeBtn.Click, AddressOf ChangeBtn_Click
Me.NameLbl = New Label()
End Sub
Private Sub ChangeBtn_Click(sender As Object, e As EventArgs)
Dim form As New Form2()
Dim dr = New form.ShowDialog()
If dr = DialogResult.OK Then
Me.NameLbl.Text = form.Result
End If
End Sub
End Class
I'd like to add that if you plan on growing this application much larger you'll run into issues with maintenance. Look into some patterns for dealing with UI logic like MVC, MVP, MVVM if you're interested.
you can try this code:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
panel1.Controls(0).NameLbl.text=NameTxt.text '"0" is the index of forminpanel in panel1,maybe it need to change.
End Sub
Form1 is contained in Panel1, so you can not access it via
Form1.
Only MainForm is visible from Form2:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If TypeOf c Is TextBox Then
c.Text = NameTxt.Text
End If
Next
End Sub
Try this:
For Each form1 As Form1 In MainForm.OwnedForms.OfType(Of Form1)
Form1.NameLbl.text = NameTxt.text
Next
I've faced same issue and I've fixed with this
Dim f As FormInPanel
f = Form.Panel1.Controls(0)
f.transection = True
f.NameLbl.text=NameTxt.text

automatically click a button in a different form in vb.net

I have 2 forms in my VB application, I am populating a textbox with some text which is working fine but i then want to automatically click a button in another form and run the actions for that button click
i have this so far:
Form1.TextBox5.Text = "C:\folder\file1.csv"
Form1.Button8.PerformClick()
but its not clicking the button and performing the actions for Button8 on Form1
How can i make my other form click Button8 on Form1 and run its actions/events?
You can do it like this. The forms will be different from yours because I coded it up as an example. Basically I have added a public method that allows another class to call PerformClick on its button. I believe that's what you were asking for.
' Form1 has two buttons, one for showing the Form2 object and another for performing the click on Form2.Button1
Public Class Form1
Private form2 As Form2
Private Sub ShowFormButton_Click(sender As System.Object, e As System.EventArgs) Handles ShowFormButton.Click
form2 = New Form2()
form2.Show()
End Sub
Private Sub PerformClickButton_Click(sender As System.Object, e As System.EventArgs) Handles PerformClickButton.Click
If form2 IsNot Nothing Then
form2.PerformClick()
End If
End Sub
End Class
' Form2 has a button and a textbox
Public Class Form2
Public Sub PerformClick()
Button1.PerformClick()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox1.Text &= "Clicked! "
End Sub
End Class

How to pass Data from Parent Form to Child Form

I have 2 forms, Form1 is a parent form and Form2 is a child form. Both of them are set to show at the same time.....Form1's mid-container is set to true and has a button, Form2 has a text-box...I want it that if I press the button in Form1 something will appear in the text-box in Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
IsMdiContainer = True
Dim inv As New Form2
inv.MdiParent = Me
inv.Show()
inv.Location = New Point(15, 15)
End Sub
I tried clicking the button but nothing happened, I also tried the other way around...putting a button in Form2 and a text-box in Form1 and it works...
you will have to move the inv variable out of the form_load scope
Public Class Form1
Private inv As New Form2 'here
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
IsMdiContainer = True
inv.MdiParent = Me
inv.Show()
inv.Location = New Point(15, 15)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
inv.TextBox1.Text = "Hello World"
End Sub
End Class
Say you have a TextBox1 control on Form2 and a button on Form1 and upon clicking button on Form1, "Hello World" will appear on TextBox1 on Form2, just do it like this...
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.TextBox1.Text="Hello World"
End Sub