Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to preselect items in a C# ListBox Control 1

Status
Not open for further replies.

sbing

Programmer
Mar 30, 2004
9
US
How do you highlight/preselect items in a C# ListBox and where is the best place in the code to do so – Form_Load, Control_Load methods?

Here is the code that I tried but it did not work:

public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//Fill in lists ******* Is there a better place to do this? *************
//lstFrom.SelectedIndex = 1;

lstFrom.Items.Add("BTU/min");
. . . . . . . . .
lstFrom.SetSelected(1,true);
lstFrom.Update();
 
sbing,
The Load event is the best place to put the code you want, unless you want to highlight the item everytime the form receives the focus, in which case you would use the Activate event.

You would do something like this:
Code:
[COLOR=blue]private void[/color] frmMain_Load([COLOR=blue]object[/color] sender,
                          System.EventArgs e)
{
  lstFrom.Items.Add("BTU/min");
  lstFrom.SetSelected([b]0[/b], [COLOR=blue]true[/color]);
  [COLOR=green]// The line below is not needed
  //lstFrom.Update();[/color] 
}
There are two things you should keep in mind:

1 - Arrays and collections in C# are are zero-based. Thus, the very first item is at index 0, not 1.

2 - If you want to select multiple items of a ListBox control, you must set its SelectionMode property to either MultiSimple, or MultiExtended. The default value of this property is "One" and you wouldn't be able to select multiple items at once unless you pick one of the values just mentioned.

Now, when you say that your code did not work, what exactly did you mean? Did you get any error messages? Where the list box items not selected?

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
JC,

I tried your suggestion. I cut and paste the frmMain_Load method from your reply into my form class, but it never seemed to be executed. I enabled debug and placed a breakpoint on the lstFrom.Items.Add("BTU/min"); statement and the program execution never stopped there. Am I putting this code in the right place. I simply added it as another method in the frmMain class.

To answer your other question – “when you say that your code did not work, what exactly did you mean?” - I am expecting to see an items in the ‘From’ listbox and an item in the ‘To’ listbox selected, in other words highlighted. I wanted to set some default selections, just incase the user does not actively select something from the listbox.

sbing…
 
sbing,
sbing said:
I tried your suggestion. I cut and paste the frmMain_Load method from your reply into my form class, but it never seemed to be executed. I enabled debug and placed a breakpoint on the lstFrom.Items.Add("BTU/min"); statement and the program execution never stopped there. Am I putting this code in the right place. I simply added it as another method in the frmMain class.
The code I gave you was meant to be placed in the Form_Load event. To create an event, you have to add a line that looks something like this:

this.Load += new EventHandler(this.frmMain_Load)

This line tells the run-time to call the method called frmMain_Load when the form loads (that's what this.Load means). If the line above is missing, your method will never be executed.

Now, the easiest way to add an event to a form (or more general to a Control) is by doing this:

1 - Select the form or control you wish to add the event to.

2 - Bring up the properties window

3 - Click on the lightning icon on the top of the properties window. This will show you all the events for the selected control.

4 - Double click the name of the event to which you want to respond and the IDE will add the line I was talking about before.

You should place the code I gave you in the Load event of your form. To do this, select the form and follow the instructions outlined above. When you do step 4, an empty method is created for you, where you can write the code for your event. At that point, take the code I gave you (just the method body, not the declaration) and paste it inside the auto-generated method created. It should work without a problem.

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
JC,

Worked as advertised! I did exactly what you described. I used the this.Load statement and moved all of my ListBox initialization code into the this.frmMain_Load method and “everyone lived happily ever after.”

Thanks...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top