|
-
Apr 29th, 2017, 11:50 AM
#1
Thread Starter
PowerPoster
[RESOLVED] How to get index of textbox array element clicked on?
Hi there folks! I am working a program with 31 text box arrays with student data. The array is called StudentNameTXT and the array goes from 0 to 30. What I would like to find out is how to get the index of the textbox I clicked on in a msgbox.
So far I have tried to set up an array, but I can only think of the text box change event, which wouldn't be what I'm looking in this scenario, I don't want to have to change the text, I just would like the index in a msgbox.
Your expertise is always appreciated!
Thanks
I just wanted to clarify, there are not 31 different arrays, just one array with 31 elements in it. : )
Last edited by Justin M; Apr 29th, 2017 at 11:59 AM.
-
Apr 29th, 2017, 11:59 AM
#2
Re: How to get index of textbox array element clicked on?
Textbox's SetFocus event would probably be better. VB also has a ActiveControl property which might be useful
-
Apr 29th, 2017, 12:15 PM
#3
Thread Starter
PowerPoster
Re: How to get index of textbox array element clicked on?
Ahh ok, so perhaps..
VB Code:
Private Sub StudentNameTXT_SetFocus(Index As Integer)
With StudentNameTXT(Index)
'Get the index
Select Case Index
Case 0
'STUDENT 1
Case 1
'STUDENT 2
Case 2
'STUDENT 3
Case 3
'STUDENT 4
Case 4
'STUDENT 5
Case 5
'STUDENT 6
Case 6
'STUDENT 7
Case 7
'STUDENT 8
Case 8
'STUDENT 9
Case 9
'STUDENT 10
Case 10
'STUDENT 11
Case 11
'STUDENT 12
Case 12
'STUDENT 13
Case 13
'STUDENT 14
Case 14
'STUDENT 15
Case 15
'STUDENT 16
Case 16
'STUDENT 17
Case 17
'STUDENT 18
Case 18
'STUDENT 19
Case 19
'STUDENT 20
Case 20
'STUDENT 21
Case 21
'STUDENT 22
Case 22
'STUDENT 23
Case 23
'STUDENT 24
Case 24
'STUDENT 25
Case 25
'STUDENT 26
Case 26
'STUDENT 27
Case 27
'STUDENT 28
Case 28
'STUDENT 29
Case 29
'STUDENT 30
Case 30
'STUDENT 31
End Select
End With
End Sub
Then to get the index, would under each case, would I just go msgbox "0", 1, etc?
-
Apr 29th, 2017, 12:17 PM
#4
Re: How to get index of textbox array element clicked on?
That event has an Index property. Wouldn't your student index simply be: Index + 1 ?
-
Apr 29th, 2017, 12:24 PM
#5
Thread Starter
PowerPoster
Re: How to get index of textbox array element clicked on?
Ok, I'd hate to bother you, but could you show me what that would look like?
-
Apr 29th, 2017, 12:45 PM
#6
Re: How to get index of textbox array element clicked on?
Code:
Private Sub StudentNameTXT_SetFocus(Index As Integer)
MsgBox "Student " & Index + 1
End Sub
Maybe I didn't understand your original question?
-
Apr 29th, 2017, 12:56 PM
#7
Thread Starter
PowerPoster
Re: How to get index of textbox array element clicked on?
Oh no you read it perfect, I might not be providing all necessary info.
I just tried your code, but I didn't get a msgbox pop up. Is anything I should have mentioned? That the text boxes already have text in them? Is there a property I need to set.
When I go to double click on any textbox, I get the change event if that is important.
-
Apr 29th, 2017, 01:19 PM
#8
Re: How to get index of textbox array element clicked on?
I think we may need to understand how your textbox events are coded. Double clicking on a textbox should not trigger a Changed event. Setting focus to a textbox should trigger the code in the SetFocus event.
For example, in a new test project add a form with an array of three textboxes. Spread them out a bit and add this in the Changed event for the textboxes: Debug.Print "changed". Run your project and double click to your heart's content. You shouldn't be seeing anything in the immediate window related to those clicks. Now add this in the SetFocus event: Debug.Print "got focus". Run project again and click/tab to the textboxes, now you should see stuff in the immediate window & double clicking still should not produce a Changed event.
I'm thinking you have other code in your other events that is causing your problems. But that's just a guess.
Edited: I mess you up. It's GetFocus not SetFocus
Last edited by LaVolpe; Apr 29th, 2017 at 01:50 PM.
-
Apr 29th, 2017, 01:32 PM
#9
Thread Starter
PowerPoster
Re: How to get index of textbox array element clicked on?
Here I will attach a test form with the code you just sent. I hope I am not sending a confusing message over what I'm trying to do with the text boxes. I just want it so when I click on a textbox in that array, it will tell me what the index of that element is.Test1.zip
-
Apr 29th, 2017, 01:38 PM
#10
Re: How to get index of textbox array element clicked on?
Jeez -- might fault. In my first reply, I said "SetFocus". My brain was on API-mode. In VB, it's "GotFocus"
Sorry
-
Apr 29th, 2017, 01:40 PM
#11
Re: How to get index of textbox array element clicked on?
Hey Justin,
I don't know if this'll help or not, but it's a little trick I sorted out many moons ago.
Code:
Option Explicit
Private Declare Function GetFocus Lib "user32" () As Long
Private Sub Form_Click()
MsgBox IndexWithinControlArrayWithFocus(Text1)
End Sub
Public Function IndexWithinControlArrayWithFocus(ctl As Object) As Long
' Whatever type control array is passed, it must have hWnd property.
' Returns -1 if none found.
Dim o As Object
'
For Each o In ctl
If o.hWnd = GetFocus Then
IndexWithinControlArrayWithFocus = o.Index
Exit Function
End If
Next o
IndexWithinControlArrayWithFocus = -1
End Function
To test it, I just started a default project, threw a Text1 on it and then copied it a few times (making a control array). To test, just click anywhere on the form, and it'll report the Text1 textbox index property that has the focus.
Maybe it'll help.
Enjoy,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Apr 29th, 2017, 01:45 PM
#12
Re: How to get index of textbox array element clicked on?
Here are a couple of somewhat related functions that might also come in handy for you:
Code:
Public Function bObjectIsControlArray(obj As Object) As Boolean
Dim o As Object
On Error GoTo NotControlArray
If TypeName(obj) = "Object" Then
For Each o In obj
If TypeOf o Is Control Then bObjectIsControlArray = True
Exit Function ' We only need to test the first one.
Next o
End If
NotControlArray: ' Just in case we try to loop through something that won't loop.
End Function
Private Function bControlIsInArray(TheControl As Control) As Boolean
Dim i As Long
On Error GoTo NotInArray
i = TheControl.Index
bControlIsInArray = True
NotInArray:
End Function
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Apr 29th, 2017, 01:53 PM
#13
Thread Starter
PowerPoster
Re: How to get index of textbox array element clicked on?
Oh this is fantastic. Thank you all for your help!! : )
-
Apr 29th, 2017, 02:01 PM
#14
Re: [RESOLVED] How to get index of textbox array element clicked on?
okay....the simple explanation without all that 'stuff', is to see what INDEX is of the textbox upon which you clicked, yes?
So simple (as suggested in 'GotFocus' event----but you can always get focus by using arrow keys to a control), use the CLICK event of the textbox (as this is what you originally asked)...simply:
Code:
Private Sub StudentNameTXT_Click(Index As Integer)
Debug.Print (Index + 1)
End Sub
-
Apr 29th, 2017, 02:04 PM
#15
Re: [RESOLVED] How to get index of textbox array element clicked on?
Hey Sam,
I thought about giving that answer too, but I thought that that had to be too obvious. 
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Apr 29th, 2017, 02:06 PM
#16
Re: [RESOLVED] How to get index of textbox array element clicked on?
Well guys, if I didn't screw him up by using the term SetFocus vs GetFocus, I think this would've been a 1-post solution. Personally, I think using the Click event for most 'input controls' doesn't consider the dinosaur that uses the keyboard to select controls. Just my two cents.
-
Apr 29th, 2017, 02:15 PM
#17
Re: [RESOLVED] How to get index of textbox array element clicked on?
 Originally Posted by Elroy
Hey Sam,
I thought about giving that answer too, but I thought that that had to be too obvious.
Elroy
Figure ya did.....it IS obvious. To ME anyway.
-
Apr 29th, 2017, 02:16 PM
#18
Re: [RESOLVED] How to get index of textbox array element clicked on?
 Originally Posted by LaVolpe
Well guys, if I didn't screw him up by using the term SetFocus vs GetFocus, I think this would've been a 1-post solution. Personally, I think using the Click event for most 'input controls' doesn't consider the dinosaur that uses the keyboard to select controls. Just my two cents.
Worth it. ~smile~....
No, you are correct...all depends on how the OP wants his program to react that is all. Does he want getting focus to be the trigger, or a mouse-click?
-
Apr 29th, 2017, 02:17 PM
#19
Re: [RESOLVED] How to get index of textbox array element clicked on?
Call me "Barney"...hate using arrow keys...IDOC is the old mouse....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|