Results 1 to 12 of 12

Thread: [RESOLVED] How to find control by tag property

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Resolved [RESOLVED] How to find control by tag property

    Hi,
    Is there a way to find a control by tag text without looping through the controls of the container?
    I usually fidn controls by ID/name by:

    Code:
    Dim Thiscontrol As Control =MyPanel.Controls.Find("TXTbox_CaseID", True)(0)
    But can we find a control by tag in such method?

    Thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to find control by tag property

    Code:
    Thiscontrol As Control =MyPanel.Controls("TXTbox_CaseID")

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: How to find control by tag property

    Will that consider the control's tag?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to find control by tag property

    What exactly does the tag contain? Why mention the name of the textbox if you want to find by tag?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How to find control by tag property

    Is the tag changing? If not, then I assume that you put something in the tag. At the same time, you could put the control into a Dictionary(of String,Control), with the key being the tag. That would be a particularly fast lookup, but it would require that the tags all be unique (I assume they are, or any other search won't work, either), and it gets to be a nuisance if the tags are changing.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: How to find control by tag property

    The tag is just some string text.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: How to find control by tag property

    Quote Originally Posted by .paul. View Post
    What exactly does the tag contain? Why mention the name of the textbox if you want to find by tag?
    The tag is just some string text.
    And it is because I have a few panels where I have some combo on each and those specific combos all have the same tag but only one at the time is visible depending on the user's selection.
    So, I need to know which of those combos I need to access. I know which panel is visible at time, just need to know which combo is it that I need to populate. They have different names obviously but the tag is the same.
    So, my idea was to look for the combo on that visible panel that has specific tag and populate it. Of course I could also loop through the controls onthe visibel panel and find that combo with the specific tag but I thought maybe I just could fidn the control as mentioned above.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: How to find control by tag property

    The tag is constant.
    So, lets say I have 10 panels and they all have, among other things, 4 combos that I need to populate differently based on the user selection and some other ramifications.
    There are 4 unique tags that I assigned one to each of those combos on each panels.

    Maybe my design is stupid and have to rethink it. I will check that dictionary thing.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to find control by tag property

    Code:
    Dim Thiscontrol As Control = MyPanel.Controls.OfType(Of ComboBox).Where(Function(cb) cb.Tag = yourTagVariable).First

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: How to find control by tag property

    Quote Originally Posted by .paul. View Post
    Code:
    Dim Thiscontrol As Control = MyPanel.Controls.OfType(Of ComboBox).Where(Function(cb) cb.Tag = yourTagVariable).First
    Thanks so much It does exactly what I was looking for.

    Thanks again.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] How to find control by tag property

    Actually, you can change that…

    Dim Thiscontrol As ComboBox = ‘etc…

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] How to find control by tag property

    There's no need to call Where and then First. The First and similar methods allow you to specify a filter:
    vb.net Code:
    1. Dim Thiscontrol As ComboBox = MyPanel.Controls.OfType(Of ComboBox).First(Function(cb) cb.Tag = yourTagVariable)

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