Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2006, 11:44 PM
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Combobox SelectedIndex Issue

When you add the following code in Combobox, you would see an unusual effect in the cmbobox.

this.comboBox1.Items.AddRange(new object[] {"same","test","same"});

this.comboBox1.SelectedIndex = 2;

And when you try to view the data in the dropdown list (clicking arrow next to the control) you would find that actually the first data gets selected. And if you have selected Value 1,2,3 and when you hit enter you would find that the selected value is 1 instead of 3.

How to resolve this issue so that the proper data gets selected in the combobox?

Thanks in advance...


ruleDworld
 
Old July 21st, 2006, 10:37 AM
Authorized User
 
Join Date: Jul 2004
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post
Default

kancha,

When are you loading the range into the comboBox1?

www.CoderForRent.com
Get A Computer Job!

www.ComputersComplete.com
Computer Parts & Accessories
 
Old July 22nd, 2006, 03:42 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When u use DataSource & Addrange property, SelectedItem, SelectedIndex etc property don't works

Bijgupt
 
Old July 23rd, 2006, 11:47 PM
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by coderforrent.com
 kancha,

When are you loading the range into the comboBox1?

www.CoderForRent.com
Get A Computer Job!

www.ComputersComplete.com
Computer Parts & Accessories
************************************************** ******************
Actually in my application i load the value from a dataset, however this problem exists even in simple data as initially loaded at component initialization...........
************************************************** ******************

ruleDworld
 
Old July 23rd, 2006, 11:57 PM
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by bijgupt
 When u use DataSource & Addrange property, SelectedItem, SelectedIndex etc property don't works

Bijgupt
************************************************** *******************
Is there any way to work around this issue...???
************************************************** ********************

ruleDworld
 
Old July 24th, 2006, 09:14 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In place of adding range use loop.
Fetch data one by one through data reader and use (within a loop)
ComboBox1.Items.Add(<Value>);
Just After the loop
ComboBox1.SelectedIndex=<any valid index>;

Bijgupt
 
Old July 24th, 2006, 05:42 PM
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another option is to instantiate the array first, then add it to the combo box:

string[] people = new string[3];
.
.
people[0] = "Fred";
people[1] = "Barney";
people[2] = "Wilma";
.
.
comboBox1.Items.AddRange(people);

I tried this, and it worked great for me. Don't forget, the array is 0 (zero) based - the first item is index number 0, not 1. Hope this helps.


 
Old July 26th, 2006, 07:14 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

using loop consume u'r cpu time and array will consume u'r memory,
so u've to compromise one. And also no. of records may not be known at disign time or may vary later, so using static array is not adviseable

Bijgupt





Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedIndex in datalist dhoward VB.NET 2002/2003 Basics 0 August 28th, 2008 10:15 AM
how to set selectedindex azitanosrati General .NET 16 December 1st, 2006 02:25 PM
Datagridview combobox issue aad1 C# 0 May 26th, 2006 01:00 PM
comboBox selectedindex problem ColdFusion C# 4 May 12th, 2006 05:37 AM
Issue Binding to ComboBox. lam_lvl VB.NET 2002/2003 Basics 1 February 22nd, 2006 09:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.