-2

現在、各 Case ステートメントに同じコードがあり、50 回繰り返す必要がある場合もあります。これを単純化する方法はありますか。 Excel の Visual Basic 6.5 でこのコードを作成する

For iVar = 1 To nrVars
    Select Case iVar
    Case 1
        VarFrame1.Caption = varInfo(iVar).varName
        VarFrame1.Top = lastRow
        VarFrame1.Left = leftMargin
        VarFrame1.Height = 12
        VarFrame1.Visible = True
    Case 2
        VarFrame2.Caption = varInfo(iVar).varName
        VarFrame2.Top = lastRow
        VarFrame2.Left = leftMargin
        VarFrame2.Height = 12
        VarFrame2.Visible = True

.... to Case 50

End Select

 lastRow = lastRow + 15

    Dim res As Boolean
Select Case varInfo(iVar).varType
    Case "RadioButton"
        nrOptionButtonVals = nrOptionButtonVals + 1
        res = SetUpOptionButtons(lastRow, iVar, nrOptionButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, False)
    Case "RadioButtonOther"
        nrOptionButtonVals = nrOptionButtonVals + 1
        res = SetUpOptionButtons(lastRow, iVar, nrOptionButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, True)
    Case "DropDown"
        res = SetUpDropDown(lastRow, iVar, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
    Case "CheckBox"
        nrCBButtonVals = nrCBButtonVals + 1
        res = SetUpCBButtons(lastRow, iVar, nrCBButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, False)
    Case "CheckBoxOther"
        nrCBButtonVals = nrCBButtonVals + 1
        res = SetUpCBButtons(lastRow, iVar, nrCBButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, True)
    Case "TextEntry"
        res = SetUpTextEntry(lastRow, iVar, varInfo(iVar).varType, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
    Case "DateEntry"
        res = SetUpTextEntry(lastRow, iVar, varInfo(iVar).varType, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
    Case "NumberEntry"
        res = SetUpTextEntry(lastRow, iVar, varInfo(iVar).varType, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
    End Select
    lastRow = lastRow + 10
Next iVar
QuitButton.Top = lastRow
lastRow = lastRow + 30
PhraseDisplay.Height = lastRow + 50

DisplayCorrectedPhrase
SetUpForm = True
End Sub

ティム・ウィリアムズのコメントで更新:

Public Function SetUpForm(ByRef thePhrase As String) As Boolean
    Dim nrVars, iVar, lastRow As Integer

    nrVars = Utilities.getNrPhraseVariables(thePhrase)
    ReDim varInfo(0 To nrVars)
    PhraseBoxOriginal.Text = thePhrase

    For iVar = 1 To nrVars
    varInfo(iVar).varName = Utilities.getPhraseVariable(thePhrase, iVar)
    varInfo(iVar).varIndex = PhraseVars.getPhraseVarIndex(varInfo(iVar).varName)
    varInfo(iVar).varType = PhraseVars.getTypeFromIndex(varInfo(iVar).varIndex)
    varInfo(iVar).varOptions = PhraseVars.getOptionsFromIndex(varInfo(iVar).varIndex)
    varInfo(iVar).varOptionText = PhraseVars.getOptionTextFromIndex(varInfo(iVar).varIndex)
    varInfo(iVar).varValue = Utilities.getVarOption("", 0) ' Get the default option string
    Next iVar

    nrOptionButtonVals = 0
    lastRow = 115
    For iVar = 1 To nrVars

      With Me.Controls("VarFrame" & iVar)
        .Caption = varInfo(iVar).varName
        .Top = lastRow
        .Left = leftMargin
        .Height = 12
        .Visible = True
      End With

    Next iVar
        lastRow = lastRow + 15

        Dim res As Boolean
    Select Case varInfo(iVar).varType
        Case "RadioButton"
            nrOptionButtonVals = nrOptionButtonVals + 1
            res = SetUpOptionButtons(lastRow, iVar, nrOptionButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, False)
        Case "RadioButtonOther"
            nrOptionButtonVals = nrOptionButtonVals + 1
            res = SetUpOptionButtons(lastRow, iVar, nrOptionButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, True)
        Case "DropDown"
            res = SetUpDropDown(lastRow, iVar, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
        Case "CheckBox"
            nrCBButtonVals = nrCBButtonVals + 1
            res = SetUpCBButtons(lastRow, iVar, nrCBButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, False)
        Case "CheckBoxOther"
            nrCBButtonVals = nrCBButtonVals + 1
            res = SetUpCBButtons(lastRow, iVar, nrCBButtonVals, varInfo(iVar).varOptions, varInfo(iVar).varOptionText, True)
        Case "TextEntry"
            res = SetUpTextEntry(lastRow, iVar, varInfo(iVar).varType, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
        Case "DateEntry"
            res = SetUpTextEntry(lastRow, iVar, varInfo(iVar).varType, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
        Case "NumberEntry"
            res = SetUpTextEntry(lastRow, iVar, varInfo(iVar).varType, varInfo(iVar).varOptions, varInfo(iVar).varOptionText)
        End Select
        lastRow = lastRow + 10
    Next iVar
    QuitButton.Top = lastRow
    lastRow = lastRow + 30
    PhraseDisplay.Height = lastRow + 50

    DisplayCorrectedPhrase
    SetUpForm = True
    End Sub
4

3 に答える 3

4
For iVar = 1 To nrVars     

  With Me.Controls("VarFrame" & iVar)        
    .Caption = varInfo(iVar).varName         
    .Top = lastRow         
    .Left = leftMargin         
    .Height = 12         
    .Visible = True 
  End With

Next iVar
于 2012-09-10T16:34:25.497 に答える
1

いくつかのオプション: 1. 実行時に追加されたコントロールの配列を使用します。2. 起動時に、これらの各コントロールをリストに追加してから、リストを反復処理するだけです。

于 2012-09-10T16:23:08.230 に答える
0

すべてのフレームを 1 つのコレクションにまとめて、それらを繰り返し処理し、次のように初期化できるようにする必要があります。

Sub initFrame(ByRef frame as VarFrame)
    frame.Caption = varInfo(iVar).varName
    frame.Top = lastRow
    frame.Left = leftMargin
    frame.Height = 12
    frame.Visible = True
End Sub

For Each element As VarFrame In frames
    initFrame(element)
Next

そうすれば、多くのフレームを同じ方法で初期化する場合、コードを複製する必要がなくなります。

于 2012-09-10T16:29:24.213 に答える