如何根据TabIndex来选定控件?

xiyun 2002-11-20 03:04:11
比如在Form上有若干Label控件,其中一部分的TabIndex为从1~n,我怎样才能在程序中实现对这部分Label进行依次访问呢?
...全文
154 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Donmi 2002-11-21
  • 打赏
  • 举报
回复
你到底是什么错误也没有说清楚,把错误表述出来才能帮你,还有你想达到什么目的?
xiyun 2002-11-20
  • 打赏
  • 举报
回复
Dim x As Control

Do
For Each x In Me.Controls
i = Me.Controls.IndexOf(x)
If x.TabIndex = p Then
x = Me.Controls.Item(i)
n = n + 1
p = p + 1
If n >= a And n < (a + b) Then
x.Text = Str(m)
m = m + 1
Else
x.Text = ""
End If
x.BackColor = System.Drawing.Color.LightYellow
Exit For
End If

Next
Loop Until p > 37

我改成这样也不行啊,谁帮帮我吧。这个东西我作了好久了,这个地方总是出问题。
xiyun 2002-11-20
  • 打赏
  • 举报
回复
to Donmi
你的这一行 有问题啊!
x = CType(Me.Controls.Item(i),Label)
Donmi 2002-11-20
  • 打赏
  • 举报
回复
其实Label的TabIndex属性没有什么意义,因为Label是无法选中的,我不知道你的用途是什么?若照你的程序来看,应该改为:
Dim i As Integer

Dim p As Integer
p = 1

Do
For i = 0 To (Me.Controls.Count - 1)
If Me.Controls.Item(i).GetType.Equals(GetType(Windows.Forms.Label)) Then
Dim x As Windows.Forms.Label()
x = CType(Me.Controls.Item(i),Label)
If x.TabIndex = p Then

p = p + 1
x.BackColor = System.Drawing.Color.LightYellow
Exit For
End If
End If
Next i
Loop Until p > 37
将New去除,但这样做有个缺点,如果Label包含在其它控件如Panel或GroupBox内,则Me.Controls集合中根本不存在此Label,它存在于Panel.Controls或GroupBox.Controls集合中,若你要如此做,则这个问题需考虑,判断Me.Controls(i)是否包含子控件,若有,继续从里搜索。
xiyun 2002-11-20
  • 打赏
  • 举报
回复
没有人会么?
xiyun 2002-11-20
  • 打赏
  • 举报
回复
我作的大概是这样,为什么不对呢?
'找到相应子Label,并改变它的属性
Dim i As Integer

Dim p As Integer
p = 1

Do
For i = 0 To (Me.Controls.Count - 1)
If Me.Controls.Item(i).GetType.Equals(GetType(Windows.Forms.Label)) Then
Dim x As New Windows.Forms.Label()
x = Me.Controls.Item(i)
If x.TabIndex = p Then

p = p + 1
x.BackColor = System.Drawing.Color.LightYellow
Exit For
End If
End If
Next i
Loop Until p > 37
wang_cel 2002-11-20
  • 打赏
  • 举报
回复
Dim c As Control

For Each c In Me.Controls

If c.TabIndex = 2 Then
MsgBox(CType(c, Label).Text)
End If

Next

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧