automatically click a button in a different form in vb.net - 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

Related

How to access DataGridView properties on an already designed Windows Form

I had already designed a windows form (Form1) with a DataGridView on it, I also have a ribbon with a button on it in which I want when I click on it, it run a code like bellow:
Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles
Button2.Click
Dim f As Form1
f = New Form1
f.Show()
'(Room for my question)
end sub
I want to be able to access my datagridview user control to add columns and rows programmatically.
How should I do that?
You can create a Public Sub on form one which where you will put the changes that you want make on the the Datagridview then call it on the Button2_Click event of your Form2.
Example:
Form 1:
Public Sub AddToDGV()
With DataGridView1
.Rows.Add(Form2.TextBox1.Text, Form2.TextBox2.Text, Form2.TextBox3.Text)
End With
End Sub
Form2:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Form1.AddToDGV()
End Sub

vb.net code to close another form if it is open

This is pretty rudimentary, but I can't figure it out.
I want to programmatically close form1 when form2 closes, if form1 is still open. Form2 was opened by a command button on form1.
The code on Form1 that opens Form2 is:
Dim frm As New form2
frm.Show()
What's the best way when Form2 closes to close any open copies of Form1 that are open, also?
If you want to handle your two forms independently, you need to watch over them from a third form or class. So my suggestion would be to create both of them in this third class, and pass a reference of the second form to the first form so it can then open it. This way:
Public Class MyHelper
Public Sub CreateForms()
Dim form2 as New Form2()
AddHandler form2.Closed, AddressOf Form2_OnClosed
‘ Create as many copies as you need
Dim form1 as New Form1(form2)
form1.Show()
End Sub
Protected Sub Form2_OnClosed(sender as object, e as EventArgs)
‘ Same code for each form1 that has been created and opened.
If (form1.IsOpen) Then form1.Close()
End Sub
End Class
Public Class Form1
Private _form2 as Form2
Public Property IsOpen as Boolean = false
Public Sub New(form2 as Form2)
_form2 = form2
End Sub
Protected Sub MyButton_Click(sender as object, e as EventArgs) handles MyButton.Click
‘ You open your form here or wherever you want (even on the constructor)
_form2.Show()
End Sub
Protected Sub Me_OnClosed(sender as object, e as EventArgs) handles Me.Closed
Me.IsOpen = false
End Sub
Protected Sub Me_OnShown(sender as object, e as EventArgs) handles Me.Shown
Me.IsOpen = true
End Sub
End Class
Add this reference to make it work.
Imports System.Linq
If Application.OpenForms().OfType(Of Form1).Any Then
MsgBox("Form1 is open")
End If
Supposing you have 3 forms and want to close the other two on button click
Private Sub EMPLEADOToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EMPLEADOToolStripMenuItem.Click
If Application.OpenForms().OfType(Of BUSCAR_INDEX).Any Then
BUSCAR_INDEX.Close()
ElseIf Application.OpenForms().OfType(Of MIEMBROS_INDEX).Any Then
MIEMBROS_INDEX.Close()
End If
EMP_INDEX.Show()
EMP_INDEX.EmpIDTextBox.Text = EmpIDTextBox.Text
End Sub

Open a form from another form and get a value and then pass it back to the first form [duplicate]

This question already has answers here:
VB.Net Passing values to another form
(2 answers)
Closed 5 years ago.
I have 2 forms with text boxes and buttons and I want the first form to open the second form. I then proceed to type a value in the text box in the second form so that when I click the button on the second form it must close and pass the value of the text box to the first form text box.
I have been trying to do this with no luck so far...
I am currently using vb.net in visual studio 2015
Here is my code:
Public Class Form1
WithEvents fr2 As New Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2.Show()
End Sub
Private Sub fr2_passvalue() Handles fr2.passvalue
Me.txtform1.Text = fr2.txtform2.Text
End Sub
End Class
Public Class Form2
Event passvalue()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue()
Me.Close()
End Sub
End Class
I have tried accessing the controls directly but it does not work as vb.net now uses classes for everything. I cannot seem to figure out how to get around this.
Redefine your event to pass a string. Then use the Form2 event to pass the value to the handler.
Public Class Form2
Public Event passvalue(text As String)
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue(txtform2.Text)
Me.Close()
End Sub
End Class
Handle it in Form1, updating Form1's TextBox.
Public Class Form1
WithEvents fr2 As Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2 = New Form2()
fr2.Show()
End Sub
Private Sub fr2_passvalue(text As String) Handles fr2.passvalue
Me.txtform1.Text = text
End Sub
End Class
As you had it before, Form1 was accessing controls on Form2 directly, which defeats the purpose of the event.
You could simply define a global variable (Public), which would be accessible from both forms.

Get button name in two different forms in VB

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

Editing button properties from a module

I am just starting out with Visual basic .Net.
I can't seem to figure what's the scope of button properties like button.text. Can they be used outside the button_click event sub? And if so, how?
How can I modify button properties from a module in real time when a certain condition is met?
I'd surely appreciate some guidance and an example, if possible. Thanks.
Just as quick sample, I don't suggest doing something like this
I have 2 forms open, Form2 and Form3. Each form has a button on it.
I also have a Module, called MyModule
Public Class Form2
Public Sub ChangeButtonText(ByVal s As String)
Button1.Text = s
End Sub
End Class
.
Public Module MyModule
Sub ChangeForm2Btn()
Form2.ChangeButtonText("LOL")
End Sub
End Module
From my Form3 I click the button and call the module function to change Form2 button's text
Public Class Form3
Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyModule.ChangeForm2Btn()
End Sub
End Class
You could pass a reference to the button to a sub in the module, and then call that sub from the form.
i.e.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeButtonText(Me.Button1, "Changed")
End Sub
End Class
Module modButton
Public Sub ChangeButtonText(ByRef Button As Button, ByVal Text As String)
Button.Text = Text
End Sub
End Module