|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do I determine if a control has a specific propertyI'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 "rmiller" <CutThroatRac***@gmail.com> wrote in message The very easiest and most straight forward way is to add error handling. The news:1144867205.091642.28750@v46g2000cwv.googlegroups.com... > 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 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. -- Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com Please keep all discussions in the groups.. Thanks Ken. The issue occurs in a for loop. Got an example of how to
handle this error Ryan "rmiller" <CutThroatRac***@gmail.com> wrote in message Uh oh <g> In dotNetish? Not really <g> This is converted from VB6. No news:1144870693.508460.132900@g10g2000cwb.googlegroups.com... > Thanks Ken. The issue occurs in a for loop. Got an example of how to > handle this error > Ryan 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 '========== -- Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com Please keep all discussions in the groups.. 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. 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. 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. 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclasshierarchy.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/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolmemberstopic.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 Show quoteHide quote "rmiller" <CutThroatRac***@gmail.com> schreef in bericht news:1144867205.091642.28750@v46g2000cwv.googlegroups.com... > 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 > 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 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 |
|||||||||||||||||||||||