Harut Posted October 25, 2005 Report Share Posted October 25, 2005 when a text box gets the focus in the application, written in vb6, the text inside needs to be selected... it's very easy task to do... but... i have some 100 of them, if not more... and don't want to be writing an event for every single one of them... (even if that's just a one-line coding per event) is there any way to write an event and run it for all the text box controls in the project? thanks Quote Link to comment Share on other sites More sharing options...
Sip Posted October 25, 2005 Report Share Posted October 25, 2005 Yah .. it's been a long time I have done this but can't you put them in an array with an index? I seem to remember you can do this in VB. Quote Link to comment Share on other sites More sharing options...
Harut Posted October 25, 2005 Author Report Share Posted October 25, 2005 (edited) hmm... well, no way i'm creating a control array... i have some 100 text boxes already setup with descriptive names and 1000s of lines of code... turning them into control array is just too much hastle... hmm... hold on... Edited October 25, 2005 by Harut Quote Link to comment Share on other sites More sharing options...
Sip Posted October 25, 2005 Report Share Posted October 25, 2005 So you basically want to go through all your source and add some code to all the textbox codes, right? If you can find a common pattern to all of them, you could use the automatic search and replace feature of some text editors (like ultra edit) that go through a whole file (or directory of files) and replace it in such a way that it keeps the old code and adds what you want to add. But at this point, I am not of much help because I don't know what your code looks like and I don't know what you need to add to all. Quote Link to comment Share on other sites More sharing options...
Harut Posted October 25, 2005 Author Report Share Posted October 25, 2005 i guess i'm just better off copy-pasting the gotfocus event for each textbox control... Quote Link to comment Share on other sites More sharing options...
Garo Posted October 25, 2005 Report Share Posted October 25, 2005 I'm not sure about VB but in Delphi/C++ there's a property that controls whether to automatically select all the text inside the control when it gets the focus or not. If there's no such property in VB for this control then you can create an even handler for OnFocus event for one text box and then assign it to all controls on your form that have the same class name by scanning form's components array in form's OnCreate event. 4-5 lines of code will take care of this whole thing. Quote Link to comment Share on other sites More sharing options...
Harut Posted October 25, 2005 Author Report Share Posted October 25, 2005 I'm not sure about VB but in Delphi/C++ there's a property that controls whether to automatically select all the text inside the control when it gets the focus or not. If there's no such property in VB for this control then you can create an even handler for OnFocus event for one text box and then assign it to all controls on your form that have the same class name by scanning form's components array in form's OnCreate event. 4-5 lines of code will take care of this whole thing. that's all fine, but i'm not sure if i can add event handles during run-time... i'll check it out... Quote Link to comment Share on other sites More sharing options...
Harut Posted October 26, 2005 Author Report Share Posted October 26, 2005 ok, got it, thanks Quote Link to comment Share on other sites More sharing options...
Harut Posted October 27, 2005 Author Report Share Posted October 27, 2005 if anybody is interested, here is the code i used (based on a code someone else provided) created a separate class module named clsHandler with the following code... Public WithEvents m_TextBox As TextBox Private Sub m_TextBox_GotFocus() Call GotTextBoxFocus(m_TextBox) End Sub then created the following 2 public subs in a code module... Public Sub GotTextBoxFocus(ByRef txtBox As TextBox) txtBox.SelStart = 0 txtBox.SelLength = Len(txtBox.Text) End Sub Public Sub InitGotTextBoxFocus(frmForm As Form) Dim ctrl As Control Dim newHandler As clsHandler For Each ctrl In frmForm.Controls If TypeName(ctrl) = "TextBox" Then Set newHandler = New clsHandler Set newHandler.m_TextBox = ctrl arrHandler.Add newHandler End If Next ctrl End Sub then i call the InitGotTextBoxFocus sub from load event of each form passing the form as a param... Quote Link to comment Share on other sites More sharing options...
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.