Listbox1 problem

  • Thread starter Thread starter Jan
  • Start date Start date
J

Jan

Hi there,

I'm a newbee in vb.net with the following problem:

4444528; Baltic Birch; 16.20; 0

I have more of these lines in a ListBox1.
Now I want to change the latest 0 with the value of a NumericUpDown1 (for
ex. 8)
My solution is this:
dim textline as string
dim item(3) as string

textline = lstWoodshop.SelectedItem

item = textline.Split(";")
item(3) = " " & NumericUpDown1.Value
textline = Join(item, ";")

lstWoodshop.Items.Insert(lstWoodshop.SelectedIndex, textline)

so far so good, but the old line with the 0 at the end still stays in the
ListBox1. How can I remove this? I tried many options, but i always get a
error?

Somebody knows they answer?
Thank you very much
 
Jan said:
Hi there,

I'm a newbee in vb.net with the following problem:

4444528; Baltic Birch; 16.20; 0

I have more of these lines in a ListBox1.
Now I want to change the latest 0 with the value of a NumericUpDown1
(for ex. 8)
My solution is this:
dim textline as string
dim item(3) as string

textline = lstWoodshop.SelectedItem

I see, you are not using Option Strict, do you? :-)
item = textline.Split(";")
item(3) = " " & NumericUpDown1.Value
textline = Join(item, ";")

lstWoodshop.Items.Insert(lstWoodshop.SelectedIndex,
textline)

so far so good, but the old line with the 0 at the end still stays in
the ListBox1. How can I remove this? I tried many options, but i
always get a error?

Somebody knows they answer?
Thank you very much

Usually it is better to keep the data in variables, change them, and reflect
the changes in the UI, i.e. in the listbox. There are different approaches.
One is:
1. Write a class containing the data (4444528, "Baltic Birch", etc.).
2. "Override" the ToString function that returns a string to be displayed in
the listbox.
3. Create instances of the class and add them to the listbox. The listbox
calls the ToString method of every object added to get the string to be
shown.

Unfortunately I didn't find a way to make the listbox look for the current
values and display them whenever you change them. So, you can get the item
from the listbox, change the value, remove the item (Items.Remove) and add
it again (Items.Insert). If there is a small number of list items and/or
they change rarely, an alternative is to clear the listbox and fill it again
(to avoid writing a "change item procedure").
 
Hi,

listbox1.items.removeat.

dim textline as string
dim item(3) as string

textline = lstWoodshop.SelectedItem

item = textline.Split(";")
item(3) = " " & NumericUpDown1.Value
textline = Join(item, ";")
lstWoodshop.Items.RemoveAt(lstWoodshop.SelectedIndex)
lstWoodshop.Items.Insert(lstWoodshop.SelectedIndex, textline)
Ken
--------------------------
 
Back
Top