我有这个代码来比较两个控制标记值的值:
Protected Function GetLabelTextForTag(tagVal As String) As String
Dim CoName As String = ""
For Each cntrl As Control In Me.Controls
If TypeOf cntrl Is Label Then
If DirectCast(cntrl, Label).Tag Is tagVal Then
CoName = DirectCast(cntrl, Label).Text
Exit For
End If
End If
Next
Return CoName
End Function
即使我到达这条线,但是:
If DirectCast(cntrl, Label).Tag Is tagVal Then
...和Tag与tagVal相同(" 1"),未到达下一行 - " 1"并不像" 1"
那样被视为同一件事不熟悉VB,我想也许是" Is"是问题。但我之所以使用" IS"但首先,是因为当我第一次尝试这个时:
If DirectCast(cntrl, Label).Tag = tagVal Then
...我得到了" 错误1选项Strict On禁止运算符类型为Object的操作数' ='。使用' Is'运算符来测试对象标识。"
该函数由按钮单击的事件处理程序调用:
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles Button1.Click
Dim connStr As String = "SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"
Dim upd8DML As String = "UPDATE CustomerCategoryLog SET Category = 'Exploding' WHERE Unit = @Unit And MemberNo = @MemberNo AND Custno = @CustNo"
Dim coName As String
Dim argVals(2) As String
Dim _Unit As String
Dim _MemberNo As String
Dim _CustNo As String
Dim curTagVal As String
For Each cntrl As Control In Me.Controls
If TypeOf cntrl Is CheckBox Then
If DirectCast(cntrl, CheckBox).Checked = True Then
curTagVal = CStr(DirectCast(cntrl, CheckBox).Tag)
coName = GetLabelTextForTag(curTagVal)
argVals = GetArgValsForCompanyName(coName)
_Unit = argVals(0)
_MemberNo = argVals(1)
_CustNo = argVals(2)
Using conn As New SqlConnection(connStr), _
cmd As New SqlCommand(upd8DML, conn)
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName
conn.Open
cmd.ExecuteScalar()
End Using
End If
End If
Next
End Sub
我正在查找具有相同标记值的标签和复选框,因为我正在动态创建这些控件的一些对,并且需要知道检查哪些标签的相应复选框。我创建它们(一些,这只是一个测试)就像这样:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sqlConnection1 As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "select C.CompanyName from CustomerCategoryLog CCL join Customers C on C.CustNo = CCL.Custno where CCL.Category = 'New' and C.Active <> 0 order by C.CompanyName"
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1
sqlConnection1.Open()
reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.
If reader.HasRows Then
Dim i As Integer = 0
While reader.Read()
i = i+1
If i > 12 'There are thousands...just grab the first dozen for this test
Exit While
End If
Dim lblCompanyName = New Label()
lblCompanyName.Tag = i.ToString() 'ID not available...?!?
lblCompanyName.Text = reader.Item(0).ToString()
Me.Controls.Add(lblCompanyName)
Dim ckbx = New CheckBox()
ckbx.Tag = i.ToString()
ckbx.Checked = True
Me.Controls.Add(ckbx)
End While
End If
reader.Close()
sqlConnection1.Close()
End Sub
我也可以在表单上添加唯一的其他代码,以便进行更好的衡量:
Protected Function GetArgValsForCompanyName(coName As String) As String()
Dim args(2) As String
Dim sqlConnection1 As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName
cmd.Connection = sqlConnection1
sqlConnection1.Open()
reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.
If reader.HasRows Then
args(0) = reader.Item(0).ToString()
args(1) = reader.Item(1).ToString()
args(2) = reader.Item(2).ToString()
End If
reader.Close()
sqlConnection1.Close()
Return args
End Function
&#34; 1&#34;不是&#34; 1&#34;?
答案 0 :(得分:7)
好的,Tag是一个Object而不是一个String因此有严格的选项就可以解决问题,试试
If DirectCast(cntrl, Label).Tag.ToString() = tagVal Then
使用您是在比较参考而不是值。
也为你解决了这个问题
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles Button1.Click
Dim connStr As String = "SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"
Dim upd8DML As String = "UPDATE CustomerCategoryLog SET Category = 'Exploding' WHERE Unit = @Unit And MemberNo = @MemberNo AND Custno = @CustNo"
Dim coName As String
Dim argVals(2) As String
Dim _Unit As String
Dim _MemberNo As String
Dim _CustNo As String
Dim curTagVal As String
For Each cntrl As Control In Me.Controls
If TypeOf cntrl Is CheckBox Then
If DirectCast(cntrl, CheckBox).Checked = True Then
curTagVal = CStr(DirectCast(cntrl, CheckBox).Tag)
coName = GetLabelTextForTag(curTagVal)
argVals = GetArgValsForCompanyName(coName)
_Unit = argVals(0)
_MemberNo = argVals(1)
_CustNo = argVals(2)
Using conn As New SqlConnection(connStr), _
cmd As New SqlCommand(upd8DML, conn)
cmd.Parameters.Add("@Unit", SqlDbType.VarChar, 50).Value = _Unit
cmd.Parameters.Add("@MemberNo", SqlDbType.VarChar, 50).Value = _MemberNo
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName
conn.Open
cmd.ExecuteScalar()
End Using
End If
End If
Next
End Sub
答案 1 :(得分:0)
我仍然不知道为什么原始代码不起作用,但将其分为以下内容:
Protected Function GetLabelTextForTag(tagVal As String) As String
Dim CoName As String = ""
Dim lblTag As String
For Each cntrl As Control In Me.Controls
If TypeOf cntrl Is Label Then
lblTag = CStr(DirectCast(cntrl, Label).Tag)
If lblTag = tagVal Then
CoName = DirectCast(cntrl, Label).Text
Exit For
End If
End If
Next
Return CoName
End Function