使用TypeOf? [英] Using TypeOf?

查看:123
本文介绍了使用TypeOf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序在点击按钮时填充6个文本框,但是当使用TypeOf

我的程序试图填充标签控件时抛出我的阵列

of范围。如何才能使用TypeOf才能读取文本框控件?


一段代码:


Dim I As Integer


For I = 0 To Me.Controls.Count - 1


If(TypeOf Controls(I)是TextBox)然后


Dim tbox As TextBox = CType(Controls(I),TextBox)


tbox.Text = arr1(I)


结束如果


下一页

I have a program that fill 6 textbox on button click, but when using TypeOf
my program is trying to also fill label controls which throws my array out
of range. how can use TypeOf to only read textbox Control?

Piece of Code:

Dim I As Integer

For I = 0 To Me.Controls.Count - 1

If (TypeOf Controls(I) Is TextBox) Then

Dim tbox As TextBox = CType(Controls(I), TextBox)

tbox.Text = arr1(I)

End If

Next

推荐答案

" Leon" < VN ***** @ msn.com> schrieb:
"Leon" <vn*****@msn.com> schrieb:
我有一个程序在按钮点击时填充6个文本框,
但是当使用TypeOf时我的程序也试图填充标签控件,这会使我的阵列超出范围。
如何使用TypeOf才能读取文本框控件?

一段代码:

Dim I as Integer

对于I = 0到Me.Controls.Count - 1
如果(TypeOf控件(I)是TextBox)那么

Dim tbox As TextBox = CType(Controls(I),TextBox)

tbox.Text = arr1(I)

结束如果
I have a program that fill 6 textbox on button click,
but when using TypeOf my program is trying to also
fill label controls which throws my array out of range.
how can use TypeOf to only read textbox Control?

Piece of Code:

Dim I As Integer

For I = 0 To Me.Controls.Count - 1

If (TypeOf Controls(I) Is TextBox) Then

Dim tbox As TextBox = CType(Controls(I), TextBox)

tbox.Text = arr1(I)

End If




您的代码只会触及文本框。也许

''arr1''中的元素太少了?


-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org/>



Your code will only touch textboxes. Maybe there are too few elements in
''arr1''?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


但当我删除标签控件时代码运行得很好。它可能是什么。


" Herfried K. Wagner [MVP]" <喜*************** @ gmx.at>在消息中写道

news:Ou ************** @ TK2MSFTNGP12.phx.gbl ...
but when I remove the label control the code runs great. What could it be.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
" Leon" < VN ***** @ msn.com> schrieb:
"Leon" <vn*****@msn.com> schrieb:
我有一个程序在按钮点击时填充6个文本框,
但是当使用TypeOf时我的程序也试图填充标签控件,这会使我的阵列超出范围。
如何使用TypeOf才能读取文本框控件?

一段代码:

Dim I as Integer

对于I = 0到Me.Controls.Count - 1
如果(TypeOf控件(I)是TextBox)那么

Dim tbox As TextBox = CType(Controls(I),TextBox)

tbox.Text = arr1(I)

结束如果
I have a program that fill 6 textbox on button click,
but when using TypeOf my program is trying to also
fill label controls which throws my array out of range.
how can use TypeOf to only read textbox Control?

Piece of Code:

Dim I As Integer

For I = 0 To Me.Controls.Count - 1

If (TypeOf Controls(I) Is TextBox) Then

Dim tbox As TextBox = CType(Controls(I), TextBox)

tbox.Text = arr1(I)

End If



您的代码只会触及文本框。也许
''arr1'中的元素太少了?

-
Herfried K. Wagner [MVP]
< URL:http:// dotnet.mvps.org/>



Your code will only touch textboxes. Maybe there are too few elements in
''arr1''?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>



" Leon" < VN ***** @ msn.com> schrieb:
"Leon" <vn*****@msn.com> schrieb:
但是当我删除标签控件时,代码运行得很好。它可能是什么。
but when I remove the label control the code runs great. What
could it be.




原因是即使控件
$ b $,计数器我也会增加b使用''TypeOf''检查不是标签。试试这个代码:


\\\

Dim Counter As Integer

For each ctr As Control in Me。控件

如果TypeOf ctr是TextBox那么

DirectCast(ctr,TextBox).Text = Arr1(计数器)

Counter = Counter + 1

结束如果

下一个ctr

///


-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org/>



The reason is that the counter ''I'' will be incremented even if the control
checked using ''TypeOf'' is not a label. Try this code:

\\\
Dim Counter As Integer
For Each ctr As Control In Me.Controls
If TypeOf ctr Is TextBox Then
DirectCast(ctr, TextBox).Text = Arr1(Counter)
Counter = Counter + 1
End If
Next ctr
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


这篇关于使用TypeOf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆