jccorner Posted June 9, 2004 Posted June 9, 2004 I have over forty fields on a form and in each textchanged proc for each field I have this code: Private Sub Field2_TextChanged(...) Handles Field2.TextChanged Field2.Text = Trim(Field2.Text) if Field2.Text.Length = 2 then Field3.Focus() End Sub Now, is there anyway where I can save myself the trouble of repeating the same concept. Basically, I want to check the length of the field, if it is two then go to the next field. Any ideas?? Appreciative for the time. Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
Administrators PlausiblyDamp Posted June 10, 2004 Administrators Posted June 10, 2004 You are probably best to wire the event handler up at runtime using the addhandler keyword. The sample below simply loops through all controls on the form in the form_load (if some are nested inside other controls then this will fail - reply and I'll show how to get that to work) and if it is a textbox then it sets the TextChanged handler to the other function. Inside the function it uses the sender object to identify which control raised the event. Private Sub TextField_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim t As TextBox t = DirectCast(sender, TextBox) t.Text = t.Text.Trim() If t.Text.Length = 2 Then Me.SelectNextControl(t, True, True, True, True) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim t As TextBox For Each c As Control In Me.Controls If TypeOf c Is TextBox Then t = DirectCast(c, TextBox) AddHandler t.TextChanged, AddressOf TextField_Changed End If Next End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
pelikan Posted June 10, 2004 Posted June 10, 2004 too bad you're using VB. With C# you could just add the same delegate(method) to each event, then find out which control fired the event (say a Tag property on the sender). Quote IN PARVUM MULTUM
jccorner Posted June 10, 2004 Author Posted June 10, 2004 Actually, I'm not using either. I'm using VR.Net. But I will try that, thanks and I'll let you know if I have any problems with the conversion. Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
*Experts* Nerseus Posted June 10, 2004 *Experts* Posted June 10, 2004 To expand on Plausably's solution, I'd offer a little more: Assuming you may have some textboxes where you *want* this behavior and some where you don't, try creating a new class/control that inherits from TextBox and has this event overriden to do what you want. On your form, use the new class type instead of TextBox. Also, I'm guessing that this is to enter a hex byte value (can't think of another reason to have a lot of textboxes that need only 2 chars and "auto-tab" to another box). If so, the new control type could not JUST check for 2 chars (and trim), but enforce it to be valid characters (0-9, A-F) and auto-uppercase the characters (a property on a Textbox that I don't remember offhand). By using the new type, you wouldn't have to hook up each event handler. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
jccorner Posted June 10, 2004 Author Posted June 10, 2004 Nerseus, actually, you are correct, upon adding the event handler, now my other textboxes only allow two chars. So now I need to use your idea I guess but my question is if I don't tie the event handler to the form, how can I distinguish between textboxes, one's that I want to perform the new event and ones that I want to act as they always do. And if you have one available, could you send me an example?? I'd greatly appreciate it. I wish to learn this technique because in most (pratically all my apps) on the gotfocus event, I change the backcolor to yellow and on the lostfocus I change it back to white, but it gets so repetitive to do that for every single textbox. But for this proj I need it for the textchanged event, but I will look around for an example on the web. Just wondered if you had one. Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
Administrators PlausiblyDamp Posted June 10, 2004 Administrators Posted June 10, 2004 If you do as Nerseus suggested in his post above and create a new type of control then the form wouldn't care which was which - the normal textboxes wouldn't handle the event and the new controls would have this 'event' handled internally. i.e. Public Class MyNewTextBox Inherits TextBox Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) Me.Text = Me.Text.Trim() If Me.Text.Length = 2 Then Me.Parent.SelectNextControl(Me, True, True, True, True) End If End Sub End Class If you create a new WindowsControlLibrary project and delete the usercontrol that is autocreated, create a new class and paste the above code into the file. If you build the project a DLL should be created, you can now open up your Windows form project and add your DLL to the toolbox (right click add remove items - browse to the DLL). You can then use this new textbox anywhere you need to implement the above logic. Also as Nerseus suggested - you may want to think about the logic a bit more / improve the validation depending on your needs. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jccorner Posted June 10, 2004 Author Posted June 10, 2004 Plausibly, thanks, I've been coding in VB so recently, that I forgot about creating new types using inheritance and the rules associated with them. Alright, that should work out great, thanks. I'll let you know if I run into anything else. Very appreciative of the time. Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
jccorner Posted June 19, 2004 Author Posted June 19, 2004 New problem. I'm able to get it to work for the most part. But I did realize something. I use panels every now and again and I found a interesting development. On one of the panels, I have several textboxes. Well when the form loads, the panel is not visible, so the textboxes on the panel are not included in the collection of me.controls. My question now is, how do I get the addhandler statements to execute when textboxes reside on a panel?? Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
Arch4ngel Posted June 21, 2004 Posted June 21, 2004 Well... a panels have a Controls collection no ? Same as the one from your form no ? Well... do you see where I'm going with this ? :) Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.