Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Is there a better way of doing this If/Then loop?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Resolved [RESOLVED] [2005] Is there a better way of doing this If/Then loop?

    I have a ListView that displays some items (it's not important what they are really). However, what IS important is that the items it displays can have a property of "IsEnabled" or not. By default I only want to show the items where IsEnabled is true. I have therefore provided a checkbox for the user to allow him/her to show all the items, enabled or not.

    Now, what I want to do is to show the items that are Disabled in red and enabled items in black. The code below works but it just doesn't look right to me. Is there a better way?

    The code is called everytime the CheckBox is clicked.

    VB Code:
    1. Private Sub GetPackages()
    2.         Me.Cursor = Cursors.WaitCursor
    3.         Me.lv_Packages.Items.Clear()
    4.         Try
    5.             objAltirisClient = CreateObject("Altiris.AeXNSClient", ComputerName)
    6.         Catch ex As Exception
    7.             MessageBox.Show("There was an error connecting to the Altiris Client.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    8.             Me.Cursor = Cursors.Default
    9.             Return
    10.         End Try
    11.         objSWDAgent = objAltirisClient.ClientPolicyMgr.ClientAgent("Altiris.SWD")
    12.         Dim ColAdvertisements As Object = objSWDAgent.Advertisements
    13.  
    14.         For Each objAdvert As Object In ColAdvertisements
    15.  
    16.             Dim LVI As New ListViewItem
    17.             LVI.Tag = objAdvert.ID
    18.             LVI.Text = objAdvert.Name
    19.  
    20.             If Me.check_ShowDisabledPackages.Checked = False Then
    21.  
    22.                 If objAdvert.IsEnabled = 1 Then
    23.                     Me.lv_Packages.Items.Add(LVI)
    24.                 End If
    25.             Else
    26.                 If objAdvert.IsEnabled = 1 Then
    27.                     Me.lv_Packages.Items.Add(LVI)
    28.                 Else
    29.                     LVI.ForeColor = Color.Red
    30.                     Me.lv_Packages.Items.Add(LVI)
    31.                 End If
    32.             End If
    33.         Next
    34. End Sub
    ManagePC - the all-in-one PC management and inventory tool

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] Is there a better way of doing this If/Then loop?

    So it doesn't matter if the checkbox is checked? This spares an If.
    VB 2005, Win Xp Pro sp2

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: [2005] Is there a better way of doing this If/Then loop?

    If it's unchecked it should only list Enabled Packages. If it's checked it should list Disabled and Enabled Packages.

    On Form Load, it's unchecked.
    ManagePC - the all-in-one PC management and inventory tool

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: [2005] Is there a better way of doing this If/Then loop?

    bump
    ManagePC - the all-in-one PC management and inventory tool

  5. #5
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: [2005] Is there a better way of doing this If/Then loop?

    You could alter it into:

    VB Code:
    1. If Me.check_ShowDisabledPackages.Checked = False And objAdvert.IsEnabled = 1 Then
    2.     Me.lv_Packages.Items.Add(LVI)
    3. ElseIf objAdvert.IsEnabled = 1 Then
    4.    Me.lv_Packages.Items.Add(LVI)
    5. Else  ' if objAdvert.IsEnabled <> 1 I'm not sure you need this part
    6.   LVI.ForeColor = Color.Red
    7.   Me.lv_Packages.Items.Add(LVI)
    8. End If
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Is there a better way of doing this If/Then loop?

    Why are you comparing the IsEnabled property to 1? Is it an Integer or a Boolean?

    Assuming that it's a Boolean, as it should be, then this:
    VB Code:
    1. If Me.check_ShowDisabledPackages.Checked = False Then
    2.  
    3.                 If objAdvert.IsEnabled = 1 Then
    4.                     Me.lv_Packages.Items.Add(LVI)
    5.                 End If
    6.             Else
    7.                 If objAdvert.IsEnabled = 1 Then
    8.                     Me.lv_Packages.Items.Add(LVI)
    9.                 Else
    10.                     LVI.ForeColor = Color.Red
    11.                     Me.lv_Packages.Items.Add(LVI)
    12.                 End If
    13.             End If
    can be written much more efficiently like this:
    VB Code:
    1. If objAdvert.IsEnbaled Then
    2.     'The object is enabled so show the item regardless of the check box.
    3.     Me.lv_Packages.Items.Add(LVI)
    4. Else If Me.check_ShowDisabledPackages.Checked Then
    5.     'The item is not enabled but disabled items are to be shown.
    6.     LVI.ForeColor = Color.Red
    7.     Me.lv_Packages.Items.Add(LVI)
    8. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Is there a better way of doing this If/Then loop?

    Actually, looking more closely the whole loop can be done better. There's no point creating items that aren't going to be used:
    VB Code:
    1. Me.lv_Packages.BeginUpdate()
    2.  
    3. For Each objAdvert As Object In ColAdvertisements
    4.     If objAdvert.IsEnabled OrElse Me.check_ShowDisabledPackages.Checked
    5.         Dim lvi As New ListViewItem(CStr(objAdvert.Name))
    6.  
    7.         lvi.Tag = objAdvert.ID
    8.  
    9.         If Not objAdvert.IsEnabled Then
    10.             lvi.ForeColor = Color.Red
    11.         End If
    12.  
    13.         Me.lv_Packages.Items.Add(lvi)
    14.     End If
    15. Next objAdvert
    16.  
    17. Me.lv_Packages.EndUpdate()
    Also, you've got some serious late-binding happening there. What's up with all the 'Object' types?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: [2005] Is there a better way of doing this If/Then loop?

    The IsEnabled property is an integer.

    Yes, I know about the Late Binding issue. The problem is that I am trying to work with the Altiris API interface but the documentation is...well, not very extensive. There is a scripting API which you normally would use with VBScript/Javascript but it seems to work just as well like this.

    It's not ideal admittedly and I'm sure there's a better way but I'm still trying to figure out the Altiris SDK.

    Thanks for the help with the loop though. I've gone with a DataGridView now so the BeginUpdate/EndUpdate thing is no longer valid.
    Last edited by Ginolard; Dec 1st, 2006 at 07:35 AM.
    ManagePC - the all-in-one PC management and inventory tool

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