Jump to content
Xtreme .Net Talk

Can I modify the existing textbox class?


Recommended Posts

Guest cgchris99
Posted

I have about 100 textboxes on a form and they are in various tabs.

I have had to add the following handlers to perform some code that I want on these events.

 

Is there a way to modify the existing Textbox class to include these changes so I don't have to add this code 100 times for each of the textboxes on the form?

 

You will notice the Procedure for TextBoxChanged is the same for every textbox on the form and so is TextBoxValidated, TextboxMouseDown, etc.

 

Thanks for any help

 

AddHandler txtTextBox1.TextChanged, AddressOf TextBoxChanged

AddHandler txtTextBox1.Validated, AddressOf TextBoxValidated

AddHandler txtTextBox1.MouseDown, AddressOf TextBoxMouseDown

AddHandler txtTextBox1.Leave, AddressOf TextBoxLeave

AddHandler txtTextBox1.KeyPress, AddressOf TextBoxKeypress

 

AddHandler txtTextBox2.TextChanged, AddressOf TextBoxChanged

AddHandler txtTextBox2.Validated, AddressOf TextBoxValidated

AddHandler txtTextBox2.MouseDown, AddressOf TextBoxMouseDown

AddHandler txtTextBox2.Leave, AddressOf TextBoxLeave

AddHandler txtTextBox2.KeyPress, AddressOf TextBoxKeypress

Posted

Excuse my ignorance ,

but if it is a proprty you can just loop through the form's controls collection .


Dim ctr As Control
       For Each ctr In Me.Controls
           If TypeOf ctr Is Label Then
               ctr.Text = "Cool"
           End If
       Next

 

 

 

or now you can make a method in Net handle multiple events

 


Private Sub subforAllText(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
       Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress

       Dim txt As TextBox
       txt = sender
       
   End Sub

 

I apologize if I completely missed the point of your post, otherwise i hope this helps?!

  • *Experts*
Posted

Your second suggestion is what Derek said, duncallen. Your

first idea can be modified, however, to add event handlers for all

the textboxes, because the other way would make for a

realllllllly long sub declaration.

 

   Dim ctl As Control
   Dim txt As TextBox

   For Each ctl In Me.Controls
     If TypeOf ctl Is TextBox Then
       txt = ctl
       AddHandler txt.TextChanged, AddressOf TextBoxChanged
       AddHandler txt.Validated, AddressOf TextBoxValidated
       AddHandler txt.MouseDown, AddressOf TextBoxMouseDown
       AddHandler txt.Leave, AddressOf TextBoxLeave
       AddHandler txt.KeyPress, AddressOf TextBoxKeypress
       ' etc etc
     End If
   Next

 

You will also have to add a For-Each loop for looping through

each TabPage, and then loop through its Controls collection.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Guest cgchris99
Posted

This looping routine to add to every textbox looks real interesting. That just might work perfectly for me.

 

Thanks

Guest lepernet
Posted

You can add a "user control" to your project.

 

This will appear like a borderless form in the designer.

 

Drop a text box onto it and resize as appropriate.

 

You can add whatever logic you want to the text box.

 

When compiled the new user control will appear in the toolbox and you will be able to drop it onto the form.

 

You can do the same with groups of controls.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...