Insert in ListBox

  • Thread starter Thread starter Carlo
  • Start date Start date
C

Carlo

I need to be able to insert an item above the item highlighted in a list box.
I'm clueless as to how to do it?
Please help
carlob1
 
Carlo said:
I need to be able to insert an item above the item highlighted in a list box.
I'm clueless as to how to do it?

Try:

Select Case ListBox1.SelectedIndex
Case < 0: ListBox1.Items.Add(item)
Case Else: ListBox1.Items.Insert(ListBox1.SelectedIndex, item)
End Select

Hopefully, that should work.

Good luck,
Jeremy
 
Hi Jeremy,

You are real a case select user, nothing wrong with your code (it is even
very nice with that test for the first row) however I never saw this what
in my opinion mostly is written like this.

If ListBox1.SelectedIndex < 0 then
ListBox1.Items.Add(item)
else
ListBox1.Items.Insert(ListBox1.SelectedIndex, item)
end if

Just for you to show you this alternative.
And if you know it, for the OP.

:-)

Cor
 
Cor said:
You are real a case select user, nothing wrong with your code (it is even
very nice with that test for the first row)

Yeah, I really do use Select Case as much as possible. First, I find
that the code is much more readable (it uses fewer lines and has less
staggered indentation). For situations where there may be quite a few
possible options, it also gives the compiler a chance to optimize the
branching, as opposed to performing umpteen consecutive If statements.

Second, it may improve performance in other ways. If I do "Select Case
MyProperty," it only has to retrieve the value of MyProperty once. With "If
MyProperty = A ... ElseIf MyProperty = B...", it has to reevaluate it for
every test. You have no way of knowing how complex that property may be, so
the alternative is to cache a local copy before each If statement to get the
same benefit. That's extra code that just clutters things up.

Third, it also gives the reader some extra hints about what's being
done. If you see "Select Case myVariable," you know that the only value
that matters for all of the Case statements is myVariable. With a bunch of
If/ElseIf statements, you have to look at every case, because there may be
some weird exception hidden in there somewhere.

Jeremy
 
Hi Jeremy,

With me it is exact the oposite, in past there where statements which where
the same that could ruin your complete program flow.

Now I do not see any problem anymore using them, however I saw them seldom
used in samples.

I have to force myself to use them, but there are situations where they are
definitly better and I agree that in some circumstances they are for
documentation better.

Although that new "andalso" and "orelse" have done something that eliminates
that again.

(In the case of this problem I prefer for that documentatio reason the "if",
but that is maybe because I am so used to is)..

Greath language that VB.net, we can do both and give no reason what is
better, I like that.

:-)

Cor
 
Back
Top