Handler for all textboxes in the form

XtremeMaC

Member
Joined
Oct 22, 2007
Messages
8
Location
Turkey
Programming Experience
3-5
hi there
i've about 10 textboxes in each tab (5 tabs)
i've an explorer bar on the left and when user clicks on each item the correspoding tab & textbox is focused.

I'd like non-empty textboxes to have a different color in the explorer bar and when the user empties the textbox, i'd like correspoding explorer item to set to default color.

now i know how to do this one by one. but not a good programming practice.

the textboxes are named according to the tabs they belong to.
for example:

1st tab 1st textbox: MyBox0_0
1st tab 2nd textbox: MyBox0_1
...
...
2nd tab 1st textbox: MyBox1_0 ... etc etc..

and items in the explorer bar have the same key values.
group 1 item 1 key: MyBox0_0
group 1 item 2 key: MyBox0_1

how can I write a sub to do this for me? :)

TIA!!!
(ps did 2 years of vb.net but time passed and I forgot sub routines and etc :(( )
 
All you do is create one event handler and add all the TextBoxes to the Handles clause at the end. You can do this manually or use the designer. Inside the event handler the 'sender' parameter is the object that raised the event.
 
well yea i know that, but as i am still adding textboxes i didn't want to do it like that. there are 50+ textboxes..
I was hoping for a control handler that controls all the textboxes. but i've not been able to write it.
 
Huh? That's how you write an event handler for multiple controls. There are only two ways to attach an event handler to an event: with the Handles clause or the AddHandler statement. If you have 50+ TextBoxes then you have to either add 50+ TextBoxes to the handles clause of your method or execute the AddHandler statement 50+ times.

If all these TextBoxes are created at design time then there's no code involved. You just select one or more TextBoxes, open the Properties window, press the Events button and then select the desired method from the drop-down list for the event of interest. If you haven't declared the method yet then you double-click the event and the IDE does it for you.
 
ok i thought i could write something like this under tabpage or form1 i dunno. just trying to make things easier..

for each Ctrl as control in me.controls
if type of ctrl is textbox and is not empty then
ctrl.text=.....
end if
next
sort of thing.. but forget it...

i've another question instead.

in the sub i can write
if sender is mybox0_0
then..
end if

but i'd like to automate some stuff so i want to get the name of the sender, how can i get the name of the sender?

my naming scheme is like this:
mybox0_1
0 shows the group number
1 shows the item number

so if i am able to get the name of the sender i'll do this:

group_number = sender.Name.Substring(5, 1))
item_number=sender.name.Substring(7, 1))

ExplorerBar1.Groups.Item(group_number).Items(item_number).StateStyles.FormatStyle.FontBold = TriState.True

thanks. sorry for bugging u with crappy questions.
 
Dim TB As TextBox = DirectCast(sender, TextBox)
group_number = TB.Name.Substring(5, 1))
item_number=TB.name.Substring(7, 1))
 
It's not a type conversion. It's a cast. Have you heard the expression "to cast something in a different light"? It means to look at the same thing or situation in a different way. For instance, let's say that you killed someone. People would say that you deserved to go to jail for a long time. Let's say that we then discovered that they were about to kill you. That information would cast your actions in a different light, and we'd let you go free. Same action, but you look at it a different way. Casting in programming terms is the same thing. The 'sender' parameter refers to the TextBox object that raised the event, but the parameter is type Object. You cast it as type TextBox and you can then refer to the members of the TextBox class, instead of just the Object class. No conversion has taken place because the object is, and always was, a TextBox.
 
Back
Top