如果那么ComboBox循环

时间:2017-09-09 19:03:51

标签: vb.net

我需要循环以下代码12次:

If t3frm.ComboBox1.SelectedIndex = 0 Then t3t1 = Val(t3frm.TextBox1.Text)
If t3frm.ComboBox1.SelectedIndex = 1 Then t3t1 = Val(t3frm.TextBox1.Text) * m
If t3frm.ComboBox1.SelectedIndex = 2 Then t3t1 = Val(t3frm.TextBox1.Text) * b
If t3frm.ComboBox1.SelectedIndex = 3 Then t3t1 = Val(t3frm.TextBox1.Text) * t
If t3frm.ComboBox1.SelectedIndex = 4 Then t3t1 = Val(t3frm.TextBox1.Text) * kt
If t3frm.ComboBox1.SelectedIndex = 5 Then t3t1 = Val(t3frm.TextBox1.Text) * mt
If t3frm.ComboBox1.SelectedIndex = 6 Then t3t1 = Val(t3frm.TextBox1.Text) * bt
If t3frm.ComboBox1.SelectedIndex = 7 Then t3t1 = Val(t3frm.TextBox1.Text) * tt

我有12个组合框(1-12),12个变量(t3t1-t3t12)和12个文本框(1-12)。

所以每个都需要增加1。

我该怎么办呢?

2 个答案:

答案 0 :(得分:1)

您可以从Controls集合中按名称获取控件。

不使用单个变量t3t1, t3 t2, t3t3 ...,而是使用数组

Dim t3t(12) As Double

For i As Integer = 1 To 12
    Dim cb = DirectCast(t3frm.Controls("ComboBox" & i), ComboBox)
    Dim tb = DirectCast(t3frm.Controls("TextBox1" & i), TextBox)

    Select Case cb.SelectedIndex
        Case 0
            t3t(i) = Val(tb.Text)
        Case 1
            t3t(i) = Val(tb.Text) * m
        Case 2
            t3t(i) = Val(tb.Text) * b
        Case 3
            t3t(i) = Val(tb.Text) * t
        Case 4
            t3t(i) = Val(tb.Text) * kt
        Case 5
            t3t(i) = Val(tb.Text) * mt
        Case 6
            t3t(i) = Val(tb.Text) * bt
        Case 7
            t3t(i) = Val(tb.Text) * tt
    End Select
Next

如果将值1和变量m存储到数组中的tt,则可以进一步简化代码

Dim factor = {1, m, b, t, kt, mt, bt, tt}
Dim t3t(12) As Double

For i As Integer = 1 To 12
    Dim cb = DirectCast(t3frm.Controls("ComboBox" & i), ComboBox)
    Dim tb = DirectCast(t3frm.Controls("TextBox1" & i), TextBox)
    t3t(i) = Val(tb.Text) * factor(cb.SelectedIndex)
Next

请参阅:Arrays in Visual Basic

答案 1 :(得分:0)

  

我有12个组合框(1-12),12个变量(t3t1-t3t12)和12个   文本框(1-12)。

似乎是完美的收藏候选人,可以与DataGridView控件绑定 创建一个代表您的数据的类

Public Class Item Implement INotifyPropertyChanged
    Private _Value As Decimal
    Public Property Value As Decimal
        Get
            Return _Value
        End Property
        Set
            _Value = value
            RaisePropertyChanged(NameOf(Result)) // Notify control to update result
        End Set
    End Property

    Private _Factor As Decimal        
    Public Property Factor As Decimal
        Get
            Return _Factor
        End Property
        Set
            _Factor = value
            RaisePropertyChanged(NameOf(Result)) // Notify control to update result
        End Set
    End Property

    Public Property Result As Decimal

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private Sub RaisePropertyChanged(<CallerMemberName()> Optional propertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

使用构造函数中的预定义列创建DataGridView(您也可以在设计器中执行此操作)

Dim factor = {1, m, b, t, kt, mt, bt, tt}
Dim valueColumn = New DataGridViewTextBoxColumn With
{
    .DataPropertyName = "Value" ' Name of the correspondent property in Item class
}
Dim resultColumn = New DataGridViewTextBoxColumn With
{
    .DataPropertyName = "Result",
    .ReadOnly = True
}
Dim factorColumn = new DataGridViewComboBoxColumn
{
    .DataPropertyName = "Factor",
    .DataSource = factors; // will add factors to combobox items
}

yourDataGridView.AutoGenerateColumns = False // Will use only columns you created
yourDataGridView.Columns.Add(valueColumn)
yourDataGridView.Columns.Add(factorColumn)
yourDataGridView.Columns.Add(factorColumn)

' Create collection of 12 items and bind it to DataGridView

Dim items = Enumerable.Range(0, 12).Select(Function(i) New Item())
Dim source = new BindingList(Of Item)(items)
yourDataGridView.DataSource = source;

使用上面的方法,DataGridView将显示12个文本框,12个组合框和12个带有选择结果的文本框。当您更改组合框的选择或更新DataGridView单元格中的值时,Result属性将更新

如果您将items变量作为表单或其他类的成员,您将能够访问代码中的结果,这样您就可以从所需的位置访问它。