listbox SelectedIndexChanged on load?
Hello, I'm newer to C# and I'm making a windows form where a listbox lists customers and populates txtboxes to the right if the index changes, er is supposed to because it does it on load. Where would this be caused to fill in even when the form loads and the index hasn't changed?
26 Replies
I've verified this is the only space those get populated because if I comment that section out nothing loads there
In unselected state the index is -1, 0 is the first item selected, since this is zero-based-index.
Is there an item selected? -> Yes -> Your condition will result in
true
how can I have it not start selected/ at 0?
It depends on how you fill the
ListBox
itemsit's being filled from an XML file
No, I mean is it filled by setting the
DataSource
or manipulating the Items
listdata source
if I comment it out here they disappear
this may clarify
Setting the
DataSource
on a ListBox
will make an automatic selection on the first item 0. This is somewhat a ListBox
bug, you could workaround by manipulating the Items
list or workaround bool when changing the DataSource
.That's weird, doesn't make it seem very indexchanged-ey, can you think of an easy way to make sure the txtboxes on the right side stay blank unless something's actually selected?
Yes, as written before.
it might be wacky but could I just "" the textboxes after the refresh() is called in form load?
Or just do
AddRange(customers)
, since it seems it's an Array
/List
.hmmm the bool thing didn't seem to work
It does, try stepping through the code.
I might just have inserted it incorrectly, how do you add code here?
$codegif
Your
Refresh
method lacks the bool set to true
.
like that^?
holy shit that worked
You can delete the thingy in the
Load
method, this was for demo purpose.
It's only important to set the bool
to true
before changing the DataSource
. So the Event
won't do it's logic.ok nice, weird workaround but a workaround nonetheless
thank you for your help!