使用VB.Net 4.6(VS Enterprise 2015),窗体
我有以下情况:
List(Of String)
名为' BlackRed',' BlackGreen',
' WhiteRed' &安培; ' WhiteGreen&#39 ;. 我有两个GroupBox,每个都有两个
单选按钮。 GroupBox1
是衬衫颜色'与RadioButtons
' Black' &安培;
'白'并且GroupBox2
是' Pants Color'与RadioButtons
'红色' &安培;
'绿色&#39 ;.
根据检查RadioButtons的内容,我最终得到了一个 名为' ColorCombo'的字符串变量包含所选颜色。
四种可能性是" BlackRed"," BlackGreen"," WhiteRed"要么 " WhiteGreen"与我的四个List(Of String)s的名称相同。
ListToChart
的图表(即
一个List(Of String)
)来绘制数据。 现在我的问题是:
我如何使用中的值
字符串变量,用于将匹配的List(Of String)
指定为
' ListToChart&#39 ;?我无法使用
ListToChart = ColorCombo,因为它显然会导致类型' String'无法转换为List(Of String)
"错误。
例如:'衬衫颜色'是黑色,裤子颜色'是红色的。这个
创造了" BlackRed"在名为的String变量中
' ColorCombo&#39 ;.我现在想要分配List(Of String)
与ColorCombo'中的值相同的名称到变量
' ListToChart'
以下是我的代码的修改版本:
Public Class Form2
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'The Lists are populated on another form and carried over to this one.
'The actual code consists of about 40 Lists and about 15 RadioButtons. This code is an extremely stripped down version.
'Assume for this example, the 4 List(Of String)s are named BlackRed, BlackGreen, WhiteRed & WhiteGreen.
Call READRADIOBUTTONS()
End Sub
Public Sub READRADIOBUTTONS()
If RadioButtonShirtBlack.Checked = True Then SHIRTSTRING = "Black"
If RadioButtonShirtWhite.Checked = True Then SHIRTSTRING = "White"
If RadioButtonPantsRed.Checked = True Then PANTSSTRING = "Red"
If RadioButtonPantsGreen.Checked = True Then PANTSSTRING = "Green"
COLORCOMBO = SHIRTSTRING & PANTSSTRING
Call PLOTCHARTDATA()
End Sub
Public Sub PLOTCHARTDATA()
'The following line is where I would need to use the String named COLORCOMBO to reference 1 of the 4 List(Of String)s.
LISTTOCHART = COLORCOMBO
ChartColors.Series("Series1").Points.Clear()
For a = 0 To LISTTOCHART.Count - 1
ChartColors.Series("Series1").Points.AddXY(LISTTOCHART.Item(a), COUNTTOCHART(a))
Next
End Sub
Public Sub RadioButtonShirtBlack_Click(sender As Object, e As EventArgs) Handles RadioButtonShirtBlack.Click
Call READRADIOBUTTONS()
End Sub
Public Sub RadioButtonShirtWhite_Click(sender As Object, e As EventArgs) Handles RadioButtonShirtWhite.Click
Call READRADIOBUTTONS()
End Sub
Public Sub RadioButtonPantsRed_Click(sender As Object, e As EventArgs) Handles RadioButtonPantsRed.Click
Call READRADIOBUTTONS()
End Sub
Public Sub RadioButtonPantsGreen(sender As Object, e As EventArgs) Handles RadioButtonPantsGreen.Click
Call READRADIOBUTTONS()
End Sub
答案 0 :(得分:0)
你不想这样做。您应该使用枚举来定义您的衬衫和裤子,而不是比较字符串:
Public Enum ShirtColour
Black
Red
End Enum
Public Enum PantsColour
White
Green
End Enum
然后你应该根据你的单选按钮选择设置一个变量,并使用一个类来保存选择:
Public Class UserSelection
Public Property Shirt As ShirtColour
Public Property Pants As PantsColour
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selection As New UserSelection
If radShirtBlack.Checked Then
selection.Shirt = ShirtColour.Black
ElseIf radShirtRed.Checked Then
selection.Shirt = ShirtColour.Red
End If
If radPantsGreen.Checked Then
selection.Pants = PantsColour.Green
ElseIf radPantsWhite.Checked Then
selection.Pants = PantsColour.White
End If
End Sub