Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 12th, 2003, 08:55 AM
Registered User
 
Join Date: Sep 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Natalie
Default Controls

I have a problem that may be simple, but I just cant think of the solution off the top of my head. ANY help most appreciated!! :D

I have loads of text boxes on one form which I am setting the TabIndex for at runtime. The specification I am working to states that when the "Tab" is clicked and the next text box gains focus, the text that is in that textbox needs to be highlighted.

At present I have:
Private Sub Text1_GotFocus()

Text1.SelLength = Len(Text1.Text)

End Sub

But this seems tedious to just keep re-using this code for every text box.

Is there anyway I can trap the keypress at the form level and execute the same piece of code for each text box??

I hope this makes sense. Thanks in advance!!
 
Old September 12th, 2003, 09:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

It sure is tedious isn't it :)

2 ways I can think of OTMH
1. make your text boxes a control array (i.e. give them all the same name), then you only have to write the code once.
Code:
Private Sub Text1_GotFocus(Index As Integer)
    Text1(Index).SelStart = 0
    Text1(Index).SelLength = Len(Text1(Index).Text)
End Sub
The downside of this is that you have to use the longer Text1(Index) notation every time you want to refer to an individual text box.
2. create your own ActiveX control containing a single text box with the code to highlight the contents in the GotFocus method, then use this control instead of the standard text box. If you've never written a control before, take a look at the Active control interface wizard...

hth
Phil
 
Old September 12th, 2003, 09:46 AM
Registered User
 
Join Date: Sep 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Natalie
Default

So how do you actually create this control?? I'm confused. That sounds the better option but I wouldnt know where to start!!

Thanks in advance for any help!!

 
Old September 12th, 2003, 10:02 AM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The 2 method that Phil propose are good. The creation of an ActiveX control can be a lot of coding and create unexpected error in your App. You could also use a class that will trap the event and execute the code.

Take this example:
Code for the class TextSelect
Code:
Option Explicit

Private WithEvents m_txtBox As TextBox

Private Sub Class_Terminate()
  Set m_txtBox = Nothing
End Sub

Private Sub m_txtBox_GotFocus()
  m_txtBox.SelLength = Len(m_txtBox.Text)
End Sub

Private Sub m_txtBox_LostFocus()
  m_txtBox.SelLength = 0
End Sub

Public Sub Init(ByRef txtBox As TextBox)
  Set m_txtBox = txtBox
End Sub
Code to use on the form
Code:
Option Explicit

Private m_collTextBox As Collection

Private Sub Form_Load()
  Dim objControl As Control
  Dim objSelect  As TextSelect

  Set m_collTextBox = New Collection

  For Each objControl In Me.Controls
    If TypeOf objControl Is TextBox Then
      Set objSelect = New TextSelect
      Call objSelect.Init(objControl)
      Call m_collTextBox.Add(objSelect)
    End If
  Next
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Do While m_collTextBox.Count > 0
    Call m_collTextBox.Remove(1)
  Loop
End Sub
Stéphane Lajoie
 
Old September 12th, 2003, 10:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well, you start with File > New Project > ActiveX Control.

This forum is hardly the place for a tutorial on how to create controls, so here's a couple I found on the web:
http://pages.cpsc.ucalgary.ca/~saul/...activex01.html
http://www.developer.com/net/vb/arti...0926_1539541_1
 
Old September 12th, 2003, 10:13 AM
Registered User
 
Join Date: Sep 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Natalie
Default

Sorry to be a pain....

Stephanie. Thank you very much for giving me that example. It worked perfectly and means that I dont have to change any of the existing layout and code!!

Thank you very much to everyone that has helped!!

 
Old September 12th, 2003, 10:27 AM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Glad to help :D

Just forgot to say. Never forget to put the code in the Form_Unload event or your form will stay loaded.

Stéphane Lajoie





Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
Retain the value of Controls S_Dogra ASP.NET 1.0 and 1.1 Professional 1 January 31st, 2007 01:21 PM
Binding controls Boris Kofman VB Databases Basics 1 January 20th, 2007 01:16 PM
Using controls AllenJ .NET Framework 2.0 1 July 31st, 2006 05:01 AM
Controls dparsons VB.NET 2002/2003 Basics 1 October 19th, 2005 11:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.