检查代码中创建的标签文本是否包含特定字符串
Check if Label text created in code contains specific string
我创建了几个标签和文本框,它们的数量取决于用户在文本框中键入的内容,即输入 5 个,因此将有 5 个标签和 5 个文本框。
在这个例子中,我可以递归地搜索名称为 "myLabel" & var, var = number 1 到 5 的所有标签。
我拥有的是包含 3 个项目的标签的上下文菜单。第 1 项将标签文本更改为用户想要的任何内容,数字、字母、and/or 符号。第 2 项和第 3 项是我遇到的问题。
项目 2 需要在第一次启动时将标签文本更改为 D1,然后第二次需要将其更改为 D2,如果第三次调用,则错误不应让它们继续。
第 3 项与第 2 项相同,只是文本应分别为 S1 和 S2。
到目前为止我所拥有的是这个,但是我一直陷入循环或者它不会将 label.text 更改为我想要的。感谢任何帮助
Private Sub lblMenuItem3_Click()
label = lblContextMenu.SourceControl.Name
For Each control As Control In Me.Controls
If TypeOf control Is Label Then
Dim myLabel As Label = DirectCast(control, Label)
Dim str As String = myLabel.Text
'If myLabel.Text = "S1" Then
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
Me.Controls.Item(label).Text = "S2"
Else
'MessageBox.Show("The string is not in here!")
Me.Controls.Item(label).Text = "S1"
End If
Else
Me.Controls.Item(label).Text = "S1"
'End If
End If
Next
'Me.Controls.Item(label).Text = "S1"
'Me.Controls.Item(label).Text = "S2"
End Sub
编辑1:
此代码在我需要时部分起作用,但出于某种原因,它运行子程序的次数等于表单上标签的数量,我不确定为什么
Private Sub lblMenuItem3_Click()
label = lblContextMenu.SourceControl.Name
Dim s1lbl As Boolean = False
Dim s2lbl As Boolean = False
For Each control As Control In Me.Controls
If TypeOf control Is Label Then
Dim myLabel As Label = DirectCast(control, Label)
Dim str As String = myLabel.Text
If LCase(str).Contains(LCase("S1")) Then
s1lbl = True
Exit For
ElseIf LCase(str).Contains(LCase("S2")) Then
s2lbl = True
Exit For
Else
Continue For
End If
End If
Next
If s1lbl = False AndAlso s2lbl = False Then
Me.Controls.Item(label).Text = "S1"
Exit Sub
End If
If s1lbl = True AndAlso s2lbl = False Then
Me.Controls.Item(label).Text = "S2"
Exit Sub
End If
If s1lbl = False AndAlso s2lbl = True Then
MessageBox.Show("Too many Shallow points, only 2 allowed.")
Exit Sub
End If
End Sub
这是代码:
Private Sub TextBoxABPts_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxABPts.KeyPress
For i = 1 To TextBoxABPts.Text
lbl1 = New Label()
lbl1.Location = New Point(2, 165 + 25 * (i - 1))
lbl1.Name = "myLabel" & i
lbl1.Text = i
Me.Controls.Add(lbl1)
AddHandler lblMenuItem1.Click, AddressOf lblMenuItem1_Click
AddHandler lblMenuItem2.Click, AddressOf lblMenuItem2_Click
AddHandler lblMenuItem3.Click, AddressOf lblMenuItem3_Click
lblContextMenu.MenuItems.Add(lblMenuItem1)
lblContextMenu.MenuItems.Add(lblMenuItem2)
lblContextMenu.MenuItems.Add(lblMenuItem3)
lbl1.ContextMenu = lblContextMenu
Next i
TextBoxABDist.Focus()
End If
我认为这应该对您有所帮助:
在创建 lblContextMenu
的 for 循环外将菜单项添加到 lblContextMenu
lblContextMenu.MenuItems.Add("click1", New System.EventHandler(AddressOf Me.lblMenuItem1_Click1))
lblContextMenu.MenuItems.Add("click2", New System.EventHandler(AddressOf Me.lblMenuItem1_Click2))
lblContextMenu.MenuItems.Add("click3", New System.EventHandler(AddressOf Me.lblMenuItem1_Click3))
然后在您的 for 循环中将上下文菜单添加到标签中:
For i As Integer = 1 To TextBoxABPts.Text
Dim lbl1 = New Label()
lbl1.Location = New Point(2, 165 + 25 * (i - 1))
lbl1.Name = "myLabel" & i
lbl1.Text = i
Me.Controls.Add(lbl1)
lbl1.ContextMenu = lblContextMenu
Next i.
例如,您的菜单点击子可能如下所示:
Private Sub lblMenuItem1_Click1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mi As MenuItem = CType(sender, MenuItem)
Dim menu As ContextMenu = mi.GetContextMenu()
Dim lbl As Label = CType(menu.SourceControl, Label)
lbl.Text = mi.Text
End Sub
您需要将其与当前的 ifelse 逻辑相结合。
这个:
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
Me.Controls.Item(label).Text = "S2"
Else
'MessageBox.Show("The string is not in here!")
Me.Controls.Item(label).Text = "S1"
End If
应该是这样的:
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
myLabel.Text = "S2"
ElseIf LCase(str).Contains(LCase("S2"))
'MessageBox.Show("Bad Stuff!")
' Do your S3 error things here
Else
'MessageBox.Show("No S Value Found!")
myLabel.Text = "S1"
End If
如果没有ElseIf 结构,您将在将其设置为S1 或S2 之间交替,没有其他选择。有了适当的结构,你就可以检查所有的可能性,它不应该循环。
您需要在您的方法中使用正确的签名。它应该是这样的。
Label1_Click(sender As Object, e As EventArgs)
然后您可以将发件人转换为标签并设置文本。
Ctype(sender, Label).Text = "S1"
我创建了几个标签和文本框,它们的数量取决于用户在文本框中键入的内容,即输入 5 个,因此将有 5 个标签和 5 个文本框。 在这个例子中,我可以递归地搜索名称为 "myLabel" & var, var = number 1 到 5 的所有标签。 我拥有的是包含 3 个项目的标签的上下文菜单。第 1 项将标签文本更改为用户想要的任何内容,数字、字母、and/or 符号。第 2 项和第 3 项是我遇到的问题。 项目 2 需要在第一次启动时将标签文本更改为 D1,然后第二次需要将其更改为 D2,如果第三次调用,则错误不应让它们继续。 第 3 项与第 2 项相同,只是文本应分别为 S1 和 S2。 到目前为止我所拥有的是这个,但是我一直陷入循环或者它不会将 label.text 更改为我想要的。感谢任何帮助
Private Sub lblMenuItem3_Click()
label = lblContextMenu.SourceControl.Name
For Each control As Control In Me.Controls
If TypeOf control Is Label Then
Dim myLabel As Label = DirectCast(control, Label)
Dim str As String = myLabel.Text
'If myLabel.Text = "S1" Then
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
Me.Controls.Item(label).Text = "S2"
Else
'MessageBox.Show("The string is not in here!")
Me.Controls.Item(label).Text = "S1"
End If
Else
Me.Controls.Item(label).Text = "S1"
'End If
End If
Next
'Me.Controls.Item(label).Text = "S1"
'Me.Controls.Item(label).Text = "S2"
End Sub
编辑1: 此代码在我需要时部分起作用,但出于某种原因,它运行子程序的次数等于表单上标签的数量,我不确定为什么
Private Sub lblMenuItem3_Click()
label = lblContextMenu.SourceControl.Name
Dim s1lbl As Boolean = False
Dim s2lbl As Boolean = False
For Each control As Control In Me.Controls
If TypeOf control Is Label Then
Dim myLabel As Label = DirectCast(control, Label)
Dim str As String = myLabel.Text
If LCase(str).Contains(LCase("S1")) Then
s1lbl = True
Exit For
ElseIf LCase(str).Contains(LCase("S2")) Then
s2lbl = True
Exit For
Else
Continue For
End If
End If
Next
If s1lbl = False AndAlso s2lbl = False Then
Me.Controls.Item(label).Text = "S1"
Exit Sub
End If
If s1lbl = True AndAlso s2lbl = False Then
Me.Controls.Item(label).Text = "S2"
Exit Sub
End If
If s1lbl = False AndAlso s2lbl = True Then
MessageBox.Show("Too many Shallow points, only 2 allowed.")
Exit Sub
End If
End Sub
这是代码:
Private Sub TextBoxABPts_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxABPts.KeyPress
For i = 1 To TextBoxABPts.Text
lbl1 = New Label()
lbl1.Location = New Point(2, 165 + 25 * (i - 1))
lbl1.Name = "myLabel" & i
lbl1.Text = i
Me.Controls.Add(lbl1)
AddHandler lblMenuItem1.Click, AddressOf lblMenuItem1_Click
AddHandler lblMenuItem2.Click, AddressOf lblMenuItem2_Click
AddHandler lblMenuItem3.Click, AddressOf lblMenuItem3_Click
lblContextMenu.MenuItems.Add(lblMenuItem1)
lblContextMenu.MenuItems.Add(lblMenuItem2)
lblContextMenu.MenuItems.Add(lblMenuItem3)
lbl1.ContextMenu = lblContextMenu
Next i
TextBoxABDist.Focus()
End If
我认为这应该对您有所帮助: 在创建 lblContextMenu
的 for 循环外将菜单项添加到 lblContextMenu lblContextMenu.MenuItems.Add("click1", New System.EventHandler(AddressOf Me.lblMenuItem1_Click1))
lblContextMenu.MenuItems.Add("click2", New System.EventHandler(AddressOf Me.lblMenuItem1_Click2))
lblContextMenu.MenuItems.Add("click3", New System.EventHandler(AddressOf Me.lblMenuItem1_Click3))
然后在您的 for 循环中将上下文菜单添加到标签中:
For i As Integer = 1 To TextBoxABPts.Text
Dim lbl1 = New Label()
lbl1.Location = New Point(2, 165 + 25 * (i - 1))
lbl1.Name = "myLabel" & i
lbl1.Text = i
Me.Controls.Add(lbl1)
lbl1.ContextMenu = lblContextMenu
Next i.
例如,您的菜单点击子可能如下所示:
Private Sub lblMenuItem1_Click1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mi As MenuItem = CType(sender, MenuItem)
Dim menu As ContextMenu = mi.GetContextMenu()
Dim lbl As Label = CType(menu.SourceControl, Label)
lbl.Text = mi.Text
End Sub
您需要将其与当前的 ifelse 逻辑相结合。
这个:
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
Me.Controls.Item(label).Text = "S2"
Else
'MessageBox.Show("The string is not in here!")
Me.Controls.Item(label).Text = "S1"
End If
应该是这样的:
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
myLabel.Text = "S2"
ElseIf LCase(str).Contains(LCase("S2"))
'MessageBox.Show("Bad Stuff!")
' Do your S3 error things here
Else
'MessageBox.Show("No S Value Found!")
myLabel.Text = "S1"
End If
如果没有ElseIf 结构,您将在将其设置为S1 或S2 之间交替,没有其他选择。有了适当的结构,你就可以检查所有的可能性,它不应该循环。
您需要在您的方法中使用正确的签名。它应该是这样的。
Label1_Click(sender As Object, e As EventArgs)
然后您可以将发件人转换为标签并设置文本。
Ctype(sender, Label).Text = "S1"