|
-
Jul 1st, 2003, 05:16 AM
#1
Thread Starter
New Member
Here is a brain teaser...
Ok I am kind of new at this but I could'nt find this anywhere else so I am hoping someone here can help me out. Lets say I have a textbox and a button that says hide along with a couple thousand labels on a form. I want to be able to put in Label24601 in the text box and hit the hide button and have it hide Label24601. As far as I know something like:
if textbox.text = "Label41" then label41.visible=false
would suffice. But due to the shear numbers and lack of a common nameing format amoung all the other labels that is not practicle. I thought I was on the right track with String.ConCat() but that turned out not to be what I am looking for. Any help would REALLY be apreciated!
-
Jul 1st, 2003, 06:00 AM
#2
Not elegant or efficient, but should work...
VB Code:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Name = TextBox1.Text Then
ctl.Hide
EndIf
Next
This world is not my home. I'm just passing through.
-
Jul 1st, 2003, 08:37 AM
#3
Addicted Member
Originally posted by trisuglow
Not elegant or efficient, but should work...
VB Code:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Name = TextBox1.Text Then
ctl.Hide
EndIf
Next
Yes, I don't think there is an elegant way finding a control by it's name. If speed was an issue, you could put all your label controls into your own collection when the form loads (see Collections.Specialized.NameObjectCollectionBase and Collections.Specialized.ListDictionary). You could identify the contol by its name then and this would be faster. Alternatively this might be a bit more efficient than trisglow's suggestion:
VB Code:
Dim ctl As label
For Each ctl In Me.Controls
If ctl.Name = TextBox1.Text Then
ctl.Hide
EndIf
Next
[/B][/QUOTE]
-
Jul 1st, 2003, 11:22 AM
#4
Thread Starter
New Member
Hey, it may not be elegant but it is a lot beter than how I was doing it. Thank you both soooooo much!
-
Jul 1st, 2003, 01:36 PM
#5
Thread Starter
New Member
Ok, well leme tel you how I did this now. After a lot of digging around MSDN about the NameObjectCollectionBase I think I have found a decent solution. I have made a class that interacts with the BaseAdd and BaseGet protected methods. And then in the form ther overridden New() sub now adds all the entries into my collection. This has the advantage over the Me.Controls collection because I add the labels from multiable forms and I can interact with the labels without having to loop through such a huge collection. Its not perfect but I think it should work Thanks!
-
Jul 1st, 2003, 10:20 PM
#6
I wonder how many charact
Lets say I have a textbox and a button that says hide along with a couple thousand labels on a form
That would be a ridiculous hypothetical solution. I hope its hypothetical, over 1000 labels on a form? Just since I'm curious, why would the user have to type in the name of the label to hide it? Couldn't you implement a right-click context menu and offer Hide as an option... you would know what label it was because its receiving the mouseclick...
Anyways, the best thing you could would be to inherit the control and add a public property that contains an identifiable value you could then loop through the control collection, inspecting that property for a match.
VB Code:
If myInheritedLabel.ID = 3
'or (you should be able to this without inheriting)
If String.Compare(myInheritedLabel.Name, "Label2605") Then 'hide it
Last edited by nemaroller; Jul 1st, 2003 at 10:30 PM.
-
Jul 2nd, 2003, 01:58 AM
#7
If you're going to use the For..Each solution, this addition's a no brainer:
VB Code:
For Each ctl In Me.Controls
If ctl.Name = TextBox1.Text Then
ctl.Hide
[b]Exit For[/b] ' no need to loop through the rest!
End If
Next
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jul 2nd, 2003, 02:49 AM
#8
Thread Starter
New Member
rofl, Indeed this would be a crazy situation. No, I in fact do not have some collection of forms with thousand of labels. This was just an easier way to word the qestion and get the responce I was looking for. In reality it would be more like a commandline built into an application that can interact with different methods, objects, and values of the actual application. I know it is kind of pointless having the "Imediant" window and all (I think thats still in vb.net?) but this is the way I want to be able to do my debuging. In my situation that dos'nt work very well because mot of the actual debuging and testing is done on a compiled version that does not have acces to VS
-
Jul 2nd, 2003, 06:37 AM
#9
I wonder how many charact
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
|