[VB.NET]关于清空件内容的问题.

本文介绍从VB升级到VB.NET时遇到的控件内容清空代码问题及解决方案,通过使用CType进行类型转换,实现TextBox、DateTimePicker和ComboBox控件的有效清空。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VB.NET源码-156个实用实例哦…… <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
关于清空件内容的问题.
Private Sub s_ClearControl()
On Error GoTo Err_Msg
Dim ctl As control
Dim nCount As Integer
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
ctl.Text = " "
ElseIf TypeOf ctl Is DTPicker Then
ctl.value = Date
ElseIf TypeOf ctl Is ComboBox Then
ctl.ListIndex = -1
End If
Next
Exit Sub
Err_Msg:
MsgBox(Err.Description)
End Sub

上面这段代码是VB中用于我清空TextBox ,ComboBox 三种控件的内容及对DTPicker控件进行初使化.现在要升成VB.net 后 trl.value=date 和ctl.ListIndex = -1
提示不是System.Windows.Forms.Control控件
下面是升成后的代码.有一点点改动了.
Private Sub s_ClearControl()
On Error GoTo Err_Msg
Dim ctl As System.Windows.Forms.Control
Dim nCount As Short
For Each ctl In Me.Controls
If ctl.Name = "TextBox " Then
ctl.Text = " "
ElseIf ctl.Name = "DateTimePicker " Then
ctl.value = Today
ElseIf ctl.Name = "ComboBox " Then
ctl.ListIndex = -1
End If
Next ctl
Exit Sub
Err_Msg:
MsgBox(Err.Description)
End Sub
各位大大有什么好的办法帮忙解决一下的....
__________________________________________________________________________
Imports System.Data
Imports System.Data.OleDb

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.DateTimePicker1.Value = "#1990-9-10# "
Me.DateTimePicker2.Value = "#1991-9-10# "
Me.ComboBox1.Items.Add( "11111 ")
Me.ComboBox1.Items.Add( "22222 ")
Me.ComboBox1.SelectedIndex = 0
Me.ComboBox2.Items.Add( "33333 ")
Me.ComboBox2.Items.Add( "44444 ")
Me.ComboBox2.SelectedIndex = 0
Me.TextBox1.Text = "text "
Me.TextBox2.Text = "alse text "
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim ctl As System.Windows.Forms.Control
For Each ctl In Me.Controls
If TypeOf (ctl) Is TextBox Then
ctl.Text = " "
ElseIf TypeOf ctl Is DateTimePicker Then
Dim d As DateTimePicker = ctl
d.Value = Today
ElseIf TypeOf ctl Is ComboBox Then
Dim cb As ComboBox = ctl
cb.SelectedIndex = -1
End If
Next ctl
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
__________________________________________________________________________
重新定义一个变量用于赋值吗?,我去试试
__________________________________________________________________________
其实用CType应该可以的,但是我不太会用。
__________________________________________________________________________
用Ctype:

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.DateTimePicker1.Value = "#1990-9-10# "
Me.DateTimePicker2.Value = "#1991-9-10# "
Me.ComboBox1.Items.Add( "11111 ")
Me.ComboBox1.Items.Add( "22222 ")
Me.ComboBox1.SelectedIndex = 0
Me.ComboBox2.Items.Add( "33333 ")
Me.ComboBox2.Items.Add( "44444 ")
Me.ComboBox2.SelectedIndex = 0
Me.TextBox1.Text = "text "
Me.TextBox2.Text = "alse text "
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim ctl As System.Windows.Forms.Control
For Each ctl In Me.Controls
If TypeOf (ctl) Is TextBox Then
ctl.Text = " "
ElseIf TypeOf ctl Is DateTimePicker Then
CType(ctl, DateTimePicker).Value = Today
ElseIf TypeOf ctl Is ComboBox Then
CType(ctl, ComboBox).SelectedIndex = -1
End If
Next ctl
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

End Class
__________________________________________________________________________
两位的都试过可以的.谢谢.
__________________________________________________________________________
晕死,明明是我一个人啊……不是不啊……难道……
__________________________________________________________________________
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值