在VB.NET中检查空控件
我想检查我表单中的控件是否为空。。。我有一个很长的代码。我给出下面给出的代码在VB.NET中检查空控件,vb.net,maskedtextbox,backcolor,is-empty,Vb.net,Maskedtextbox,Backcolor,Is Empty,我想检查我表单中的控件是否为空。。。我有一个很长的代码。我给出下面给出的代码 Public totflag As Boolean Private Sub BTNSAVE_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTNSAVE.Click CheckMyControls() If totflag = False Then MessageBox.Show("Give Comp
Public totflag As Boolean
Private Sub BTNSAVE_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTNSAVE.Click
CheckMyControls()
If totflag = False Then
MessageBox.Show("Give Compleate Data!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
coloring()
ElseIf totflag = True Then
MessageBox.Show("Success", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Public Sub coloring()
Dim txt, cmb, mtxt, rtxt As Control
For Each txt In EMPGBDATA.Controls
If TypeOf txt Is TextBox Then
If txt.Text = "" Then
txt.BackColor = Color.Red
End If
End If
Next
For Each cmb In EMPGBDATA.Controls
If TypeOf cmb Is ComboBox Then
If cmb.Text = "Select" Then
cmb.BackColor = Color.Red
End If
End If
Next
For Each mtxt In EMPGBDATA.Controls
If TypeOf mtxt Is MaskedTextBox Then
If mtxt.Text = "" AndAlso mtxt.Name <> "MTXTPFESI" Then
mtxt.BackColor = Color.Red
End If
End If
Next
For Each rtxt In EMPGBDATA.Controls
If TypeOf rtxt Is RichTextBox Then
If rtxt.Text = "" Then
rtxt.BackColor = Color.Red
End If
End If
Next
End Sub
Public Function CheckMyControls() As Boolean
Dim txt, cmb, mtxt, rtxt As Control
Dim flagtxt, flagcmb, flagmtxt, flagrtxt As Boolean
flagtxt = False
For Each txt In EMPGBDATA.Controls
If TypeOf txt Is TextBox Then
If txt.Text = "" Then
flagtxt = True
End If
End If
Next
flagcmb = False
For Each cmb In EMPGBDATA.Controls
If TypeOf cmb Is ComboBox Then
If cmb.Text = "Select" Then
flagcmb = True
End If
End If
Next
flagmtxt = False
For Each mtxt In EMPGBDATA.Controls
If TypeOf mtxt Is MaskedTextBox Then
If mtxt.Text = "" AndAlso mtxt.Name <> "MTXTPFESI" Then
flagmtxt = True
End If
End If
Next
flagrtxt = False
For Each rtxt In EMPGBDATA.Controls
If TypeOf rtxt Is RichTextBox Then
If rtxt.Text = "" Then
flagrtxt = True
End If
End If
Next
If flagtxt = True Or flagcmb = True Or flagmtxt = True Or flagrtxt = True Then
totflag = False
Else
totflag = True
End If
Return totflag
End Function
Public标记为布尔值
私有子BTNSAVE_Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理BTNSAVE。单击
CheckMyControls()
如果totflag=False,则
显示(“提供完整数据!”,“警告”,MessageBoxButtons.OK,MessageBoxIcon.Warning)
着色()
ElseIf totflag=则为真
显示(“成功”,“信息”,MessageBoxButtons.OK,MessageBoxIcon.Information)
如果结束
端接头
公共子着色()
Dim txt、cmb、mtxt、rtxt作为控制
对于EMPGBDATA.Controls中的每个txt
如果txt的类型为文本框,则
如果txt.Text=”“,则
txt.BackColor=Color.Red
如果结束
如果结束
下一个
对于EMPGBDATA.控件中的每个cmb
如果cmb的类型为组合框,则
如果cmb.Text=“选择”,则
cmb.BackColor=颜色.红色
如果结束
如果结束
下一个
对于EMPGBDATA.Controls中的每个mtxt
如果mtxt的类型为MaskedTextBox,则
如果mtxt.Text=”“且也有mtxt.Name“MTXTPFESI”,则
mtxt.BackColor=Color.Red
如果结束
如果结束
下一个
对于EMPGBDATA.Controls中的每个rtxt
如果rtxt的类型为RichTextBox,则
如果rtxt.Text=”“,则
rtxt.BackColor=Color.Red
如果结束
如果结束
下一个
端接头
公共函数checkMyControl()为布尔值
Dim txt、cmb、mtxt、rtxt作为控制
将flagtxt、flagcmb、flagmtxt、flagrtxt设置为布尔值
flagtxt=False
对于EMPGBDATA.Controls中的每个txt
如果txt的类型为文本框,则
如果txt.Text=”“,则
flagtxt=True
如果结束
如果结束
下一个
flagcmb=错误
对于EMPGBDATA.控件中的每个cmb
如果cmb的类型为组合框,则
如果cmb.Text=“选择”,则
flagcmb=真
如果结束
如果结束
下一个
flagmtxt=False
对于EMPGBDATA.Controls中的每个mtxt
如果mtxt的类型为MaskedTextBox,则
如果mtxt.Text=”“且也有mtxt.Name“MTXTPFESI”,则
flagmtxt=True
如果结束
如果结束
下一个
flagrtxt=False
对于EMPGBDATA.Controls中的每个rtxt
如果rtxt的类型为RichTextBox,则
如果rtxt.Text=”“,则
flagrText=True
如果结束
如果结束
下一个
如果flagtxt=True或flagcmb=True或flagmtxt=True或flagrtxt=True,则
totflag=False
其他的
totflag=True
如果结束
返回totflag
端函数
实际上我不想勾选一个PFESI文本框。是否为空。这是一个屏蔽文本框。我的问题是,当我在所有控件都有数据的情况下按下提交按钮时,它会显示消息框“提供完整数据!”。没有数据的控件也显示相同的消息。请尝试检查代码并给我解决方案。您的操作太复杂了。 我不在电脑前,所以代码可能有小错误:
If CheckMyControls() = False Then
MessageBox.Show("Give Compleate Data!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
coloring()
Else
MessageBox.Show("Success", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Public Sub coloring()
For Each cntrl In Me.Controls
Select Case cntrl.GetType
Case GetType(ComboBox)
If cntrl.Text = "Select" Then
cntrl.BackColor = Color.Red
Exit For
End If
Case GetType(MaskedTextBox)
If cntrl.Text = "" AndAlso cntrl.Name <> "MTXTPFESI" Then
cntrl.BackColor = Color.Red
End If
Case Else
If cntrl.Text = "" Then
cntrl.BackColor = Color.Red
End If
End Select
Next
End Sub
Public Function CheckMyControls() As Boolean
Dim bEmptyControlFlag As Boolean
For Each cntrl In Me.Controls
Select Case cntrl.GetType
Case GetType(ComboBox)
If cntrl.Text = "Select" Then
bEmptyControlFlag = True
Exit For
End If
Case Else
If cntrl.Text = "" Then
bEmptyControlFlag = True
End If
End Select
Next
Return Not bEmptyControlFlag
End Function
如果checkMyControl()=False,则
显示(“提供完整数据!”,“警告”,MessageBoxButtons.OK,MessageBoxIcon.Warning)
着色()
其他的
显示(“成功”,“信息”,MessageBoxButtons.OK,MessageBoxIcon.Information)
如果结束
端接头
公共子着色()
对于Me.控件中的每个cntrl
选择Case cntrl.GetType
Case GetType(组合框)
如果cntrl.Text=“选择”,则
cntrl.BackColor=Color.Red
退出
如果结束
Case GetType(MaskedTextBox)
如果cntrl.Text=“”并且cntrl.Name“MTXTPFESI”,则
cntrl.BackColor=Color.Red
如果结束
其他情况
如果cntrl.Text=”“,则
cntrl.BackColor=Color.Red
如果结束
结束选择
下一个
端接头
公共函数checkMyControl()为布尔值
Dim BemptyControl标志为布尔值
对于Me.控件中的每个cntrl
选择Case cntrl.GetType
Case GetType(组合框)
如果cntrl.Text=“选择”,则
bEmptyControlFlag=True
退出
如果结束
其他情况
如果cntrl.Text=”“,则
bEmptyControlFlag=True
如果结束
结束选择
下一个
返回非bEmptyControlFlag
端函数
每次签入我的代码时,我都会添加其他部分。。。。它很好用。。。。Thanx为您重播。。。。