如何验证复选框? [英] How to Validate Checkbox?

查看:66
本文介绍了如何验证复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个带有两个Textbox,一个Checkbox和两个按钮的vb表单.我正在使用SQL数据库作为此表单的后端.我窗体的控件是这样的:

1. txtGenderID
2. txtGenderName
3. chkIsDefault
4. btnSave
5. btnCancel

SQL表如下:

表名称:tblGender

Hi All,

I have one vb form with two Textbox, one Checkbox, and two buttons. I am using SQL Database as back end for this form. Controls of my form goes like that:

1. txtGenderID
2. txtGenderName
3. chkIsDefault
4. btnSave
5. btnCancel

SQL Table as follows:

Table Name: tblGender

GsGenderCode char(4)
GsGenderName varchar(50)
IsDefault bit (0 = false, 1=true)
SetDate datetime


在这种形式下,我想在"chkIsDefault"上进行一些事件,我希望此复选框可以验证真实值.

即,如果用户已经将一个记录设置为true,并且如果用户将另一值设置为true,则它应该验证说已经设置了真实值".我做了编码但没有工作.

我的代码如下:

功能
-------------------------------------------------- -----------------------------
[代码]


In this form I want to make some events on "chkIsDefault", I want this check box to validate true value.

i.e. if the user has already set one record as true and if user set another value as true then it should validate saying that "True value has already been set.". I did coding but not working.

My Code are as follow:

Function
-------------------------------------------------------------------------------
[code]

Public Function CheckBoxValidation(ByVal sCheckBoxCheck As Boolean) As Boolean
 Try
 Dim oCtsDr As OleDb.OleDbDataReader
 Dim CtsSQL As String = "SELECT IsDefault FROM dbo.tblGender WHERE IsDefault = '" & sCheckBoxCheck & "'"
 Dim CtsCommand As New OleDb.OleDbCommand(CtsSQL, SQLConn)
 SQLConn.ConnectionString = oFunc.GetConnectionString(strINIPath)
 SQLConn.Open()
 oCtsDr = CtsCommand.ExecuteReader()
 CheckBoxValidation = oCtsDr.HasRows
 oCtsDr.Close()
 SQLConn.Close()
 Catch ex As Exception
 Throw ex
 End Try
 End Function

[/code]
-------------------------------------------------- -----------------------------
btnSave代码
[代码]

[/code]
-------------------------------------------------------------------------------
btnSave Code
[code]

Private Sub btnCtsSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCtsSave.Click
 If ValidateData() Then
 If vilGender.CheckBoxValidation(True) Then
 MessageBox.Show("Another Gender already set as Default. Please check", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Information)
 ctsDefaultCheckBox.Focus()
 Exit Sub
 Else
 btnCtsSave.Focus()
 End If
 Try
 If bNewData Then
 SQLConn.Open()
 trns = SQLConn.BeginTransaction
 InsertCMD()
 trns.Commit()
 SQLConn.Close()
 GetData()
 bNewData = False
 bEditData = False
 Count()
 MessageBox.Show("Record Saved Successfully", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
 EnableControlsLoadMode(True)
 Else
 SQLConn.Open()
 trns = SQLConn.BeginTransaction
 UpdateCMD()
 trns.Commit()
 SQLConn.Close()
 GetData()
 bNewData = False
 bEditData = False
 Count()
 MessageBox.Show("Record Updated Successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)
 EnableControlsLoadMode(True)
 End If
 Catch ex As Exception
 trns.Rollback()
 MessageBox.Show("Critical Error!" & ex.Message, "Critical Error.", MessageBoxButtons.OK, MessageBoxIcon.Error)
 End Try
 End If
 End Sub


[/code]
-------------------------------------------------- -----------------------------

有人可以告诉我我错了吗?当我单击按钮时,它将同时验证值[TRUE和FALSE].
(请勿使用所有大写字母.)
谁能告诉我我哪里出了错?当我单击按钮时,它将同时验证值[True和False].


[/code]
-------------------------------------------------------------------------------

CAN ANYONE TELL ME WHERE DID I WENT WRONG? WHEN I CLICK BUTTON IT VALIDATES BOTH THE VALUE [TRUE AND FALSE].
(Do not use all capital letters.)
Can anyone tell me where did I went wrong? When I click button it validates both the value[True and False].

推荐答案

vilGender.CheckBoxValidation(True)

始终发送 true .复选框的选中状态和未选中状态都会同时进行验证.

相反,您可以先调用复选框值,然后再调用诸如
之类的验证

is always sending true. So is the validation is happening for both checked and unchecked state of the checkbox.

Instead, you may checkfirst on the checkbox value before you invoke the validation like

if(chkIsDefault.Checked)Then
If vilGender.CheckBoxValidation(True)



干杯,
Balaji



Cheers,
Balaji


非常感谢,您真是天才.现在工作正常.如何对文本框值进行相同的验证.

功能如下:
[代码]
公共函数GenderNameExists(ByVal sGenderName作为字符串)作为布尔值
试试
Dim oCtsDr As OleDb.OleDbDataReader
Dim CtsSQL As String =从dbo.hrGenderSetup中选择性别,性别="&性别名称& ""
将CtsCommand设置为新的OleDb.OleDbCommand(CtsSQL,SQLConn)
SQLConn.ConnectionString = oFunc.GetConnectionString(strINIPath)
SQLConn.Open()
oCtsDr = CtsCommand.ExecuteReader()
GenderNameExists = oCtsDr.HasRows
oCtsDr.Close()
SQLConn.Close()
异常捕获
扔前
结束尝试
最终功能
[/code]

我在保存"按钮中将此功能称为:

[代码]
如果ValidateData()然后
如果vilContacts.GenderNameExists(Gender.Text.Trim)然后
MessageBox.Show("Gender [& Gender.Text.Trim&"]已经存在.请检查.",数据验证",MessageBoxButtons.OK,MessageBoxIcon.Warning)
Gender.Focus()
退出子
ElseIf(ctsDefaultCheckBox.Checked)然后
如果vilContacts.CheckBoxValidation(True)然后
MessageBox.Show(另一个标题已经设置为默认标题.请检查",数据验证",MessageBoxButtons.OK,MessageBoxIcon.Information)
ctsDefaultCheckBox.Focus()
退出子
其他
btnCtsSave.Focus()
如果结束
如果结束
试试
如果是bNewData然后
SQLConn.Open()
trns = SQLConn.BeginTransaction
InsertCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show(记录已成功保存",保存",MessageBoxButtons.OK,MessageBoxIcon.Information)
EnableControlsLoadMode(True)
其他
SQLConn.Open()
trns = SQLConn.BeginTransaction
UpdateCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show(记录已成功更新",更新",MessageBoxButtons.OK,MessageBoxIcon.Information)
EnableControlsLoadMode(True)
如果结束
异常捕获
trns.Rollback()
MessageBox.Show(严重错误!"& ex.Message,严重错误.",MessageBoxButtons.OK,MessageBoxIcon.Error)
结束尝试
如果结束
结束子
[/code]

上面的代码仅在添加新记录时有效,但是在编辑现有记录时不起作用.
Thank you very much, you are genius. It worked fine now. How can do the same validation for textbox value.

Function as follows:
[code]
Public Function GenderNameExists(ByVal sGenderName As String) As Boolean
Try
Dim oCtsDr As OleDb.OleDbDataReader
Dim CtsSQL As String = "SELECT Gender FROM dbo.hrGenderSetup WHERE Gender = ''" & sGenderName & "''"
Dim CtsCommand As New OleDb.OleDbCommand(CtsSQL, SQLConn)
SQLConn.ConnectionString = oFunc.GetConnectionString(strINIPath)
SQLConn.Open()
oCtsDr = CtsCommand.ExecuteReader()
GenderNameExists = oCtsDr.HasRows
oCtsDr.Close()
SQLConn.Close()
Catch ex As Exception
Throw ex
End Try
End Function
[/code]

I have called this funcation in save button as follows:

[code]
If ValidateData() Then
If vilContacts.GenderNameExists(Gender.Text.Trim) Then
MessageBox.Show("Gender [" & Gender.Text.Trim & "] already exists. Please Check.", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Gender.Focus()
Exit Sub
ElseIf (ctsDefaultCheckBox.Checked) Then
If vilContacts.CheckBoxValidation(True) Then
MessageBox.Show("Another Title already set as Default. Please check", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Information)
ctsDefaultCheckBox.Focus()
Exit Sub
Else
btnCtsSave.Focus()
End If
End If
Try
If bNewData Then
SQLConn.Open()
trns = SQLConn.BeginTransaction
InsertCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show("Record Saved Successfully", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
EnableControlsLoadMode(True)
Else
SQLConn.Open()
trns = SQLConn.BeginTransaction
UpdateCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show("Record Updated Successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)
EnableControlsLoadMode(True)
End If
Catch ex As Exception
trns.Rollback()
MessageBox.Show("Critical Error!" & ex.Message, "Critical Error.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
[/code]

The above code is working only when i add new record, but when i edit existing record it does not work.


这篇关于如何验证复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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