How do I determine if a control has a specific property

R

rmiller

I'm looping through controls on my form and grabbing the TabIndex off
each one, but when it hits a control w/out a TabIndex (like a timer) it
crashes. So, how do i check to see if the current control has a
specific property?
Thanks
 
K

Ken Halter

rmiller said:
I'm looping through controls on my form and grabbing the TabIndex off
each one, but when it hits a control w/out a TabIndex (like a timer) it
crashes. So, how do i check to see if the current control has a
specific property?
Thanks

The very easiest and most straight forward way is to add error handling. The
app's not crashing, it's hitting a point that needs your attention. When it
fails to get the attention it deserves, then, it crashes.
 
R

rmiller

Thanks Ken. The issue occurs in a for loop. Got an example of how to
handle this error
Ryan
 
K

Ken Halter

rmiller said:
Thanks Ken. The issue occurs in a for loop. Got an example of how to
handle this error
Ryan

Uh oh <g> In dotNetish? Not really <g> This is converted from VB6. No
Try/Catch stuff (no "real" dotNet experience here).

The first one ignores all errors raised from accessing an invalid property..
if all you're doing is checking tab indexes, that may be all you'd need. The
second has a "trap" that shows a box if the error is anything other than the
one we're trying to trap. (there seems to be so much more typing involved in
dotNet, it's nuts)
'==========
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles Command1.Click
Dim c As System.Windows.Forms.Control
Dim i As Short

On Error Resume Next

For Each c In Me.Controls
i = c.TabStop 'this is enough to raise the error
'Here you can check for Err.Number <> 0 if you want.
Next c

Err.Clear()
End Sub

Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles Command2.Click
Dim c As System.Windows.Forms.Control
Dim i As Short

On Error GoTo ErrorTrap

For Each c In Me.Controls
i = c.TabStop 'this is enough to raise the error
Next c

Terminate:
Exit Sub

ErrorTrap:
If Err.Number = 438 Then
Resume Next
Else
MsgBox("Error " & Err.Number)
Resume Terminate
End If
End Sub
'==========
 
R

rmiller

Hmmm...something's still not right. Here's the code:

If KeyCode = 9 Then
Set curCtl = Me.ActiveControl
If Not (curCtl Is Nothing) Then
curTabIndex = curCtl.TabIndex
For Each ctl In Me.Controls
If Not (ctl Is curCtl) Then
If ctl.TabIndex = (curTabIndex + 1) Then ///Here's
where the error pops up
ctl.SetFocus
Exit Sub
End If
End If
Next
End If
End If

As you can see i need to go to the next ctl not ignore the error and
move to the next line.
 
R

rmiller

Hmmm...something's still not right. Here's the code:

If KeyCode = 9 Then
Set curCtl = Me.ActiveControl
If Not (curCtl Is Nothing) Then
curTabIndex = curCtl.TabIndex
For Each ctl In Me.Controls
If Not (ctl Is curCtl) Then
If ctl.TabIndex = (curTabIndex + 1) Then ///Here's
where the error pops up
ctl.SetFocus
Exit Sub
End If
End If
Next
End If
End If

As you can see i need to go to the next ctl not ignore the error and
move to the next line.
 
R

rmiller

Hmmm...something's still not right. Here's the code:

If KeyCode = 9 Then
Set curCtl = Me.ActiveControl
If Not (curCtl Is Nothing) Then
curTabIndex = curCtl.TabIndex
For Each ctl In Me.Controls
If Not (ctl Is curCtl) Then
If ctl.TabIndex = (curTabIndex + 1) Then ///Here's
where the error pops up
ctl.SetFocus
Exit Sub
End If
End If
Next
End If
End If

As you can see i need to go to the next ctl not ignore the error and
move to the next line.
 
C

Cor Ligthert [MVP]

rMiller,

A timer is not a control (It is sad that some from Microsoft have used
Control for every Component that could be placed in the toolbox).

Control are class from Forms and WebUI

This is the 1.x list from forms
http://msdn.microsoft.com/library/d...rfsystemwindowsformscontrolclasshierarchy.asp

Tabindex is a member of control, so it will always be in a derived control
(although it can be overridden in a way that it does nothing)

http://msdn.microsoft.com/library/d...rlrfsystemwindowsformscontrolmemberstopic.asp

Therefore there should not be any problem if you are looping through a
control collection if it is about TabIndex.

It is something else with things as selectedindex which are part of the
derived classes as ListControl, therefore I check this almost forever. There
where a for will be used is mostly needed to check if it is value or text
that has to be set.

I hope this helps,

Cor
 
R

rmiller

Well, control or not. When i loop through the controls it thinks it is
one. To solve the problem I broke it by first setting the newTabIndex
to -1 then setting it to the next controls TabIndex. If this control
doesn't have a TabIndex it errors, but resumes next and the newTabIndex
remains -1. I then do a check for the -1 value and proceed if it isn't.

On Error Resume Next
Dim ctl As Control
Dim curCtl As Control
Dim curTabIndex As Integer
Dim newTabIndex As Integer

If KeyCode = 9 Then
Set curCtl = Screen.ActiveControl
If Not (curCtl Is Nothing) Then
curTabIndex = curCtl.TabIndex
For Each ctl In Screen.ActiveForm.Controls
If Not (ctl Is curCtl) Then
newTabIndex = -1
newTabIndex = ctl.TabIndex
If newTabIndex <> -1 And newTabIndex = (curTabIndex
+ 1) Then
ctl.SetFocus
Exit Sub
End If
End If
Next
End If
End If
 
R

rmiller

Well, control or not. When i loop through the controls it thinks it is
one. To solve the problem I broke it by first setting the newTabIndex
to -1 then setting it to the next controls TabIndex. If this control
doesn't have a TabIndex it errors, but resumes next and the newTabIndex
remains -1. I then do a check for the -1 value and proceed if it isn't.

On Error Resume Next
Dim ctl As Control
Dim curCtl As Control
Dim curTabIndex As Integer
Dim newTabIndex As Integer

If KeyCode = 9 Then
Set curCtl = Screen.ActiveControl
If Not (curCtl Is Nothing) Then
curTabIndex = curCtl.TabIndex
For Each ctl In Screen.ActiveForm.Controls
If Not (ctl Is curCtl) Then
newTabIndex = -1
newTabIndex = ctl.TabIndex
If newTabIndex <> -1 And newTabIndex = (curTabIndex
+ 1) Then
ctl.SetFocus
Exit Sub
End If
End If
Next
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top