Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy One: Catching a Button press on a Form instance? 2

Status
Not open for further replies.

HerbAndEdnaWeinstein

Technical User
Apr 30, 2003
104
US
Hi, I've designed a Form in a Project, and called it "Form_Main". Then I declared the form globally in MyClass like this:

**************************************************
Private m_pForm_Main As Form_Main
**************************************************

I can access it and make changes to it like this:

**************************************************
m_pForm_Main.Show()
m_pForm_Main.TextBox1.Text = "This is a textbox"
**************************************************

What I can't figure out how to do, however, is track button presses or other events on m_pForm_Main.

Thanks in advance for any help!
Herbie

 
You may need to change parameters depending on the event you're wanting to handle.
Code:
Private WithEvents m_pForm_Main As Form_Main

Private Sub foo(ByVal sender As Object, ByVal e As EventArgs) Handles m_pForm_Main.Click
'Insert code
End Sub

Scott
Programmer Analyst
<{{><
 
Great, almost there.

The code you provided got me to the point where I can make events happen when the form itself is clicked on.

But, what if I wanted "foo" to handle the click of "Button1" on the form?

Thanks again,
Herbie
 
Hi Herbie,

You just need to create a routine like Scott has shown, but change the code after the Handles key word to represent whatever event you want the procedure to 'handle'. For example:
Code:
Private Sub foo(ByVal sender As Object, ByVal e As EventArgs) Handles m_pForm_Main.Button1.Click
'Insert code
End Sub
This routine will be executed each time you click on Button1.

Let me know if you need more help.

T0AD


There's a thin line between genius, and insanity!
 
Hi T0AD,

I'm afraid that doesn't work. The only options listed for Handles m_pForm_Main.* are other actions for the form itself -- I can't access its buttons, comboboxes, etc... from there. :( :( :(

Herbie

 
This is what I have:

Main declarations:
Code:
Dim WithEvents f3 As Form3
Dim RemoteButton As Button


Form Load:
Code:
f3 = New Form3()
RemoteButton = f3.Button1
AddHandler RemoteButton.Click, AddressOf RemoteClicked


New Event Handler:
Code:
Private Sub RemoteClicked(ByVal sender As Object, ByVal e As System.EventArgs)
   MsgBox("The remote button was clicked!")
End Sub


 
Yes, I agree it is. If I was implementing it, I may do something different. I may capture the events on the clickable form, and then inform the other class of the event. Maybe something like:

SomethingWasClicked(FormName as string, ControlName as String)



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top