-
Feb 12th, 2009, 11:38 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Handle Groups?
I was just wondering if their was an easy way to create one handle for several controls.
For instance, I have 20 buttons and I want them all to do the same thing when clicked. Instead of having 20 handles for each button.click, is there a way to create one handle that can cover them all, such as a grouping or something?
-
Feb 13th, 2009, 12:16 AM
#2
Re: [2005] Handle Groups?
You just put them all in the Handles clause of the event handler. You can do it manually or have the IDE do it for you. To do that you select all the controls in the designer, open the Properties window, click the Events button and then double-click the event of interest.
Last edited by jmcilhinney; Feb 13th, 2009 at 12:48 AM.
-
Feb 13th, 2009, 12:19 AM
#3
Re: [2005] Handle Groups?
That is pretty easy.
If you have dropped the buttons on form at design time then use this code:
vb.net Code:
Private Sub Button_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs _ ) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click ' do stufff End Sub
And if you are creating controls at run-time then use below code:
vb.net Code:
AddHandler Button1.Click, AddressOf Button_Click AddHandler Button2.Click, AddressOf Button_Click AddHandler Button3.Click, AddressOf Button_Click AddHandler Button4.Click, AddressOf Button_Click
-
Feb 13th, 2009, 12:50 AM
#4
Re: [2005] Handle Groups?
Just to clarify, if you were to follow the instructions I provided then you'd end up with code much like what DS provided in his first code snippet. If you need to know whcih Button was actually clicked then you can get a reference to it from the 'sender' parameter.
-
Feb 13th, 2009, 04:37 PM
#5
Thread Starter
Fanatic Member
Re: [2005] Handle Groups?
Thanks, that didn't really solve the problem that I addressed but jmc your first suggestion will work for now.
I've used multiple events in the handles for a while, so I know how that works. What I want to know is if there is a way to create one variable or something for a group of buttons, and then I can use that variable.click in the handles section of a procedure and it will cover them all.
I don't want to have to type in "Handles button1.click button2.click button3.click...".
All the way to 20 or however many I use.
jmc your first suggestion may work in that I don't have to do as much typing if I simply select and then use the properties window to assign them all to an event automatically.
Thank you both for the suggestions though.
-
Feb 13th, 2009, 04:44 PM
#6
Re: [2005] Handle Groups?
If you have a lot of buttons, make them into an array of buttons, then create the AddHandler (per Deepak's post) in a simple loop.
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Feb 13th, 2009, 07:29 PM
#7
Thread Starter
Fanatic Member
Re: [2005] Handle Groups?
How can I make an array of buttons?
-
Feb 13th, 2009, 07:51 PM
#8
Re: [2005] Handle Groups?
Dim MyButtons() As Button
or you can create the array by clustering your existing buttons;
Dim MyButtons(4) As Button = {MyButton1, MyButton2, ExitButto, SaveButton}
Controls can also be grouped into an array programmatically, so you do not need to type all of them in, this can be done by looping through Me.Controls, and adding any occurrences of a button control to an array.
Arrays such as this allow you to loop through controls, rather than consulting each control with individual statements.
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Feb 13th, 2009, 08:15 PM
#9
Thread Starter
Fanatic Member
Re: [2005] Handle Groups?
Ok so once I put the names of the button in the array, I can just use:
Handles MyButtons.Click
To have the click event for every button in the array all in one handle? I'll try it out.
Also, is there a specific command for looping through arrays? I know what you mean by looping through me.controls using "for each ctrl as control in controls", but is there something for arrays like "for each variable as [slot] in array"?
-
Feb 13th, 2009, 08:28 PM
#10
Re: [2005] Handle Groups?
ok, let's be more succinct here.
If you have an array of buttons called MyButtons, you can loop through using loops such as;
Code:
For Each B As Button In MyButtons
B.Enabled = False
Next
So using a loop like that, you can assign the same event handler to every button and have one sub that will be called when any of those buttons is clicked. Within that sub, you can determine which button has been clicked by virtue of the "sender" property.
To loop through all controls, you will need to determine if a given control is the type you want. So the loop below will show the names of all buttons on "Me."
Code:
For Each Con As Control In Me.Controls
If Con.GetType.ToString = "System.Windows.Forms.Button" Then
MessageBox.Show(Con.Name & " is a button")
End If
Next
You do need care in a loop such as this to check the component type. If you did not do that and tried to change a button property, but the current control was actually a CheckBox for example, you will get an exception thrown (if the CheckBox does not have that same property).
Last edited by Bulldog; Feb 13th, 2009 at 08:31 PM.
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Feb 13th, 2009, 08:34 PM
#11
Re: [2005] Handle Groups?
Originally Posted by Vectris
Ok so once I put the names of the button in the array, I can just use:
Handles MyButtons.Click
To have the click event for every button in the array all in one handle? I'll try it out.
Also, is there a specific command for looping through arrays? I know what you mean by looping through me.controls using "for each ctrl as control in controls", but is there something for arrays like "for each variable as [slot] in array"?
No you have misunderstood. An array is an array. It has no Click event. If you want to handle the Click events of multiple Buttons then you have to handle the Click events of multiple Buttons, plain and simple. You either have to put them all in the Handles clause or else you have to call AddHandler for them all. What you're suggesting was possible in VB6 with control arrays but is not in VB.NET
Basically, this a waste of time. You're writing code to save yourself writing code but, as I've demonstrated, you don't have to write the code you're trying to avoid anyway. Just use the designer to create and/or attach your event handlers, which is exactly why VB.NET doesn't support control arrays in the first place.
-
Feb 13th, 2009, 10:39 PM
#12
Thread Starter
Fanatic Member
Re: [2005] Handle Groups?
Originally Posted by jmcilhinney
No you have misunderstood. An array is an array. It has no Click event.
Well I know arrays don't have click events but I was wondering if adding click to the end of a special kind of array for controls or something would automatically read the contents and handle the click events for all the controls in the array.
Basically, this a waste of time.
Well I don't see this as a waste of time, I see it as a way to save time, that's why I'm asking.
As for the rest of your post, is that just a "no" that it' can't be done? I'm not used to using the designer to make handles, I just go to the view code area and select the control and then the event. Then I add in handles as I need them. I was just looking for a way to add a bunch of handles quickly or cover a bunch of handles with just one.
-
Feb 14th, 2009, 03:10 AM
#13
Re: [2005] Handle Groups?
Fact of the matter is, if you put a whole bunch of buttons on your form, then you're going to need a whole bunch of handlers. The benefit of tying all buttons to a single handler is that much less code needs to be written, making your program leaner and more efficient.
But whining about the correct solution because you may have to do a little typing is just childish and pointless. In the time it took you to post complaints, you could have finished adding the required code and done something useful.
-
Feb 14th, 2009, 04:29 AM
#14
Re: [2005] Handle Groups?
I always think writing the code to do the loops is much more efficient in the long run. I have found myself adding 80 event handlers to buttons using the designer, and then having to delete about 20, and add another 30, etc...
Using a loop you don't need to make any adjustments to your code after adding or removing buttons later.
First, create a List(Of Button) to hold your buttons. In the Form_Load loop through all the controls on the form and add only the buttons to this List.
In addition, you add an event handler to each button.
vb.net Code:
Dim buttons As List(Of Button) Private Sub Form1_Load(...) Handles Mybase.Load For Each ctrl As Control In Me.Controls If TypeOf ctrl Is Button Then Dim btn As Button = DirectCast(ctrl,Button) buttons.Add(btn) AddHandler buttons_Click, AddressOf btn.Click End If Next End Sub Private Sub buttons_Click(sender As Object, e As EventArgs) Dim btn As Button = DirectCast(sender, Button) btn.Text = "Hi, I'm a clicked button!" End Sub
Whenever you might need to loop through all the buttons now you can loop through the List:
vb.net Code:
For Each btn As Button In buttons btn.... Next
-
Feb 14th, 2009, 05:23 AM
#15
Re: [2005] Handle Groups?
You might be looking for something like this:
vb.net Code:
Dim buttons() As Button Dim textBoxes() As TextBox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'initialize array of controls we want to use buttons = New Button() {Button2, Button3, Button4, Button5} textBoxes = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5} 'Attach whatever events you want to. Ensure correct spelling :) AttachEvent("Click", AddressOf Button_Click, buttons) AttachEvent("TextChanged", AddressOf TextBox_TextChanged, textBoxes) End Sub Private Sub AttachEvent(ByVal eventName As String, ByVal eventHandler As EventHandler, ByVal ParamArray ctrls() As Control) Dim e As Reflection.EventInfo = ctrls(0).GetType.GetEvent(eventName) For Each ctrl In ctrls e.AddEventHandler(ctrl, eventHandler) Next End Sub Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'button click event code here MsgBox(sender.Name & " clicked") End Sub Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) 'text-change event code here MsgBox(sender.Name & " text-changed") End Sub
The code is self-explanatory. Let me know if you need some clarifications.
Pradeep
Last edited by Pradeep1210; Feb 14th, 2009 at 05:27 AM.
-
Feb 14th, 2009, 07:12 AM
#16
Re: [2005] Handle Groups?
Originally Posted by Vectris
Well I know arrays don't have click events but I was wondering if adding click to the end of a special kind of array for controls or something would automatically read the contents and handle the click events for all the controls in the array.
There is no special kind of array. There was in VB6. There isn't in VB.NET.
Originally Posted by Vectris
Well I don't see this as a waste of time, I see it as a way to save time, that's why I'm asking.
As for the rest of your post, is that just a "no" that it' can't be done? I'm not used to using the designer to make handles, I just go to the view code area and select the control and then the event. Then I add in handles as I need them. I was just looking for a way to add a bunch of handles quickly or cover a bunch of handles with just one.
All this talk of writing code to loop through all the buttons top attach event handlers is a waste of time. You asked for a quick way to add a control to a Handles clause and you have one: use the Properties window of the designer.
-
Feb 14th, 2009, 01:31 PM
#17
Thread Starter
Fanatic Member
Re: [2005] Handle Groups?
Originally Posted by MaximilianMayrhofer
But whining about the correct solution because you may have to do a little typing is just childish and pointless.
I'm not whining, I asked a question and I want my question answered, that's why I asked it. I don't want a bunch of "no you can't do this", just don't answer it then. Also I fail to see any "correct solutions" other than using the properties window, and I'm not complaining about that at all, in fact that's what I'm going to use, and it doesn't involve typing.
@Nick
While the properties window should be faster that will work very well for adding handles during runtime or just as an alternate way. I also like having a list of buttons, I never thought of doing that but it can be useful, thanks.
All this talk of writing code to loop through all the buttons top attach event handlers is a waste of time.
It's not a waste of time if I need to re-asign lots of handles during runtime . But properties window will most likely be what I use for now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|