Results 1 to 9 of 9

Thread: Here is a brain teaser...

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    4

    Angry 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!

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Not elegant or efficient, but should work...

    VB Code:
    1. Dim ctl As Control
    2.         For Each ctl In Me.Controls
    3.             If ctl.Name = TextBox1.Text Then
    4.                 ctl.Hide
    5.             EndIf
    6.         Next
    This world is not my home. I'm just passing through.

  3. #3
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    Originally posted by trisuglow
    Not elegant or efficient, but should work...

    VB Code:
    1. Dim ctl As Control
    2.         For Each ctl In Me.Controls
    3.             If ctl.Name = TextBox1.Text Then
    4.                 ctl.Hide
    5.             EndIf
    6.         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:
    1. Dim ctl As label
    2.         For Each ctl In Me.Controls
    3.             If ctl.Name = TextBox1.Text Then
    4.                 ctl.Hide
    5.             EndIf
    6.         Next
    [/B][/QUOTE]

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    4

    Thumbs up

    Hey, it may not be elegant but it is a lot beter than how I was doing it. Thank you both soooooo much!

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    4
    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!

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. If myInheritedLabel.ID = 3
    2.  
    3. 'or (you should be able to this without inheriting)
    4.  
    5. If String.Compare(myInheritedLabel.Name, "Label2605") Then 'hide it
    Last edited by nemaroller; Jul 1st, 2003 at 10:30 PM.

  7. #7
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    If you're going to use the For..Each solution, this addition's a no brainer:
    VB Code:
    1. For Each ctl In Me.Controls
    2.             If ctl.Name = TextBox1.Text Then
    3.                 ctl.Hide
    4.                 [b]Exit For[/b] ' no need to loop through the rest!
    5.             End If
    6.         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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    4
    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

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Talking

    Cool...

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