Jump to content

VB List Box Help


Recommended Posts

I have a list box on my form that when Command Button 1 is clicked it gets all the Services.

Private Sub Command1_Click()
 Dim Service As Variant
     For Each Service In GetObject("winmgmts:").ExecQuery("select * from Win32_Service")
       List1.AddItem Service.DisplayName
     Next
   Command1.Enabled = False
End Sub

That gets all the Services and displays them in a Listbox. I got about 80 services on my computer. Now i changed the style too Check box and when the user clicks on a check next too a service it adds it too List Box 2.

Private Sub List1_Click()
List2.AddItem List1.Text
End Sub

But if the user keeps clicking on the Checkbox it shows up multiple times in ListBox2. ... How can i make it so if the Checkbox is Checked in List1 it adds the Item too List2 but if it gets unchecked it removes the item from List2?

Thanks

*Notice how theres multiple "Alerter" services in the second List box*

Link to comment
Share on other sites


oh yea...

with this coding, i can get True or False values for wether the service is Started or Stopped using:

Service.Started

anyway too make it so if Service.Started is True the Checkbox would be Checked. if its False it would be unchecked..?

Link to comment
Share on other sites

You'll want to do something like this:

Private Sub List1_Click()
Dim isFound As Boolean
Dim i As Integer
isFound = False
For i = 0 To (List2.ListCount - 1)
 If List1.ListItems(List1.SelectedItem).Checked Then
  If List2.List(i) = List1.ListItems(List1.SelectedItem).Text Then isFound = True
 Else
  If List2.List(i) = List1.ListItems(List1.SelectedItem).Text Then List2.RemoveItem i
 End If
Next
If List1.ListItems(List1.SelectedItem).Checked And Not isFound Then List2.AddItem List1.Text
End Sub

Link to comment
Share on other sites

You'll want to do something like this:

Private Sub List1_Click()
Dim isFound As Boolean
Dim i As Integer
isFound = False
For i = 0 To (List2.ListCount - 1)
 If List1.ListItems(List1.SelectedItem).Checked Then
  If List2.List(i) = List1.ListItems(List1.SelectedItem).Text Then isFound = True
 Else
  If List2.List(i) = List1.ListItems(List1.SelectedItem).Text Then List2.RemoveItem i
 End If
Next
If List1.ListItems(List1.SelectedItem).Checked And Not isFound Then List2.AddItem List1.Text
End Sub

thanks i'll try it now :D

Link to comment
Share on other sites

hmmm getting an error on this line:

If List1.ListItems(List1.SelectedItem).Checked Then

saying Method or Data Member not found. *this is Visual Basic 6 by the way*

tried to substitute "SelectedItem" for "Seleted" and got "Argument not Optional.

*sorry, still a newb too Visual Basic lol*

EDIT:

Is there a way too make the List box like the attached pic of Msconfig?

Link to comment
Share on other sites

Still working with the "ListView" object insted of the normal "List" a bit more complicated but got the gridlines, columns and all that good stuff. Just need too findout how too make it so that when populating the list if "SubItems" is True then check the box in my list, if its false leave it unchecked.

Private Sub Command1_Click()
 Dim Service As Variant
 Dim i As Integer
   i = 1
     For Each Service In GetObject("winmgmts:").ExecQuery("select * from Win32_Service")
       ListView1.ListItems.Add i, , Service.DisplayName
       ListView1.ListItems(i).SubItems(1) = Service.Started
i = 1 + i
   Next
   Command1.Enabled = False
   ListView1.Sorted = True
End Sub

somewhat primitive but gets the job done.

---Attached Pic---

thats my project in testing mode checkin all that crap and yea... Command 1 populates the List, Command 2 Exits, Command 3 Deletes and service from the "ListView" if they are checked...

Link to comment
Share on other sites

Jcarle, I've been "playin" with the code you gave me and finally got it too "somewhat" work... I changed some of it too:

Private Sub ListView1_Click()
Dim isFound As Boolean
Dim i As Integer
isFound = False
For i = 0 To (List2.ListCount - 1)
If ListView1.SelectedItem.Checked Then
 If List2.List(i) = ListView1.SelectedItem.Text Then isFound = True
Else
 If List2.List(i) = ListView1.SelectedItem.Text Then List2.RemoveItem i
End If
Next
If ListView1.SelectedItem.Checked And Not isFound Then List2.AddItem ListView1.SelectedItem

Now when i have the Listview populated and i click on just the checkbox without selecting the actual text it checks but doesnt add it too the list. But when i finally click on the Text it adds it too "List2"

Overall, its doing what i wanted, but is there anyway that i can make it to when i click on the checkbox it'll select the current line?

Thanks again.

*Edit* If push comes too shove i'll probably just add a timer if thats possible.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...