I have been building an app to learn and make my life easier some day.
I have way too many text boxes on it now and want to be able to handle some of the events for all the text boxes using a single sub. Can this be done Have been poking around with the addhandler stuff, but no luck as yet.
In VB6 I might have built a 'real' large array of textboxes but this didn't seem possible in 2005, might have missed something thou....
TIA
stubs

textbox_gotfocus event to event handler
Tamer Raafat
(Newbie here)
I am just at the finishing stages of my first project and have something similar: 72 textboxes in a grid all using the same click event hadler, and a sub form with 32 textboxes.
To get this, I needed to create and set all the properties for the textboxes at run time. The advantage there was that by using
AddHandler newT.Click, AddressOf mainClickButton ' all grid clicks
in the loop that the textboxes are created, I get all the click events to mainClickButton procedure.
Fredrik Lindstr&#246&#59;m - Qbranch AB
This approach works nicely as you can redirect different events from a variety of controls to the same function. Those in VB6 had control arrays which would sort of allow common code but it was limited to a specific type and method (yes you could create a common function and then just put calls in each of the control array event handlers but you still had calls all over the place).
.net sukbir
Yes it can,
If you handle multiple events that you want to consolidate you can hook up the same sub to many different textbox events.
Example
Whenever I leave one of three text boxes I want to set the text of that box to a label. So I know the label will reflect the contents of the last text box I was in.
Private Sub TextBoxLoseFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus
Label1.Text = CType(sender, TextBox).Text
End Sub
So you will see I have created a single function TextBoxLoseFocus and hooked up the following three events TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus to it and use the value of the sender parameter to get the text from the appropriate control that has lost the focus.
StrongTypes
Well, I built a loop which creates each Textbox, defines several properties for the new Textbox, adds each text box to a Collection and assigns the event handler for the Textbox.
All the above in the same loop, therefor, each new Textbox gets assigned the same event handler. See below
Sub DrawNewGameGrid() Dim xOffset As Integer = 4, yOffset As Integer = 4 ' placement location Dim cC As Integer = 0 Dim newP As New PanelnewP.Location =
New Point(24, 24)newP.Size =
New Size(442, 528)newP.BorderStyle = BorderStyle.Fixed3D
newP.BackColor = Color.BurlyWood
Me.Controls.Add(newP) Dim r As Integer, c As Integer For r = 1 To 9 For c = 1 To 8cC += 1
' inc Textbox count Dim newT As New Textbox 'create new instance of a Textbox ' set some properties.newT.Name =
"newT" + cC.ToString()newT.Size =
New Size(50, 50)newT.Font =
New Font(newT.Font.Name, 32)newT.TextAlign =
CType(2, HorizontalAlignment)newT.BorderStyle = BorderStyle.FixedSingle
newT.Location =
New Point(xOffset, yOffset)newT.Cursor = Cursors.Arrow
newP.Controls.Add(newT)
'put new button into panel COLLECTION AddHandler newT.Click, AddressOf mainClickButton ' all main grid clickmyTextboxes.Add(newT)
xOffset += 54
Next cxOffset = 4
yOffset += 58
Next rFirstGameStarted =
True End Subdeveloperjj
If your asking if you can hookup events other than clicked, such as gotfocus. Sure you can hook up any event using the AddHandler method.
If you asking if the gotfocus is the event you want to use in your application you'd need to detail your usage scenario more. As the gotfocus event occurs any time to focus gets control - which could be via a click or via keyboard input such as Tab
Madhu74
Well I was looking to be able to hook more than just one event the question was sort of general about 'addhandler'... I worked out what I needed. Although I need to do more testing...
Dim coa() as Control
Other filtering code...
ReDim coa(CurrentCodeTab.Controls.Count - 1)CurrentCodeTab.Controls.CopyTo(coa, 0)
this adds an event handler for the Gotfocus event for the displayed textboxes.. Dim cnt As Integer For cnt = coa.GetUpperBound(0) To 0 Step -1 If TypeOf coa(cnt) Is TextBox Then AddHandler coa(cnt).GotFocus, AddressOf mysub End If NextThis will add a handler to existing design time created controls ''textbox''. the GotFocus events call the MySub sub...
Now I just have to hook the other events I need to some other subs.. and then RemoveHandler after I'm done..
Thanks for the clues as to where to look All of you other coders...
stubs
BigRedDog
If you create the controls at runtime they would not have any of the event handlers hooked up to perform actions. Hooking up an event handler can be done in a couple of ways , one is to add the handles clause after the procedure and add the events which is what was talked about earlier in this thread. The other is to do it programatically which usually occurs at runtime.
AddHandler statement hooks up the event with the procedure to be run. In this case, in this example
AddHandler newT.Click, AddressOf mainClickButton
It hooks up the NewT button click event and will run the mainClickButton method for each of the newT buttons created.
This way the button click events are being hooked up on the new controls created.
This saves having to type handles Button1.click, Button2.click, .... Button72.click when defining this events at design time.
Mike Nguyen
Ok so in this way
'AddHandler newT.Gotfocus AddressOf mainClickButton'
would hook the gotfocus event in the textbox to the mainClickButton sub
stubs..
RT
Yes I have done this with other controls and like the way it works, but with upwards of 35 text boxes so far and most likely more to come.
I was looking for a way to redirect all of one type of event to a sub with out having to type so many "Handles".. ..
Some kind of sub classing maybe Still new to the .net stuff stayed with VB6 to long I guess..
stubs
James Peters
Sorry still a bit dense on this. How does creating the controls at run time allow you to redirect the click events for the grid
stubs
kaushal.b
I don;t know about the GotFocus event, but the Click event is the one which I thought you wantd The GotFocus event fires when you (say) use tab to move between controls.
AddHandler newT.Gotfocus AddressOf mainClickButton'
If you build a series of controls (textboxes) in a loop, and assign one handler for all of them, then clicking on any of them will be trapped by the sub mainClickButton in the following statement.
AddHandler newT.Click, AddressOf mainClickButton
by the way, the newT was my choice of variable name for each textbox at time of creation to which the event handler is attached,