[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.
Re: How to find control by tag property
Code:
Thiscontrol As Control =MyPanel.Controls("TXTbox_CaseID")
Re: How to find control by tag property
Will that consider the control's tag?
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?
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.
Re: How to find control by tag property
The tag is just some string text.
Re: How to find control by tag property
Quote:
Originally Posted by
.paul.
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.
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.
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
Re: How to find control by tag property
Quote:
Originally Posted by
.paul.
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.
Re: [RESOLVED] How to find control by tag property
Actually, you can change that…
Dim Thiscontrol As ComboBox = ‘etc…
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:
Dim Thiscontrol As ComboBox = MyPanel.Controls.OfType(Of ComboBox).First(Function(cb) cb.Tag = yourTagVariable)