如何在VB.net中判断控件的类型?

snailbug 2004-06-29 10:54:09
在一个form中,存在若干个button和label以及其他的控件。我想在语句中一次设置不同类型的控件的不同的样式,如下所示:

For i = 0 To Me.Container.Components.Count - 1
If Me.Container.Components.Item 是 botton Then
Me.Container.Components.Item。。。。。
。。。。
End If
If Me.Container.Components.Item 是 Label Then
Me.Container.Components.Item。。。。。
。。。。
End If

Next

请问中间的代码该如何写??
...全文
1626 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
snailbug 2004-07-03
  • 打赏
  • 举报
回复
Problem have been solved.
thx.
水如烟 2004-07-02
  • 打赏
  • 举报
回复
mycontrol.Button要全名吧,前头加上你的命名空间

Ctype(cT,myNameSpace.myControl.Button)

估计是这样。
snailbug 2004-07-01
  • 打赏
  • 举报
回复
-------------------------------
我碰到这样的一个问题。
我自己的一个属于mycontrol.button命名控件的类,并且是从 System.Windows.Forms.Control继承而来的。当我执行:

dim cT as control
for each cT in me.Controls
CTYPE(cT,mycontrol.button)
Next

在CTYPE(cT,mycontrol.button)总是出现mycontrol.button未定义。这是什么原因??
而CTYPE(cT,system.windows.form.button)是没有错误的。

如果要把cT转换成mycontrol.Button应该如何做??谢谢
应该如何进行类型转换?
----------------------------------------
sglwm 2004-07-01
  • 打赏
  • 举报
回复
Dim ctrlX As Control
For Each ctrlX In Me.Controls
If ctrlX.Tag <> "" Then
strTG = ctrlX.Tag
Select Case strTG
Case "Text"
..........

Case "Comb"
......
Case "Date"
........
Case "Check"
.....
End Select
End If
Next ctrlX
zheninchangjiang 2004-07-01
  • 打赏
  • 举报
回复
if ctrl.gettype.tostring="yourclassname" then
ctrl.buttonstyle.....
snailbug 2004-07-01
  • 打赏
  • 举报
回复
可是我自定义的控件mycontrol.Button的有些属性是control所没有的,我想通过将control转换成mycontrol.Button的类型,然后在访问它的属性。

比如mycontrol.Button有 buttonstyle属性(我自定义的),而一般Control是没有这个属性的。怎么办???
snailbug 2004-06-30
  • 打赏
  • 举报
回复
I get some information there:
http://www.dotnet247.com/247reference/msgs/40/204071.aspx

but I still can not solve this problem?
anyone can help me?
Relta 2004-06-30
  • 打赏
  • 举报
回复
Private Sub Button3_Click(略...) Handles Button3.Click
Dim cT As Control
For Each cT In Me.Controls
If TypeOf ct is button Then
with ctype(ct,button)
...
end with
Elseif TypeOf ct is label then
with ctype(ct,label )
...
end with
Elseif TypeOf ct is textbox then
with ctype(ct,textbox )
...
end with
End If
Next
End Sub
lxcc 2004-06-30
  • 打赏
  • 举报
回复
已经测试,昨天因为没有环境,所以写错了!:P
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer = 0
Dim con As Control
For Each con In Me.Controls
If con.GetType().ToString() = "System.Windows.Forms.Button" Then
i = i + 1
End If
Next
MsgBox(i)
End Sub
snailbug 2004-06-29
  • 打赏
  • 举报
回复
I get a error when run the line:
For i = 1 To Me.Container.Components.Count

& Error Message below:

n unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an object.


What's WRONG???
lxcc 2004-06-29
  • 打赏
  • 举报
回复
少了()
Me.Container.Components.Item(i).GetType().ToString()="System.Windows.Form.Label"
snailbug 2004-06-29
  • 打赏
  • 举报
回复
谢谢
但是问题依然没有解决。
如何判断是哪一个控件呢?

Me.Container.Components.Item(i).GetType.ToString()="System.Windows.Form.Label"

这样的语法是不正确的啊?
而且Item所带的参数是String型的?

lxcc 2004-06-29
  • 打赏
  • 举报
回复
Me.Container.Components.Item.GetType.ToString()="System.Windows.Form.Label"
vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集(八) 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView显示选择框CheckBox 46. DataGridView显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView显示按钮 49. DataGridView显示链接 50. DataGridView显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一列显示相应图片 57. DataGridView显示进度条(ProgressBar) 58. DataGridView添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridViewEnter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)

16,554

社区成员

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

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