Results 1 to 4 of 4

Thread: Can this code be reduced?

  1. #1

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104

    Talking Can this code be reduced?

    VB Code:
    1. Private Sub SetSingle(bEnabled As Boolean)
    2.     Dim iCtr As Integer
    3.     grdSingle.Enabled = bEnabled
    4.     If bEnabled = False Then
    5.         For iCtr = 0 To btnSingle.Count - 1
    6.             btnSingle(iCtr).Enabled = False
    7.         Next iCtr
    8.     Else
    9.         If iMode = enumEDIT_FILTER Or iMode = enumVIEW_FILTER Then
    10.             If FilterList.Filters(FilterList.Selected).GetSCCount = 0 Then
    11.                 btnSingle(2).Enabled = True
    12.                 btnSingle(1).Enabled = False
    13.                 btnSingle(0).Enabled = False
    14.             ElseIf FilterList.Filters(FilterList.Selected).GetSCCount = 1 Then
    15.                 btnSingle(0).Enabled = False
    16.                 btnSingle(1).Enabled = True
    17.                 btnSingle(2).Enabled = True
    18.             Else
    19.                 btnSingle(2).Enabled = True
    20.                 btnSingle(1).Enabled = True
    21.                 btnSingle(0).Enabled = True
    22.             End If
    23.         Else
    24.             If grdSingle.Rows = 1 Then
    25.                 btnSingle(2).Enabled = True
    26.                 btnSingle(1).Enabled = False
    27.                 btnSingle(0).Enabled = False
    28.             ElseIf grdSingle.Rows = 2 Then
    29.                 btnSingle(0).Enabled = False
    30.                 btnSingle(1).Enabled = True
    31.                 btnSingle(2).Enabled = True
    32.             Else
    33.                 btnSingle(2).Enabled = True
    34.                 btnSingle(1).Enabled = True
    35.                 btnSingle(0).Enabled = True
    36.             End If
    37.         End If
    38.     End If
    39. End Sub
    Last edited by debbie_82; Jan 30th, 2004 at 01:50 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You could build lookup tables for matching the value against the minimum enabled control index, then use a single loop to enable them all.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    based on part of what CornedBee said, you could do this:
    VB Code:
    1. Private Sub SetSingle(bEnabled As Boolean)
    2.  
    3. Dim iCtr As Integer
    4. Dim iMinBtn As Integer
    5.     grdSingle.Enabled = bEnabled
    6.     If bEnabled = False Then
    7.         iMinBtn = btnSingle.Count
    8.     Else
    9.         If iMode = enumEDIT_FILTER Or iMode = enumVIEW_FILTER Then
    10.             If FilterList.Filters(FilterList.Selected).GetSCCount = 0 Then
    11.                 iMinBtn = 2
    12.             ElseIf FilterList.Filters(FilterList.Selected).GetSCCount = 1 Then
    13.                 iMinBtn = 1
    14.             Else
    15.                 iMinBtn = 0
    16.             End If
    17.         Else
    18.             If grdSingle.Rows = 1 Then
    19.                 iMinBtn = 2
    20.             ElseIf grdSingle.Rows = 2 Then
    21.                 iMinBtn = 1
    22.             Else
    23.                 iMinBtn = 0
    24.             End If
    25.         End If
    26.     End If
    27.  
    28.     For iCtr = 0 To iMinBtn - 1
    29.         btnSingle(iCtr).Enabled = False
    30.     Next iCtr
    31.     For iCtr = iMinBtn To btnSingle.Count - 1
    32.         btnSingle(iCtr).Enabled = True
    33.     Next iCtr
    34.    
    35. End Sub

  4. #4

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    Wow, just shows why I like programming.

    I better practice more...

    Thank you for some cool insights.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width