Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Control Name the user is focused on 1

Status
Not open for further replies.

wakkara

Programmer
Jun 15, 2005
31
MX
Hi there...Im trying to make a routine or function that acomplishes this:

if the user gets focus on a text box, and keypress a key (Lets say a dash (/)) with the keybord, the text box the user is focused on, change its height to 250.

Is there any way to acomplish this by not having to code sub's for 150 textboxes.

Ive been reading quite a bit of this issue...and as far as i got is this code with help of this thread thread222-1364428.

****************************************************
Dim sClip As String
Dim sObj As String
Dim sSub As String
Dim sOrig As String
Dim ctl As Control

Clipboard.Clear

sOrig = "Private Sub XXX_GotFocus()"

For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
sObj = ctl.Name
sSub = Replace(sOrig, "XXX", ctl.Name)
sSub = sSub & vbCrLf & ctl.Name & ".Height = 250 " & vbCrLf & "End Sub" & vbCrLf & vbCrLf
sClip = sClip & sSub
End If
Next

Clipboard.SetText sClip

MsgBox "The code for all the textbox GotFocus events is in the Clipboard"
****************************************************

Ok so far looks great, but i just have the plaintext of the routines on the clipboard.....

Any suggestions? please
 
Thanks man you have a star.

And for those that are looking for something similar. This is what i did.


2 Procedures

*******************************************************
------------------------------------------------------
Public Sub ExpandirText()
If TypeOf Screen.ActiveControl Is TextBox Then
If Screen.ActiveControl.Height = 375 Then
Screen.ActiveControl.Height = 1095
End If

End If
End Sub
------------------------------------------------------
Public Sub ContraerText()
If TypeOf Screen.ActiveControl Is TextBox Then
If Screen.ActiveControl.Height = 1095 Then
Screen.ActiveControl.Height = 375
End If

End If
End Sub
------------------------------------------------------

*******************************************************
On the KeyDown Sub of the FORM


*******************************************************
------------------------------------------------------
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyAdd And Shift = 2 Then
Call ExpandirText
End If

If KeyCode = vbKeySubtract And Shift = 2 Then
Call ContraerText
End If

End Sub
------------------------------------------------------
*******************************************************


On the Load Sub of the Form (Really dont know why but with out this it wont work)



*******************************************************
Private Sub Form_Load()
KeyPreview = True
End Sub
*******************************************************


Everytime the Keyboard combination key is Ctrl + (+) the active texbox will make bigger.

And Everytime the Keyboard combination is are Ctrl + (-) the active texbox will make smaller.

Stupid right?, but it really driving me crazy.
 
<Really dont know why but with out this it wont work
You can also set this in the properties window of the form, rather than doing it from code. KeyPreview simply means that keystrokes that you put into a control are first handled by the form's event handlers and then by the control's.

I would also suggest that, rather than hard-coding the numbers for the larger and smaller numbers, that you consider using relative values. This way, the actual screen resolution doesn't matter. You can either code something like "make the textbox 3 times as big" or "make the textbox some fraction of Screen.Height," in your Expandirtext routine. To see why I'm saying this, you might wish to experiment with running your program as it stands with various screen settings and seeing how it works for you.

Bob
 
If you want to know how to trap all the lostfocus and gotfocus events on a form, so long as you aren't using control arrays, let me know. Their is a way of doing this, but it's a little involved and not someting I want to go into if you're not interested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top