Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

VB2008 Listbox assistance


  • Please log in to reply

#1
dsenette

dsenette

    Je suis Napoléon!

  • Community Leader
  • 26,047 posts
  • MVP
sounds stupid doesn't it? yeah it does.. but none the less

i've got a series of programs that i've written at work to manage certain functions in a simple manner.

they basically use a list box to select a certain machine in the network to perform an action upon (i.e. ping it, restart it, kill it, or reload the software that's running on it) now every time we add a batch of machines to this network (usually 20 at a time) i've got to go in and add all these new machines into the text boxes on all the forms involved then go through and add the subsequent code (if item 25 is selected then do this stuff) for new line....this is tedious...

what i'd like to be able to do is populate all the listboxes from a text file that i can modify (from one location)...that part i think i can do...what i can't do is have the program automatically decide what code to do based on which thing is selected without typing it out with each new item in the list..
  • 0

Advertisements


#2
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
Hey there. Not quite sure that I understand what you are trying to do...

If you are trying to make a decision based on what listbox item is selected, use a SelectedIndexChanged event. Then something like...

If Me.ListBox1.SelectedIndex = 0 Then
(whatever)
ElseIf Me.ListBox1.SelectedIndex = 1 Then
(whatever)
etc.
etc.
End If

As for populating the listbox, if you have a text file for each machine, with the name of the machine in the first line of the text file, all in the same folder, you could do:

Dim lines As New List(Of String)

For Each filePath As String In IO.Directory.GetFiles("folder path here", "*.txt")
	Using reader As New IO.StreamReader(filePath)
		lines.Add(reader.ReadLine())
	End Using
Next

Me.ListBox1.DataSource = lines

Or if it's just one text file with a list of all machines...

Dim lines As New List(Of String)
Dim reader As New IO.StreamReader("file path here")

Do Until reader.ReadLine() = Nothing
	lines.Add(reader.ReadLine())
Loop

Me.ListBox1.DataSource = lines

But I don't think I fully understand what you are trying to do. :)
  • 0

#3
dsenette

dsenette

    Je suis Napoléon!

  • Topic Starter
  • Community Leader
  • 26,047 posts
  • MVP
yeah better description

i've got two programs that do similar things so let's just stick with the biggest one

i've got 4 seperate functions that it can do....it can ping a machine from the list (the listbox), restart a machine, shutdown a machine, or send a pskill/psexecute to the machine to kill and restart a program that's running on the machines...all of these actions are triggered by a cmd button (i'm working on getting them triggered by a double click in the list box which i know how to do...i just didn't do it yet)...

so basically you higlight a machine name in the list and press the "go" button for your selected option..

what i'd like to do is populate the text boxes from a text file (all machines in one text file...which as you've pointed out is rediculously easy)...but i still want to be able to perform my operation on the item that's selected in the list box when i hit the "go" button...right now i've got to write an if listbox1.selected=0 then do something and on and on....which isn't helpfull if the listbox is rellatively dynamic...i need it to know that no matter what i click do this but use the item in the listbox for the operation....

clear as mud right?
  • 0

#4
dsenette

dsenette

    Je suis Napoléon!

  • Topic Starter
  • Community Leader
  • 26,047 posts
  • MVP
Private Sub cmdIndi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIndi.Click
		'ping individual
		If ListBox1.SelectedIndex = 0 Then
			Shell("c:\windows\system32\ping.exe plasma1", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 1 Then
			Shell("c:\windows\system32\ping.exe plasma2", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 2 Then
			Shell("c:\windows\system32\ping.exe aisin-tp1", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 3 Then
			Shell("c:\windows\system32\ping.exe aisin-tp2", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 4 Then
			Shell("c:\windows\system32\ping.exe aisin-tp3", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 5 Then
			Shell("c:\windows\system32\ping.exe aisin-tp4", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 6 Then
			Shell("c:\windows\system32\ping.exe aisin-tp5", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 7 Then
			Shell("c:\windows\system32\ping.exe aisin-tp6", AppWinStyle.MaximizedFocus, True)
		ElseIf ListBox1.SelectedIndex = 8 Then
			Shell("c:\windows\system32\ping.exe aisin-tp7", AppWinStyle.MaximizedFocus, True)
example of what i'm doing....take note that i'm about to have all the way to aisin-tp60...which is why i'm wanting be able to have the code grow on it's own as the text file with the computer names in it grows...so if the text file add's up to tp60 into the listbox that i'm selecting from...then it will know that if listbox1.selectedindex = 60 to ping aisin-tp60
  • 0

#5
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
Well, if the names in the text file (which are then put in the list box) are the same as the network names, couldn't you do something like:

Public networkedMachine As String
(then in the ListBox1.SelectedIndexChanged event)
networkedMachine = Me.ListBox1.SelectedItem
(then in the "ping" (or whatever) button.Click event)
executePinging(networkedMachine)

Sorry if that's ridiculously abstract, as I don't know the specific code you're dealing with. Also sorry if I still don't get what you're trying to do. :)
  • 0

#6
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
Just saw your last post. So something like:

Public networkedMachine As String

(then in the ListBox1.SelectedIndexChanged event)
networkedMachine = Me.ListBox1.SelectedItem

Private Sub cmdIndi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIndi.Click
	Shell("c:\windows\system32\ping.exe " & networkedMachine, AppWinStyle.MaximizedFocus, True)
End Sub

  • 0

#7
dsenette

dsenette

    Je suis Napoléon!

  • Topic Starter
  • Community Leader
  • 26,047 posts
  • MVP
i'll give it a shot when i get a minute....but that makes sense
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP