我正在制作一个程序,当我使用_Mouseclicked事件右键单击一个标签时,它将显示一个列表框并从列表项中进行选择,并且当我右键单击该标签以更改背景色时,还显示一个colorDialog。但是,问题在于,我在表单上有多个标签可供选择,并且每当我要更改任何标签的值或颜色时,都必须重复调用。请我需要某人来帮助检查我在这里所做的是否是关于此事的最佳方法。
Private Sub listParameters_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lblParameter.MouseDown, lblParameter2.MouseDown, lblParameter3.Click, lblParameter4.Click, _
lblParameter5.Click, lblParameter6.Click, lblParameter7.Click, lblParameter8.Click, lblParameter9.Click, lblParameter10.Click, lblParameter11.Click, lblParameter12.Click, lblParameter13.Click, lblParameter14.Click, lblParameter15.Click, _
lblParameter16.Click, lblParameter17.Click, lblParameter18.Click, lblParameter19.Click, lblParameter20.Click, lblParameter21.Click, lblParameter22.Click, lblParameter23.Click, lblParameter24.Click, lblParameter25.Click, _
lblParameter26.Click, lblParameter27.Click, lblParameter28.Click, lblParameter29.Click, lblParameter30.Click, lblParameter31.Click, lblParameter32.Click, lblParameter33.Click, lblParameter34.Click, lblParameter35.Click, _
ListParameters.SelectedIndexChanged
Select Case sender.Name
Case "lblParameter"
lblParameter.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter2"
lblParameter2.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue2.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter3"
lblParameter3.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue3.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter4"
lblParameter4.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue4.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter5"
lblParameter5.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue5.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter6"
lblParameter6.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue6.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter7"
lblParameter7.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue7.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter8"
lblParameter8.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue8.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter9"
lblParameter9.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue9.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter10"
lblParameter10.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue10.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter11"
lblParameter11.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue11.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter12"
lblParameter12.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue12.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter13"
lblParameter13.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue13.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter14"
lblParameter14.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue14.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter15"
lblParameter15.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue15.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter16"
lblParameter16.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue16.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter17"
lblParameter17.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue17.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter18"
lblParameter18.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue18.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter19"
lblParameter19.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue19.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter20"
lblParameter20.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue20.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter21"
lblParameter21.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue21.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter22"
lblParameter22.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue22.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter23"
lblParameter23.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue23.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter24"
lblParameter24.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue24.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter25"
lblParameter25.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue25.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter26"
lblParameter26.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue26.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter27"
lblParameter27.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue27.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter28"
lblParameter28.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue28.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter29"
lblParameter29.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue29.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter30"
lblParameter30.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue30.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter31"
lblParameter31.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue31.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter32"
lblParameter32.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue32.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter33"
lblParameter33.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue33.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter34"
lblParameter34.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue34.Text = ListParameters.SelectedItem("Metric Conversion").ToString
Case "lblParameter35"
lblParameter35.Text = ListParameters.SelectedItem("Medium Name").ToString
lblValue35.Text = ListParameters.SelectedItem("Metric Conversion").ToString
End Select
End Sub
答案 0 :(得分:0)
这是我正在谈论的一个示例,该示例可以大大减少您拥有的代码。您将需要进入每个lblParameters并在.Tag属性中放置关联的Value标签的名称(即,参数的lblValue,参数2的lblValue2等)。要进行测试,请高亮选择案例组并将其标记出来,然后将以下内容放入该子项中。
Dim clickedLabel As Label = DirectCast(sender, Label)
clickedLabel.Text = ListParameters.Text
DirectCast(Me.Controls(clickedLabel.Tag.ToString), Label).Text = ListParameters.SelectedValue.ToString
如您所见,先将发送方转换为标签并设置其.Text属性,然后将在标签中找到的具有名称的控件转换回标签并设置其.Text属性。