获取TexBox的ID ......来计算

时间:2010-05-04 11:04:03

标签: vb.net textbox

我有:

vb代码:

    Private Sub Calculation()

     Dim txt1 As Decimal
            txt1 = (CS_Incoms_done.Text / CS_Incoms_target.Text) * 100
            CS_Incom_Result.Text = "%" + FormatNumber(txt, 2, TriState.False)

            Dim txt2 As Decimal
            txt2 = (CS_GovernmentService_done.Text / CS_GovernmentService_target.Text) * 100
            CS_GovernmentService_Result.Text = "%" + FormatNumber(txt2, 2, TriState.False)

            Dim txt3 As Decimal
            txt3 = (CS_RentBox_done.Text / CS_RentBox_target.Text) * 100
            CS_RentBox_Result.Text = "%" + FormatNumber(txt3, 2, TriState.False)

Dim txt4 As Decimal
            txt4 = (CS_ServiceAdvertising_done.Text / CS_ServiceAdvertising_target.Text) * 100
            CS_ServiceAdvertising_Result.Text = "%" + FormatNumber(txt4, 2, TriState.False)

  Dim txt5 As Decimal
            txt5 = (CS_ServiceCatogray_done.Text / CS_ServiceCatogray_target.Text) * 100
            CS_ServiceCatogray_Result.Text = "%" + FormatNumber(txt5, 2, TriState.False)
    End Sub

我只是向您展示5 textbox's 100 textbox's ....并且不想完成所有textbox's这样的...我想要一个简单的代码来做它..

...正如您所注意到的,textbox's的前两个部分每三个id's看起来就像...... {/ p> 例如

- > CS_ServiceCatogray _Result.TextCS_ServiceCatogray _done.TextCS_ServiceCatogray _target.Text ...

〜..最后一部分在所有文本框中都是相同的,用于对结果进行遗传。> _Result.Text_done.Text_target.Text

所以......我有一个想法,就是把id和相似的两个部分放在一个数组中......并使用For Each之类的东西:

Dim allItems As Array
        For Each item As Control In panel4.Controls
            Select Case item.[GetType]().Name
                Case "TextBox"

                    'here just be sure that this item is not saved in the allItems array ,if it is not  do >>'
                    allItems[Last_Item_Saved_Index+1] = DirectCast(item, TextBox).ID ',  i want  to save just the two Similar parts of the textboxs ids'


'i am not sure if this completely correct, but i wanted to do something like it['
                    Dim partOFtxt As String = allItems[Last_Item_Saved_Index]

                    Dim txt As Decimal = (partOFtxt + "_done.Text") / (partOFtxt + "_target.Text")

                    (partOFtxt + "_Result.Text") = "%" + FormatNumber(txt, 2, TriState.False)  ']'
                    'end condition'

                    Exit Select
                Case Else
        Exit Select
            End Select
        Next

我希望你明白这一点......

如果你有更好的主意......那就太好了..

提前致谢..

4 个答案:

答案 0 :(得分:3)

这是简短而甜蜜的代码,它给出了与答案相同的结果。如果需要澄清,请随意。

Private Sub Calculation()
    Dim CurParent As ContainerControl = Me
    For Each item As Control In CurParent.Controls
        Dim decZeroResult As Decimal = 0
        Select Case item.GetType().Name.ToUpper
            Case "LABEL"
                Dim ControlName As String
                Dim ResultControl As Control = item
                ControlName = ResultControl.Name
                If ControlName.ToUpper.EndsWith("_RESULT") Then
                    Dim ControlPartName As String = Mid(ControlName, 1, ControlName.Length - 7)
                    Dim TargetControl As TextBox = Nothing
                    Dim DoneControl As TextBox = Nothing
                    Dim controlsFound() As Control
                    controlsFound = CurParent.Controls.Find(ControlPartName & "_Target", True)
                    If controlsFound.Length > 0 Then
                        TargetControl = controlsFound(0)
                    End If
                    controlsFound = CurParent.Controls.Find(ControlPartName & "_Done", True)
                    If controlsFound.Length > 0 Then
                        DoneControl = controlsFound(0)
                    End If
                    If TargetControl IsNot Nothing And DoneControl IsNot Nothing Then
                        If TargetControl.Text > 0 Then
                            decZeroResult = FormatNumber(((DoneControl.Text / TargetControl.Text) * 100), 1, TriState.False)
                        Else
                            decZeroResult = 0
                        End If
                        ResultControl.Text = decZeroResult & " %"
                    End If
                End If
        End Select

    Next

End Sub

答案 1 :(得分:2)

编辑:删除部分答案,使其余部分更清晰。

或者如果你想让它们很容易遍历它们,你可以这样做:

在初始化代码中,您将每个文本框与用于计算其值的文本框链接为:

CS_Incom_Result.Tag = New KeyValuePair(Of TextBox, TextBox)(CS_Incoms_done, CS_Incoms_target)

然后您创建一个方法:

Public sub UpdateTextBox(t as TextBox) 
    Dim kvp as KeyValuePair(Of TextBox, TextBox) = CType(t.Tag, KeyValuePair(Of TextBox, TextBox))
    Dim txt as Decimal = (decimal.Parse(kvp.Key.Text) / decimal.Parse(kvp.Value.Text)) * 100
    t.Text = "%" & FormatNumber(txt, 2, TriState.False)
End Function

然后你可以将它们全部更新为:

For Each ctrl As Control In panel4.Controls
    If Typeof ctrl is TextBox Then
        UpdateTextBox(Ctype(ctrl, TextBox)
    Endif
Next

答案 2 :(得分:1)

我明白了...... ^ _ ^:

Private Sub Calculation()
        Dim decZeroResult As Decimal = 1.1
        Dim strCommonID As String
        For Each item As Control In panel4.Controls
            Select Case item.[GetType]().Name
                Case "TextBox"
                    If InStr(item.ID.ToUpper, "_Done".ToUpper) Then
                        strCommonID = Mid(item.ID, 1, InStr(item.ID.ToUpper, "_Done".ToUpper) - 1)
                        Dim MyTxt1 As TextBox
                        Dim MyTxt2 As TextBox
                        MyTxt1 = CType(item, TextBox)
                        For Each item2 As Control In panel4.Controls
                            Select Case item2.[GetType]().Name
                                Case "TextBox"
                                    If item2.ID.ToUpper = strCommonID.ToUpper & "_target".ToUpper Then
                                        MyTxt2 = CType(item2, TextBox)
                                        If CType(item2, TextBox).Text = 0 Then
                                            If CType(item, TextBox).Text = 0 Then
                                                decZeroResult = 0
                                            ElseIf CType(item, TextBox).Text > 0 Then
                                                decZeroResult = 100
                                            End If
                                        ElseIf CType(item2, TextBox).Text > 0 Then
                                            If CType(item, TextBox).Text > 0 Then
                                                decZeroResult = FormatNumber(((MyTxt1.Text / MyTxt2.Text) * 100), 1, TriState.False)
                                            Else
                                                decZeroResult = 0
                                            End If
                                        End If
                                        For Each item3 As Control In panel4.Controls
                                            Select Case item3.[GetType]().Name
                                                Case "Label"
                                                    If item3.ID.ToUpper = strCommonID.ToUpper & "_Result".ToUpper Then

                                                        CType(item3, Label).Text = decZeroResult & " %"

                                                        Exit For
                                                    End If
                                            End Select
                                        Next
                                        Exit For
                                    End If
                            End Select
                        Next
                    End If
            End Select
        Next

        For Each item As Control In panel1.Controls
            Select Case item.[GetType]().Name
                Case "TextBox"
                    If InStr(item.ID.ToUpper, "_Done".ToUpper) Then
                        strCommonID = Mid(item.ID, 1, InStr(item.ID.ToUpper, "_Done".ToUpper) - 1)
                        Dim MyTxt1 As TextBox
                        Dim MyTxt2 As TextBox
                        MyTxt1 = CType(item, TextBox)
                        For Each item2 As Control In panel1.Controls
                            Select Case item2.[GetType]().Name
                                Case "TextBox"
                                    If item2.ID.ToUpper = strCommonID.ToUpper & "_target".ToUpper Then
                                        MyTxt2 = CType(item2, TextBox)
                                        If CType(item2, TextBox).Text = 0 Then
                                            If CType(item, TextBox).Text = 0 Then
                                                decZeroResult = 0
                                            ElseIf CType(item, TextBox).Text > 0 Then
                                                decZeroResult = 100
                                            End If
                                        ElseIf CType(item2, TextBox).Text > 0 Then
                                            If CType(item, TextBox).Text > 0 Then
                                                decZeroResult = FormatNumber(((MyTxt1.Text / MyTxt2.Text) * 100), 1, TriState.False)
                                            Else
                                                decZeroResult = 0
                                            End If
                                        End If
                                        For Each item3 As Control In panel1.Controls
                                            Select Case item3.[GetType]().Name
                                                Case "Label"
                                                    If item3.ID.ToUpper = strCommonID.ToUpper & "_Result".ToUpper Then

                                                        CType(item3, Label).Text = decZeroResult & " %"

                                                        Exit For
                                                    End If
                                            End Select
                                        Next
                                        Exit For
                                    End If
                            End Select
                        Next
                    End If
            End Select
        Next

        For Each item As Control In panel2.Controls
            Select Case item.[GetType]().Name
                Case "TextBox"
                    If InStr(item.ID.ToUpper, "_Done".ToUpper) Then
                        strCommonID = Mid(item.ID, 1, InStr(item.ID.ToUpper, "_Done".ToUpper) - 1)
                        Dim MyTxt1 As TextBox
                        Dim MyTxt2 As TextBox
                        MyTxt1 = CType(item, TextBox)
                        For Each item2 As Control In panel2.Controls
                            Select Case item2.[GetType]().Name
                                Case "TextBox"
                                    If item2.ID.ToUpper = strCommonID.ToUpper & "_target".ToUpper Then
                                        MyTxt2 = CType(item2, TextBox)
                                        If CType(item2, TextBox).Text = 0 Then
                                            If CType(item, TextBox).Text = 0 Then
                                                decZeroResult = 0
                                            ElseIf CType(item, TextBox).Text > 0 Then
                                                decZeroResult = 100
                                            End If
                                        ElseIf CType(item2, TextBox).Text > 0 Then
                                            If CType(item, TextBox).Text > 0 Then
                                                decZeroResult = FormatNumber(((MyTxt1.Text / MyTxt2.Text) * 100), 1, TriState.False)
                                            Else
                                                decZeroResult = 0
                                            End If
                                        End If
                                        For Each item3 As Control In panel2.Controls
                                            Select Case item3.[GetType]().Name
                                                Case "Label"
                                                    If item3.ID.ToUpper = strCommonID.ToUpper & "_Result".ToUpper Then

                                                        CType(item3, Label).Text = decZeroResult & " %"

                                                        Exit For
                                                    End If
                                            End Select
                                        Next
                                        Exit For
                                    End If
                            End Select
                        Next
                    End If
            End Select
        Next

        For Each item As Control In panel3.Controls
            Select Case item.[GetType]().Name
                Case "TextBox"
                    If InStr(item.ID.ToUpper, "_Done".ToUpper) Then
                        strCommonID = Mid(item.ID, 1, InStr(item.ID.ToUpper, "_Done".ToUpper) - 1)
                        Dim MyTxt1 As TextBox
                        Dim MyTxt2 As TextBox
                        MyTxt1 = CType(item, TextBox)
                        For Each item2 As Control In panel3.Controls
                            Select Case item2.[GetType]().Name
                                Case "TextBox"
                                    If item2.ID.ToUpper = strCommonID.ToUpper & "_target".ToUpper Then
                                        MyTxt2 = CType(item2, TextBox)
                                        If CType(item2, TextBox).Text = 0 Then
                                            If CType(item, TextBox).Text = 0 Then
                                                decZeroResult = 0
                                            ElseIf CType(item, TextBox).Text > 0 Then
                                                decZeroResult = 100
                                            End If
                                        ElseIf CType(item2, TextBox).Text > 0 Then
                                            If CType(item, TextBox).Text > 0 Then
                                                decZeroResult = FormatNumber(((MyTxt1.Text / MyTxt2.Text) * 100), 1, TriState.False)
                                            Else
                                                decZeroResult = 0
                                            End If
                                        End If
                                        For Each item3 As Control In panel3.Controls
                                            Select Case item3.[GetType]().Name
                                                Case "Label"
                                                    If item3.ID.ToUpper = strCommonID.ToUpper & "_Result".ToUpper Then

                                                        CType(item3, Label).Text = decZeroResult & " %"

                                                        Exit For
                                                    End If
                                            End Select
                                        Next
                                        Exit For
                                    End If
                            End Select
                        Next
                    End If
            End Select
        Next
    End Sub

答案 3 :(得分:0)

我不认为您可以灵活地创建用户控件,将三个文本框组合在一起并集中计算结果的逻辑?在你后面的代码中,这种方式可以循环使用Panel中的所有控件,使用“is”测试控件类型,强制转换为该类型并调用计算方法。

的伪代码:

Public Class ThreeTextboxUserControl
    Private doneTextBox
    Private targetTextBox
    Private resultTextBox

Public Sub loadValues(doneTextBoxValue, targetTextBoxValue)
Public Sub calculateResult()

在页面代码中:

Public Sub Calculation()
    For Each item As Control In panel4.Controls
        If (item is ThreeTextboxUserControl)
            [Cast item and call calculateResult()]
        End IF
    Next
End Sub