Click a specific button on another form - vb.net

Consider i am having two forms, form1 and form2
How can i click,mouse over(any events) a specific button on another form using coding in vb.net?

I'm assuming that Form1 launches Form2, since there's not a whole lot of information in the description.
When Form1 launches, there are two buttons: "button1" and "Launch Form 2" (forgot to change text on button1, sorry. :(
When you click "Launch Form 2", Form2 pops up:
Clicking the "button1" on Form1, a message box originating from Form1 pops up saying:
Clicking the "button1" on Form2, a message box ALSO originating from Form1 pops up saying:
Here's the code:
Form1
Public Class Form1
Private WithEvents frm2 As New Form2
Private Sub Form1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1Button.Click
RunSomeCode("Called from form 1!")
End Sub
Public Sub RunSomeCode(ByVal message As String)
MessageBox.Show(message)
End Sub
Private Sub Form1LaunchForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1LaunchForm2Button.Click
frm2.Activate()
frm2.Show()
End Sub
Private Sub frm2_SimulateForm1ButtonClick() Handles frm2.SimulateForm1ButtonClick
RunSomeCode("Called from form 2!")
End Sub
End Class
Form2
Public Class Form2
Public Event SimulateForm1ButtonClick()
Private Sub Form2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2Button.Click
RaiseEvent SimulateForm1ButtonClick()
End Sub
End Class
How it works
Form 2 has a public event called "SimulateForm1ButtonClick". That event can be raised whenever you want, from any code block. I just decided to raise it when I click the button on the form.
Form 1 has an instance of Form2 WithEvents. It's very important that you use the WithEvents keyword, or that public event in Form2 won't show up. :(
Form 1 has a sub that handles the "SimulateForm1ButtonClick" that is raised when Form2 clicks its button.
Now, here's another important detail: The code executed when button1 is clicked on Form1 is actually in a private sub called RunSomeCode(). This is important, because it makes the code accessible from any other part of Form1, namely the part that handles Form2's event.
I hope that helps you out a little bit. I'm not sure exactly what you were asking. :/
Code: http://darin.hoover.fm/code/dl/FormsSandbox.zip

If you are trying to fire the event, just use Form2.Button1.PerformClick() assuming that the button on form 2 is called 'button1'.

Related

Passing value from one form to another using events in vb.net

I'm trying to pass a value from one form, let's call it Form1 to another form, Form2. When i double click a row in a listview that is in Form2, the value should be passed to a combobox in Form1.
I could get one of my other forms to do this using an event called PropertyChanged, but I can't seem to get it to work on other forms. I don't know if it is the fact that you can only have 1 event in the entire project and not have another with the same name. I'm missing something, but i just don't know where.
This is the code i used in Form2:
Public Event PropertyChanged As Action(Of Object)
Private Sub ListView2_DoubleClick(sender As Object, e as EventArgs) Handles ListView2.DoubleClick
RaiseEvent PropertyChanged(ListView1.SelectedItems(0).SubItems(0).Text)
End Sub
And this is the code I used in Form1:
Dim WithEvents f2 As Form2
Private Sub PropertyChanged(obj As Object) Handles f2.PropertyChanged
cmb_form1.Text = obj
End Sub
There are several ways to implement this. This one is quick and dirty.
It consists of writing up a property in Form2 which is public. When the user makes a choice, it goes in the property. Then Form1 reads the property before it disposes Form2.
Here's a little code snippet to better illustrate what I mean:
Public Class Form1
' This form has a button which opens a Form2 instance
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 As New Form2 ' instantiate Form2
form2.ShowDialog() ' the user chooses a value
MessageBox.Show(form2.Result) ' get it before it's out of scope!
End Sub
End Class
Public Class Form2
Public Property Result As String ' the value is stored in there, it can be any type
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Result = "Clicked!" ' store the value you want to share between forms
Me.Close()
End Sub
End Class
Have fun!

Visual Basic:Closing and Opening Forms?

I am making a program in which you can press a button and it makes a new form but I have a little bit of a problem.
Because I have Form1 and Form2. When I press the button on Form1 it shows Form2 and it should just close Form1.
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Form2.Show()
Me.Close()
What actually happens is that it closes Form1 and Form2 even if I said Me.Close().
Is there a fix to it or did I just do it wrong somehow?
I bet Form1 is your startup form. If not sure sure check that in Project properties.
The moment you close Form1, your application terminates altogether. So in simple words you can't close Form1 and show another form, at least not with 2 lines of code.
What you can do is hide Form1 and Show Form2.
Form2.Show()
Me.Hide()
Now when you close Form2, make sure you either unhide Form1 (so that usercan manually close it) or automatically close Form1 from Form2's FormClosing event, else your process will be alive in the background, a ghost :)
So in your Form2, add the FormClosing event handler and then inside that close Form1
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub

How to Call usercontrol in Windows form

I am working with windowsForm in vb.Net.
I have one main form(frmMaster) that has buttons on the left side and one panel(PanelDetail) control on the right side.
I have two user controls, let say uc1 and uc2.
When I clicked the button from main form, it opens the uc1 in the panel properly through below code.
PanelDetail.Controls.Add(uc1)
uc1.Show()
There is a button control on uc1 and i wants to open uc2, when user clicks on the button from uc1.
So i have to add the uc2 to the PanelDetail and hide or remove the uc1. To do this, i have created the public method in the frmMaster
Public Sub DisplayControl(ControlName As UserControl)
PanelDetail.Controls.Clear()
PanelDetail.Controls.Add(ControlName)
ControlName.Show()
End Sub
and call this method from the button click event of the uc1 to call uc2
frmMaster.DisplayControl(frmMaster.vuc2)
uc2 is already declared as shared in the main form as
Public Shared vuc2 As New CtrlLeavesList
So when i click the button from uc1, it clears the uc1 from panel, but it does not load the uc2.
can anyone suggest me the right way to do this.
Thanks.
As OneFineDay said, you can declare an event in uc1.
Public Event RemoveButtonClicked()
Then in the event handler for the actual button on the user control (uc1), you raise the event.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent RemoveButtonClicked()
End Sub
Then in the main form, you would declare an instance of the uc1, and register a handle for the event.
Public WithEvents uc1 As New CtrlLeavesList
Public Sub uc1_RemoveButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles uc1.RemoveButtonClicked
DisplayControl(uc2)
End Sub
I have fixed the issue by using below code.
You need to define the public event as below in uc1
Public Event RemoveButtonClicked As EventHandler
and then raise the event in button click of the uc1
Private Sub btnApplyLeave_Click(sender As System.Object, e As System.EventArgs) Handles btnApplyLeave.Click
RaiseEvent RemoveButtonClicked(sender, e)
End Sub
Then in the main form, you would declare an instance of the uc1, and register a handle for the event.
Public Shared uc1 As New CtrlLeavesList
Public Shared uc2 As New CtrlApplyforLeave
Then most important you need to add the handler on the form load of the main form
AddHandler vCtrlLeavesList.RemoveButtonClicked, AddressOf vCtrlLeavesList_RemoveButtonClicked
and then register the event
Public Sub vCtrlLeavesList_RemoveButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs)
PanelDetail.Controls.Add(uc2)
uc2.Show()
End Sub
The above example is working perfectly for me.

Launching a MDI menu option with Enter key fires KeyUp in MDI Child form

Using Visual Studio 2012
Added an MDI Form to project which creates a bunch of default menu items and then added the following code for the File/Open menu item:
Public Class MDIParent1
Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
Dim frm As New Form1
With frm
.MdiParent = Me
.Show()
End With
End Sub
End Class
In another form, Form1, set KeyPreview = True and put in this code:
Public Class Form1
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Enter Then
MsgBox("enter pressed")
End If
End Sub
End Class
Now, if you launch the MDI form and click the File menu (or ALT+F) then scroll down using arrow keys to Open and hit Enter on the keyboard, it launches Form1 but fires KeyUp(). In my actual application this ends up launching another child form to Form1 and I need to suppress the Enter key from the MDI menu launch but can't figure out how to do that.
Handle KeyPress or KeyDown instead of KeyUp.

How to pass text from one form to opened form at back?

I have two forms. One is InvoiceEntry and another is ListofSuppliers. There is a control "txtsnam" in InvoiceEntry.ListofSuppliers contains a Datagrid which has all the names of suppliers. When user presses f9 on this textbox ListofSupplier form is opened. The user select the name of supplier from grid and press enter. On this press enter i want to pass the name of supplier to the "txtsnam".I have used this code but its not working for me:
On InvoiceEntry:
Private Sub txtsnam_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtsnam.KeyDown`
If e.KeyCode = Keys.F9 Then
Dim lov As New SupplierLOV
lov.ShowDialog()
End If
End Sub
On ListofSuppliers:
Private Sub RadGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadGridView1.KeyDown
If e.keycode=keys.Enter then
InvoiceEntry.txtsnam.text=RadGridView1.CurrentRow.Cells(0).Value.ToString
End If
End Sub
I also tried with the help of property but its also not working. I placed the breakpoint on line 2 it executes bt text doesnot appear in txtsnam
Solution 1: Create an event in Form2 and subscribe to it in Form1 when you open the Form2. When pressing on the Return key, raise the event in Form2 and transmit the value as parameter. Form1 will receive your value.
Solution 2: Add a member to Form2 of type Form1 and pass Form1 as parameter to Form2 when you create Form2. In this way you have full access to Form1 properties and you can set it from code anytime. When you close Form2 you already have your data set in Form1.
Solution 3: Have a static provider class and store there your info that you want from different Forms. You can set and get these values from anywhere in your program.
Solution 4: ... there are many other ways. These were the simplest ones.
Try this
Private Sub RadGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadGridView1.KeyDown
If e.keycode=keys.Enter then
InvoiceEntry.txtsnam.text=RadGridView1.Currentcell.value
End If
End Sub