473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SetSelected on listbox working oddly

I have a simple winform with a listbox on it. I dynamically load the
box with integers (the years 1975 to present.) This works fine and I
can see the box has all of the items present. I then do a
mylistbox.SetSelected(0,true) so that the first item starts off
selected. It is a MultiSimple listbox. What happens is nothing. The
form draws and the listbox has the list of years but nothing is
selected. I do the selection in the MainForm() constructor.

Here's another odd bit. If I poke the listbox then it works and the
selection is fine. Thus if I follow up the SetSelected with:

int Kludge=mylistbox.SelectedItems.Count;

then it works. I've tried doing mylistbox.invalidate(),
mylistbox.refresh() and a couple of other things with no success. Am I
calling the SetSelected in the wrong place? If not, what would the
right place to add it be?

Thanks!
--Pete
Nov 16 '05 #1
3 2238
Hi Peter,

You have to call the SetSelected method in the Load event instead of the
constructor. Constructor is a good place for example to initialize objects,
however for any initialization to controls and forms that involve UI
elements should be done in the load event.

Hope it helps.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================
"Peter Mancini" <pm******@yahoo.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
I have a simple winform with a listbox on it. I dynamically load the
box with integers (the years 1975 to present.) This works fine and I
can see the box has all of the items present. I then do a
mylistbox.SetSelected(0,true) so that the first item starts off
selected. It is a MultiSimple listbox. What happens is nothing. The
form draws and the listbox has the list of years but nothing is
selected. I do the selection in the MainForm() constructor.

Here's another odd bit. If I poke the listbox then it works and the
selection is fine. Thus if I follow up the SetSelected with:

int Kludge=mylistbox.SelectedItems.Count;

then it works. I've tried doing mylistbox.invalidate(),
mylistbox.refresh() and a couple of other things with no success. Am I
calling the SetSelected in the wrong place? If not, what would the
right place to add it be?

Thanks!
--Pete

Nov 16 '05 #2
I take it you mean to put the call in an OnLoad event handler for the
Winform? One doesn't exist for the winform automatically so I will
look into seeing how one builds one. Thanks for the lead.

--Pete

Nov 16 '05 #3
Wow that was easier than I thought. I overrode OnLoad as follows and
it worked like a charm:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
FYBox.SetSelected(0,true);
}

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim | last post by:
I have form with a ListBox. That ListBox is bound to a datasource. That ListBox has a SelectionMode set to Multi-Extended. When I try to programatically set multiple items selected via the...
3
by: Andrew | last post by:
I'm having a major problem with a databound listbox in C#. In the constructor for the form I am trying to pre-select some of the items based in information in the database. When I step through the...
0
by: John Wood | last post by:
If you run SetSelected on an invisible ListBox it seems to ignore the call. This is especially annoying when the ListBox is on a tab control and the tab isn't selected at startup... it's impossible...
1
by: Steven M | last post by:
Hello!!! I dont know what is the problem!!! I am desperate!!! I want to put enabled = false a button when in ListBox has selected more than a item. this is the javascript. it dosen't...
4
by: amber | last post by:
Hello I'm not sure if I should give up trying to find an answer here...or just keep posting my problem.. I'm having problems with a listbox.. I have a listbox that is populated when a user...
5
by: Dave | last post by:
Hi All, I have a windows form that contains 2 listboxes and 2 buttons. The listbox on the right is populated by a database routine (This is the easy part). The listbox on the left is populated...
2
by: Henry Padilla | last post by:
I'm trying to select multiple items in a listbox and the demo from the help file is not working. What do I do to select multiple items in a listbox? Code sample below. Tom P. ...
11
by: John Dann | last post by:
I'm still struggling to find a way of reordering the items within the same single listbox with drag and drop. I think I've got the drag working but it's the drop code I can't figure out. What I...
4
by: lgbjr | last post by:
Hi All, I've got a listbox on a VB.NET form. when the form opens, the ListBox SelectionMode is set to Single. while running various routines on the form, items get added to the list box (results...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.