[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.
Re: [2005] Testing whether a control exists
Could use a try catch like...
VB Code:
Try
mycontrol.left = 10
catch
msgbox("Control does not exist")
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 :blush:
Re: [2005] Testing whether a control exists
Less resources being used to just test it with the TypeOf eval.
VB Code:
If TypeOf MyLabel(35) Is Label Then
If MyPanel.Contains(MyLabel(35)) Then
'Meow!
End If
End If
Re: [2005] Testing whether a control exists
Unfortunately a catch/try does not catch an exception if the index is invalid.
Re: [2005] Testing whether a control exists
VB Code:
For X = 0 To fromForm.Controls.Count - 1
If fromForm.Controls(X).ToString = "DropDownMessage.DropDownMsg" Then
fromForm.Controls(X).Dispose()
Exit For
End If
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...
Re: [2005] Testing whether a control exists
Quote:
Originally Posted by warrenayen
VB Code:
For X = 0 To fromForm.Controls.Count - 1
If fromForm.Controls(X).ToString = "DropDownMessage.DropDownMsg" Then
fromForm.Controls(X).Dispose()
Exit For
End If
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:
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.
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...
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.
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:
Dim a(2, 35) As Label
Dim t As New TableLayoutPanel
For i = 0 To 35 - 1
For j = 1 To 2
a(j, i) = New Label
a(j, i).Text = j.Tostring & "/" & i.Tostring
t.Controls.Add(a(j, i))
Next
Next
'At some point in the code I re-dim, for example, here we lose a(35)
ReDim Preserve a(2, 34)
'In a sub somewhere I want to update the "a" labels
'Rather than keep track of the current index, I just (try and) detect whether the control exists
Try
If t.Contains(a(2, 35)) Then
MessageBox.Show("Its there!")
Else
MessageBox.Show("This message will never get shown")
End If
Catch ex As Exception
MessageBox.Show("Its not there!")
End Try
Re: [2005] Testing whether a control exists
Use the GetUpperBounds function:
VB Code:
Dim a(2, 35) As Label
MessageBox.Show(a.GetUpperBound(0)) 'Returns 2
MessageBox.Show(a.GetUpperBound(1)) 'Returns 35
ReDim Preserve a(2, 34)
MessageBox.Show(a.GetUpperBound(0)) 'Returns 2
MessageBox.Show(a.GetUpperBound(1)) 'Returns 34
Re: [RESOLVED] [2005] Testing whether a control exists
Aha! that is just perfect. I didnt know I could do that.
Thanks all for commenting.