Results 1 to 11 of 11

Thread: [RESOLVED] [2005] Testing whether a control exists

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Resolved [RESOLVED] [2005] Testing whether a control exists

    Is there some easy way to determine if a control exists? The problem I have is that I create an array of controls (labels for example) that gets re-dim'd from time to time. Id like to be able to say, if MyLabel(35).Exists then do something.

    I have been using;
    If MyPanel.Contains(MyLabel(35)) Then do something

    but this craps out if the index is not valid, even if I wrap it in a try/catch.

  2. #2
    New Member
    Join Date
    Oct 2006
    Posts
    5

    Re: [2005] Testing whether a control exists

    Could use a try catch like...

    VB Code:
    1. Try
    2.  mycontrol.left = 10
    3. catch
    4.  msgbox("Control does not exist")
    5. End try

    If the control does not exist then trying to set the left parameter would raise an error.

    Hope this helps

    EDIT: Sorry, I missed the last bit of your post. Ignore what I just said

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Testing whether a control exists

    Less resources being used to just test it with the TypeOf eval.
    VB Code:
    1. If TypeOf MyLabel(35) Is Label Then
    2.     If MyPanel.Contains(MyLabel(35)) Then
    3.         'Meow!
    4.     End If
    5. End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Testing whether a control exists

    Unfortunately a catch/try does not catch an exception if the index is invalid.

  5. #5
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: [2005] Testing whether a control exists

    VB Code:
    1. For X = 0 To fromForm.Controls.Count - 1
    2. If fromForm.Controls(X).ToString = "DropDownMessage.DropDownMsg" Then
    3. fromForm.Controls(X).Dispose()
    4. Exit For
    5. End If
    6. Next

    That's what I did. I created a nifty control that does drop down messages from the bottom of another control. To check if it exists, I check that it's type (to string) = "whatever" and leave the loop when done...

    fromForm is the form...BTW...
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Testing whether a control exists

    Quote Originally Posted by warrenayen
    VB Code:
    1. For X = 0 To fromForm.Controls.Count - 1
    2. If fromForm.Controls(X).ToString = "DropDownMessage.DropDownMsg" Then
    3. fromForm.Controls(X).Dispose()
    4. Exit For
    5. End If
    6. Next

    That's what I did. I created a nifty control that does drop down messages from the bottom of another control. To check if it exists, I check that it's type (to string) = "whatever" and leave the loop when done...

    fromForm is the form...BTW...
    VB Code:
    1. If TypeOf(fromForm.Controls(X)) Is DropDownMessage.DropDownMsg
    would probably be better than checking against a string value. If the name every changes, this method would cause a syntax error and let you know right away. Using a string comparison leaves room for error.

    This just goes for your example code, as far as the original thread poster, I would also do what RobDog suggested.
    Last edited by kleinma; Oct 27th, 2006 at 03:21 PM.

  7. #7
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: [2005] Testing whether a control exists

    No, that's not the name, that is the type, to a string. Everything came up as TextBox or Combobox or Label...

    Still, you're probably right. At the time I wrote that code over a year ago I couldn't seem to get it to check the type properly, so I gave up and made it a string...
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Testing whether a control exists

    Quote Originally Posted by warrenayen
    No, that's not the name, that is the type, to a string. Everything came up as TextBox or Combobox or Label...

    Still, you're probably right. At the time I wrote that code over a year ago I couldn't seem to get it to check the type properly, so I gave up and made it a string...
    Yeah I meant name as in the name of the TYPE, not the name of the variable.

    You never know when you are going to rework your namespaces to make things more organized, only to have it break because of some string check like that. Its happened to me, which is why I figured I would point it out. May come in handy to others.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Testing whether a control exists

    You guys leave me about five laps behind on this stuff, here's an example of what Im trying to accomplish. Rather than keep tabs on the label indices, I thought it was neater to check whether the control exists and then do the action.

    What I dont like about this, is that its written to use an exception as part of the code (instead of an exceptional circumstance like a file not being found or whatever). There are probably all kinds of different ways of doing this example, but Im interested to find a way of doing the equivalent of "If Control.Exists then do something" but without relying on exception throwing to determine the result. This example works fine btw, but it just doesnt look like good coding to me.

    VB Code:
    1. Dim a(2, 35) As Label
    2.         Dim t As New TableLayoutPanel
    3.         For i = 0 To 35 - 1
    4.             For j = 1 To 2
    5.                 a(j, i) = New Label
    6.                 a(j, i).Text = j.Tostring & "/" & i.Tostring
    7.                 t.Controls.Add(a(j, i))
    8.             Next
    9.         Next
    10.  
    11.         'At some point in the code I re-dim, for example, here we lose a(35)
    12.         ReDim Preserve a(2, 34)
    13.  
    14.         'In a sub somewhere I want to update the "a" labels
    15.         'Rather than keep track of the current index, I just (try and) detect whether the control exists
    16.         Try
    17.             If t.Contains(a(2, 35)) Then
    18.                 MessageBox.Show("Its there!")
    19.             Else
    20.                 MessageBox.Show("This message will never get shown")
    21.             End If
    22.         Catch ex As Exception
    23.             MessageBox.Show("Its not there!")
    24.         End Try

  10. #10
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] Testing whether a control exists

    Use the GetUpperBounds function:

    VB Code:
    1. Dim a(2, 35) As Label
    2.         MessageBox.Show(a.GetUpperBound(0))     'Returns 2
    3.         MessageBox.Show(a.GetUpperBound(1))     'Returns 35
    4.  
    5.         ReDim Preserve a(2, 34)
    6.         MessageBox.Show(a.GetUpperBound(0))     'Returns 2
    7.         MessageBox.Show(a.GetUpperBound(1))     'Returns 34

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [RESOLVED] [2005] Testing whether a control exists

    Aha! that is just perfect. I didnt know I could do that.

    Thanks all for commenting.

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