Results 1 to 19 of 19

Thread: [RESOLVED] How to get index of textbox array element clicked on?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [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.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How to get index of textbox array element clicked on?

    Ahh ok, so perhaps..

    VB Code:
    1. Private Sub StudentNameTXT_SetFocus(Index As Integer)
    2. With StudentNameTXT(Index)
    3.  
    4. 'Get the index
    5. Select Case Index
    6.  
    7. Case 0
    8. 'STUDENT 1
    9.  
    10.  
    11. Case 1
    12. 'STUDENT 2
    13.  
    14.  
    15. Case 2
    16. 'STUDENT 3
    17.  
    18. Case 3
    19. 'STUDENT 4
    20.  
    21.  
    22. Case 4
    23. 'STUDENT 5
    24.  
    25. Case 5
    26. 'STUDENT 6
    27.  
    28.  
    29. Case 6
    30. 'STUDENT 7
    31.  
    32.  
    33. Case 7
    34. 'STUDENT 8
    35.  
    36.  
    37. Case 8
    38. 'STUDENT 9
    39.  
    40.  
    41. Case 9
    42. 'STUDENT 10
    43.  
    44.  
    45. Case 10
    46. 'STUDENT 11
    47.  
    48.  
    49. Case 11
    50. 'STUDENT 12
    51.  
    52.  
    53. Case 12
    54. 'STUDENT 13
    55.  
    56.  
    57. Case 13
    58. 'STUDENT 14
    59.  
    60.  
    61. Case 14
    62. 'STUDENT 15
    63.  
    64. Case 15
    65. 'STUDENT 16
    66.  
    67.  
    68. Case 16
    69. 'STUDENT 17
    70.  
    71.  
    72. Case 17
    73. 'STUDENT 18
    74.  
    75.  
    76. Case 18
    77. 'STUDENT 19
    78.  
    79.  
    80. Case 19
    81. 'STUDENT 20
    82.  
    83.  
    84. Case 20
    85. 'STUDENT 21
    86.  
    87.  
    88. Case 21
    89. 'STUDENT 22
    90.  
    91.  
    92. Case 22
    93. 'STUDENT 23
    94.  
    95.  
    96. Case 23
    97. 'STUDENT 24
    98.  
    99.  
    100. Case 24
    101. 'STUDENT 25
    102.  
    103.  
    104. Case 25
    105. 'STUDENT 26
    106.  
    107.  
    108. Case 26
    109. 'STUDENT 27
    110.  
    111.  
    112. Case 27
    113. 'STUDENT 28
    114.  
    115. Case 28
    116. 'STUDENT 29
    117.  
    118.  
    119. Case 29
    120. 'STUDENT 30
    121.  
    122.  
    123. Case 30
    124. 'STUDENT 31
    125.  
    126.  
    127. End Select
    128. End With
    129.  
    130. End Sub

    Then to get the index, would under each case, would I just go msgbox "0", 1, etc?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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 ?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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?

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    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.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    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.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How to get index of textbox array element clicked on?

    Oh this is fantastic. Thank you all for your help!! : )

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    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

  15. #15
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    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.

  16. #16
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    Re: [RESOLVED] How to get index of textbox array element clicked on?

    Quote Originally Posted by Elroy View Post
    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.

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    Re: [RESOLVED] How to get index of textbox array element clicked on?

    Quote Originally Posted by LaVolpe View Post
    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?

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    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
  •  



Click Here to Expand Forum to Full Width