Control Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dfg

    Control Array

    Hello. I have a control Array of option buttons. I'd like to use them
    in a Case statement, but I can't get it to work. I can only seem to get
    it working if I use an if-then-else structure, like so:

    If optFinishType(0 ) Then
    TotalAccess = TotalAccess + mcurStandard
    ElseIf optFinishType(1 ) Then
    TotalAccess = TotalAccess + mcurPearl
    ElseIf optFinishType(2 ) Then
    TotalAccess = TotalAccess + mcurDetailing
    End If

    This structure is currently in the click-event of a command button, but
    I was wondering if I can place it in the click event of the option
    button. Do you think that would be better?

  • Rick Rothstein

    #2
    Re: Control Array

    > Hello. I have a control Array of option buttons. I'd like to use them[color=blue]
    > in a Case statement, but I can't get it to work. I can only seem to get
    > it working if I use an if-then-else structure, like so:
    >
    > If optFinishType(0 ) Then
    > TotalAccess = TotalAccess + mcurStandard
    > ElseIf optFinishType(1 ) Then
    > TotalAccess = TotalAccess + mcurPearl
    > ElseIf optFinishType(2 ) Then
    > TotalAccess = TotalAccess + mcurDetailing
    > End If[/color]

    If you have it working with an If-Then structure, why do you feel you must
    change it to a Select Case structure? Anyway, this should work

    Select Case True
    Case optFinishType(0 ).Value
    TotalAccess = TotalAccess + mcurStandard
    Case optFinishType(1 ).Value
    TotalAccess = TotalAccess + mcurPearl
    Case optFinishType(2 ).Value
    TotalAccess = TotalAccess + mcurDetailing
    End Select

    I would suggest that you not rely on VB supplying the default property of
    the OptionButton (or any other control, for that matter)... six months from
    now, you control's name **might** look like the name of an ordinary
    variable. You will find it easier to read your code later on if you specify
    all properties for your controls.

    Rick - MVP


    Comment

    • Geoff Turner

      #3
      Re: Control Array

      "dfg" <[email protected]> wrote in message
      news:YL7jb.1038 82$pl3.101496@p d7tw3no...[color=blue]
      > Hello. I have a control Array of option buttons. I'd like to use them
      > in a Case statement, but I can't get it to work. I can only seem to get
      > it working if I use an if-then-else structure, like so:
      >
      > If optFinishType(0 ) Then
      > TotalAccess = TotalAccess + mcurStandard
      > ElseIf optFinishType(1 ) Then
      > TotalAccess = TotalAccess + mcurPearl
      > ElseIf optFinishType(2 ) Then
      > TotalAccess = TotalAccess + mcurDetailing
      > End If
      >
      > This structure is currently in the click-event of a command button, but
      > I was wondering if I can place it in the click event of the option
      > button. Do you think that would be better?
      >[/color]

      Given you have an array of option buttons,
      I would use this code in the optFinishType_C lick() event:

      Private Sub optFinishType_C lick(Index)
      Select Case Index
      Case = 0
      TotalAccess=Tot alAccess+mcurSt andard
      Case = 1
      TotalAccess=Tot alAccess+mcurPe arl
      Case = 2
      TotalAccess=Tot alAccess+mcurDe tailing
      End Select

      End Sub


      Comment

      • J French

        #4
        Re: Control Array

        On Wed, 15 Oct 2003 05:10:08 -0400, "Rick Rothstein"
        <rickNOSPAMnews @NOSPAMcomcast. net> wrote:

        <snip>[color=blue]
        >
        >If you have it working with an If-Then structure, why do you feel you must
        >change it to a Select Case structure? Anyway, this should work
        >
        >Select Case True
        > Case optFinishType(0 ).Value
        > TotalAccess = TotalAccess + mcurStandard
        > Case optFinishType(1 ).Value
        > TotalAccess = TotalAccess + mcurPearl
        > Case optFinishType(2 ).Value
        > TotalAccess = TotalAccess + mcurDetailing
        >End Select[/color]

        I rather suspect that these options are mutually exclusive

        Comment

        • Rick Rothstein

          #5
          Re: Control Array

          > <snip>[color=blue][color=green]
          > >
          > >If you have it working with an If-Then structure, why do you feel you[/color][/color]
          must[color=blue][color=green]
          > >change it to a Select Case structure? Anyway, this should work
          > >
          > >Select Case True
          > > Case optFinishType(0 ).Value
          > > TotalAccess = TotalAccess + mcurStandard
          > > Case optFinishType(1 ).Value
          > > TotalAccess = TotalAccess + mcurPearl
          > > Case optFinishType(2 ).Value
          > > TotalAccess = TotalAccess + mcurDetailing
          > >End Select[/color]
          >
          > I rather suspect that these options are mutually exclusive[/color]

          I'm not sure I understand your comment... did you intend to post more?

          Rick - MVP


          Comment

          • J French

            #6
            Re: Control Array

            On Wed, 15 Oct 2003 08:34:40 -0400, "Rick Rothstein"
            <rickNOSPAMnews @NOSPAMcomcast. net> wrote:
            [color=blue][color=green]
            >> <snip>[color=darkred]
            >> >
            >> >If you have it working with an If-Then structure, why do you feel you[/color][/color]
            >must[color=green][color=darkred]
            >> >change it to a Select Case structure? Anyway, this should work
            >> >
            >> >Select Case True
            >> > Case optFinishType(0 ).Value
            >> > TotalAccess = TotalAccess + mcurStandard
            >> > Case optFinishType(1 ).Value
            >> > TotalAccess = TotalAccess + mcurPearl
            >> > Case optFinishType(2 ).Value
            >> > TotalAccess = TotalAccess + mcurDetailing
            >> >End Select[/color]
            >>
            >> I rather suspect that these options are mutually exclusive[/color]
            >
            >I'm not sure I understand your comment... did you intend to post more?[/color]

            No, the nature of the 'mcur' Properties and the fact that it is an
            array of Option Buttons suggests to me that only one result is
            required.

            I would use a small UserControl ....

            Comment

            • Rick Rothstein

              #7
              Re: Control Array

              > >> <snip>[color=blue][color=green][color=darkred]
              > >> >
              > >> >If you have it working with an If-Then structure, why do you feel you[/color]
              > >must[color=darkred]
              > >> >change it to a Select Case structure? Anyway, this should work
              > >> >
              > >> >Select Case True
              > >> > Case optFinishType(0 ).Value
              > >> > TotalAccess = TotalAccess + mcurStandard
              > >> > Case optFinishType(1 ).Value
              > >> > TotalAccess = TotalAccess + mcurPearl
              > >> > Case optFinishType(2 ).Value
              > >> > TotalAccess = TotalAccess + mcurDetailing
              > >> >End Select
              > >>
              > >> I rather suspect that these options are mutually exclusive[/color]
              > >
              > >I'm not sure I understand your comment... did you intend to post more?[/color]
              >
              > No, the nature of the 'mcur' Properties and the fact that it is an
              > array of Option Buttons suggests to me that only one result is
              > required.
              >
              > I would use a small UserControl ....[/color]

              Perhaps... but if I had to guess from the original question, I'd say the OP
              is not ready to start playing around with UserControls just yet. In any
              event, the OP's question was pretty specific... where does my original reply
              not respond to it? Or am I still missing the point why you posted your
              original comment against my original response?

              Rick - MVP


              Comment

              • J French

                #8
                Re: Control Array

                On Wed, 15 Oct 2003 10:00:50 -0400, "Rick Rothstein"
                <rickNOSPAMnews @NOSPAMcomcast. net> wrote:

                <snip>[color=blue][color=green]
                >>
                >> I would use a small UserControl ....[/color]
                >
                >Perhaps... but if I had to guess from the original question, I'd say the OP
                >is not ready to start playing around with UserControls just yet. In any
                >event, the OP's question was pretty specific... where does my original reply
                >not respond to it? Or am I still missing the point why you posted your
                >original comment against my original response?[/color]

                'mutually exclusive' was the bit I sat up and looked at

                That sort of situation does not need a case statement
                - unless it does something

                .... perhaps an Enum

                Realistically a spot of 'encapsulation' would help the OP
                - and frankly I'm not up to that right now

                Comment

                • Randy Birch

                  #9
                  Re: Control Array

                  If you're using option buttons, and want to detect which is selected from
                  inside a command button's click event, you have two options ... iterate
                  through the control array examining the value property (using a for-next
                  loop) until you determine the index selected, and use that index in your
                  select case statement to execute the appropriate code, or you can create a
                  private function that will return the selected option item index with which
                  you execute the select case. As 'air code' ...

                  '------using a loop------

                  dim cnt as long
                  dim selectedindex as long

                  for cnt = 0 to 2 'since you indicate 3 options
                  if option1(cnt).va lue = true then
                  selectedindex = cnt
                  exit for
                  end if
                  next cnt

                  select case selectedindex
                  case 0
                  case 1
                  etc ...
                  end select


                  '------using a function------
                  select case GetSelectedOpti onIndex()
                  case 0
                  case 1
                  etc ...
                  end select

                  Private Function GetSelectedOpti onIndex() As Long

                  'returns the selected item index from
                  'an option button array. Use in place
                  'of multiple If...Then statements!
                  'If your array contains more elements,
                  'just append them to the test condition,
                  'setting the multiplier to the button's
                  'negative -index.
                  GetSelectedOpti onIndex = Option1(0).Valu e * 0 Or _
                  Option1(1).Valu e * -1 Or _
                  Option1(2).Valu e * -2
                  End Function
                  --

                  Randy Birch
                  MVP Visual Basic

                  Please respond only to the newsgroups so all can benefit.


                  "dfg" <[email protected]> wrote in message
                  news:YL7jb.1038 82$pl3.101496@p d7tw3no...
                  : Hello. I have a control Array of option buttons. I'd like to use them
                  : in a Case statement, but I can't get it to work. I can only seem to get
                  : it working if I use an if-then-else structure, like so:
                  :
                  : If optFinishType(0 ) Then
                  : TotalAccess = TotalAccess + mcurStandard
                  : ElseIf optFinishType(1 ) Then
                  : TotalAccess = TotalAccess + mcurPearl
                  : ElseIf optFinishType(2 ) Then
                  : TotalAccess = TotalAccess + mcurDetailing
                  : End If
                  :
                  : This structure is currently in the click-event of a command button, but
                  : I was wondering if I can place it in the click event of the option
                  : button. Do you think that would be better?
                  :


                  Comment

                  • Bob Butler

                    #10
                    Re: Control Array

                    "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
                    news:[email protected][color=blue]
                    > Select Case True[/color]

                    heathen

                    Comment

                    Working...