Visual Basic 2010 Event Listeners on a List - vb.net

I have a list of buttons in VB2010.
What is the best way to assign a function to their click event.
So every button has the same function, e.g.:
On Button Click
FireFunction(1)
End On Button Click
Without having to add a click event for every button.
The goal is to produce something similar to what is done with the Control Array idea in Visual Basic 2006.

Define a click function as in:
http://visualbasic.about.com/od/learnvbnet/a/eventhandler.htm
and react based on Sender. There may be a cleaner way to setup delegates in VB.NET, but I use it not.

Related

How to click on a position of form or a control automatically?

I'm using Visual Basic 2008 (VB.NET) and I have an old control which have some buttons on it. These buttons can not be pressed without actual click and the control have no event or API for that. The control is something like the picture below which has multiple objects and buttons as a single control which controls inside the control can be changed dynamically according to some conditions but at a fixed place and this is why I think simulating a click that I said is the best way in my opinion:(The actual control is completely different that the picture below. It's only a sample)
I can click on the button by doing these steps:
Collecting all controls positions on my form
Moving the control in order to put the button exactly at the left top of the form
MyControl.Left = -43
MyControl.Top = -6
Moving form exactly to the center of the screen
Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - 1, Screen.PrimaryScreen.WorkingArea.Height / 2 - 1)
Simulating a click action at the center of the screen
Moving back controls to their positions that I got in the first step
It can be done that way but it's not reliable and it might not be user friendly for my program customers.
I want to ask if there are any better method to do that like simulating a click event on a position on the form itself (so I don't have to move controls and do other steps)?
Of course I feel I must tell you that simulating a mouse click in order to programmatically click a button is not a very good idea. Many things can go wrong with this method (The form could move, making your mouse click on the wrong location; Form could be minimized, or another form could be on top of it, etc)
That said, it is possible, so take a look at this article that contains the code to simulate a mouse click.
http://www.developerfusion.com/code/276/simulating-mouse-events/
Once again, please consider doing this a different way. Iterate through the controls in the user control until you find the button you want and use button.performclick. Or you can call the function that the button.click event calls yourself, if it is public

Triggering Visual Basic Keydown events without a specific function

I'm building on top of code that a previous developer has left me, and he left something that intrigued me quite a bit.
Basically on his menus, he has a TextBox to take in user input and a button next to it to submit the value of the TextBox (for example if the user wanted to select option 1, he would input 1 into the TextBox and click the button). However, the user could also press the Enter key while focusing the TextBox, and it would be treated as the submit button was clicked.
Now this is simple enough to do, but when I check the VB code behind the menu, there's no TextBox_Keydown(...) Handles TextBox.Keydown function anywhere, only the button click event. How is he doing this? He has several menus that are similar and I can't figure out how.
A standard dialog box, if not told to act otherwise, enter does default command button and escape does cancel. In VB look at the properties Default for the command button.
I discovered how he was doing it. He basically mapped the AcceptButton and CancelButton properties of the entire Windows Form to various button functions.

VB.NET: How to change setting on Click events

Visual Studio with VB.net: If I create a copy of a button on the forms designer, the click event is added to the click event handler of the source button.
How do I change this behaviour ? I want the click event of the second button be wired to a totally new click handler code, not to the already existing one of the first button.
There are two ways to copy a button.
CTRL+C / CTRL+V (or same with right click menu). This will create a new button with a new event handler. Note that this new event handler is not created automatically, but will be created on button click in VS designer.
CTRL + drag a control into a new location. This will create a copy with the same event handler, i.e. add an event handler to already existing one. This is the behavior you are seeing.
I'd be interested to know the official reference about this behavior, found the above by experiment. I've used CTRL + drag copy method 95% of the time, and had the same question for quite a while. Fortunately, there is a quick fix for event wiring - read below.
If you used the wrong method, you can manually delete the wiring code (the second handles clause), then double-click the button in question to create new click event handler for it (not a big deal).

In Excel, is there a way to determine the method being called by a password protected 3rd-party ribbon button?

I'm looking to automate a simple process in VBA and need to "click" a ribbon button. It makes the most sense to just call the button's underlying method. Is there a way to figure out what it is?
We are using Excel 2007 and 2010.
If it's Excel 2003 or earlier, you can use the CommandbarControl.Execute method. For instance:
Application.Commandbars("3rd Party Toolbar").Controls("Button to Push").Execute
There are two ways you can do this.
Right click on the toolbar and select Customise. If you right click on the button there may be details somewhere of what Macro the button is associated with.
If you record a macro and click the button. Then you can look at the VBA workspace and view the code that is generated to run your macro (which is just clicking the button you want). This will show you what method the macro clicks.

Visual Basic (VB) Forms: wait for a user click?

I'm making a simple form that is to step through a program iteratively.
How can I get it to run a method each time a user clicks a button and wait once it is finished for the next click?
Maybe I'm not understanding your question very well, but is it like the "Next" button on a Wizard?
You can have a State variable to store the current "state", so at starup its has the value 1. when click "Next" then do the code for State=1 and then change to State=2 and so on.
Inside you Click event you can have a case statement to proceed according
You want to create a method that gets called when it sees the OnClick event.
The simplest way to do this is to double-click the button in the GUI Form Designer, and it will go to the code page and start the method for you with the correct syntax.