Results 1 to 13 of 13

Thread: [RESOLVED] Want to check all labels on form to see if Label name has certain string within it.

  1. #1

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Resolved [RESOLVED] Want to check all labels on form to see if Label name has certain string within it.

    I have a form that has a large number of labels. Is there a way I can move the focus from label to label. After I move the focus I'll check if the name of the label has a certain string in it. If it does then I'll leave the focus at that label. If it doesn't I'll move the focus to the next label and check if that label name has the string in it. I just need to know how to move the focus from label to label. I was trying SelectNextControl without success. Maybe I can tab programmatically to the next label but I don't know how to do that.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Want to check all labels on form to see if Label name has certain string within i

    Hey,

    Have you seen jm's post here:

    http://www.vbforums.com/showthread.php?t=387308

    I think this might help you. Check each control to see if it is a Label, and if it is, do the check that you need.

    Hope that helps!!

    Gary

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Want to check all labels on form to see if Label name has certain string within i

    try this:

    vb Code:
    1. for each ctrl as control in me.controls
    2.     ctrl.focus
    3.     if ctrl.name.contains("string") then exit for
    4. next

  4. #4
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Want to check all labels on form to see if Label name has certain string within i

    You cant select labels with selectnextcontrol...

    You can do something like:
    vb.net Code:
    1. For Each ctrl As Control In Me.Controls
    2.             If TypeOf ctrl Is Label Then
    3.                 Me.ActiveControl = ctrl
    4.                 MsgBox(Me.ActiveControl.Name)
    5.             End If
    6.         Next

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Want to check all labels on form to see if Label name has certain string within i

    Hey,

    Am I not right in saying that to use each of the above approaches, you would also need to recurse into each control to see whether it had additional child controls? i.e. if there is a Panel on the form.

    That is why I suggested jm's approach, as this recursion is not needed.

    Gary

  6. #6

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Want to check all labels on form to see if Label name has certain string within i

    Thanks for the help. .paul your code works perfectly. Perfectly that is the first time but for some reason if I close the form and change the string to something else and then reopen the form it goes to the first result every time. I tried setting ctrl = nothing before closing the form but it doesn't change anything. Not sure why. I'm putting the code in the form load sub.

    I've checked to see that the string is changing and it is but once the form opens at a certain position going to the correct label it keeps on opening at the same position even though the string is changing to something that is found in some other label name for that form. I looked at jmc's code and I get the same problem using his method.

    I tried setting the focus to the first label on the form before running the code but it doesn't fix the problem.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Want to check all labels on form to see if Label name has certain string within i

    delete your binaries + rebuild

    thats your bin/debug folder

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Want to check all labels on form to see if Label name has certain string within i

    if that didn't work...
    what are the names of the 2 controls + what are you searching for?

  9. #9

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Want to check all labels on form to see if Label name has certain string within i

    OK. I tried deleting both the bin and obj(has debug folder in it) folders and did a rebuild and the problem is still there.

    On a particular first trial the string st5 = "Dph". I open the form with all the labels and it goes to LabelDph just like it should. I close the form and click in a different combobox ComboBoxBph. st5 = "Bph" now since I use :
    Code:
       st5 = Me.ActiveControl.Name
       st5 = st5.Replace("ComboBox", "")
    I reopen the form with all the labels and it still goes to LabelDph and not LabelBph which is way above it on the Form.

    I used st6 to see what ctrl.Name is and it's LabelDph in the first case and LabelBph in the second case even though the form is opening at the LabelDph position in both cases.

    Code:
       For Each ctrl In Me.Controls
            ctrl.Focus()
            If ctrl.Name.Contains(st5) Then
                 st6 = ctrl.Name
                 Exit For
            End If
       Next
    If I could make the second, third, etc. opening of the form respond like the first then I'd be ok.

    If I click in ComboBoxBph first and then open the form with all the labels then it will open at LabelBph. If I then closed the form clicked in ComboBoxDph and then opened the form it would open at LabelBph again. It always gets the first opening of the form right and it always keeps the same position at all openings of the form after that until I close my application and start it up again.
    Last edited by EntityX; Dec 19th, 2009 at 12:56 PM.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  10. #10
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Want to check all labels on form to see if Label name has certain string within i

    I'm a little bit confused...

    You're trying to change the label names at runtime, and expect the second time you open the form the name it's the new one?
    When you say close the form, you say close the application?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  11. #11

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Want to check all labels on form to see if Label name has certain string within i

    I created a new project from scratch. Made it very simple. 2 labels on the form that opens and I had the same problem even when using different code. I tried using Me.AutoScrollPosition = New Point(0, 1100) and Me.AutoScrollPosition = New Point(0, 0). And it would again open at the proper position the first time but would keep opening at the same position after that. I traced it to using Form2.ShowDialog()instead of using Form2.Show(). If I use Show instead of ShowDialog then the problem disappears. Thanks for your help everyone.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Want to check all labels on form to see if Label name has certain string within i

    try changing this:

    vb Code:
    1. If ctrl.Name.Contains(st5) Then

    to:

    vb Code:
    1. if typeof ctrl is label andalso ctrl.Name.Contains(st5) Then

  13. #13

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Want to check all labels on form to see if Label name has certain string within i

    An explanation for mickey pt :

    The second form in my large application was a form to explain the settings that are seen on a form that has numerous ComboBoxes. So I have ComboBoxBph and ComboBoxDph, etc, etc, on one form and then you click a button Explain Settings and another form opens with explanations of the settings that you change with the ComboBoxes. So LabelBph on form ExplainSettings would explain setting Bph which is alterred using ComboBoxBph which is on the first form and not on the form that has the explanations.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

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