Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

ComboBox Setting A Default Value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BobM - 16 Sep 2004 06:33 GMT
I have derived from a combo box which is linked to a static list of countries
in the constructor I have used the following in the constructor to populate
the box

InitializeComponent();

_pList = new ArrayList();
_pList.Add(new CountryCodeItem("ALB","Albania"));
_pList.Add(new CountryCodeItem("DZA","Algeria"));
_pList.Add(new CountryCodeItem("GBR","United Kingdom"));
_pList.Add(new CountryCodeItem("USA","United States"));

// I then attach it and identify functions to use to manipulate the data
columns.
this.DataSource = _pList;
this.ValueMember = "Code";
this.DisplayMember = "Description";

I then need to set the default to the United Kingdom but at this point item
count is zero, How do I set the default value on initialisation.
Sijin Joseph - 16 Sep 2004 12:25 GMT
How can the item count be zero, when you have added items to the combo?
Can you post some more code?

To select the default item just use this line after setting up the
datasource

this.SelectedIndex = indexOfUK;

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> I have derived from a combo box which is linked to a static list of countries
> in the constructor I have used the following in the constructor to populate
[quoted text clipped - 16 lines]
> I then need to set the default to the United Kingdom but at this point item
> count is zero, How do I set the default value on initialisation.
BobM - 16 Sep 2004 13:13 GMT
I think the reason it is zero is because of Late Binding. I have already
attempted to use the SelectedIndex property which fails because the Items are
not initialised.

Incidently I have also tried to use this.SelectedValue = "GBR" which also
fails for the same reason.

The combobox is initialised correctly as it works as advertised once it is
displayed, it is merely setting the default value which has become a
challenge.

I have also tried adding each of the properties using this.Items.Add()
within the constructor  but this causes problems when using this as a
component within the IDE as values can be duplicated during editing.

I have attempted to use the bindmanager but the bindcontext for this
datasource appears not to exist until after the constructor has been run.

Would appreciate any assistance or code samples.

> How can the item count be zero, when you have added items to the combo?
> Can you post some more code?
[quoted text clipped - 28 lines]
> > I then need to set the default to the United Kingdom but at this point item
> > count is zero, How do I set the default value on initialisation.
Joey Calisay - 16 Sep 2004 16:22 GMT
Where are you setting the default value of the combo? You should set it on
the Load event of the form.

> I think the reason it is zero is because of Late Binding. I have already
> attempted to use the SelectedIndex property which fails because the Items are
[quoted text clipped - 48 lines]
> > > I then need to set the default to the United Kingdom but at this point item
> > > count is zero, How do I set the default value on initialisation.
BobM - 16 Sep 2004 16:53 GMT
I have a generic address usercontrol. I have overriden the OnLoad function
which saves me putting this on every form.

protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
if( this.Country.SelectedIndex<0)
{
this.Country.SelectedValue = "GBR";
}
}

> Where are you setting the default value of the combo? You should set it on
> the Load event of the form.
[quoted text clipped - 56 lines]
> item
> > > > count is zero, How do I set the default value on initialisation.
BobM - 16 Sep 2004 16:29 GMT
I have managed to set the default value external to the ComboBox by setting
the SelectedValue in a custom control which includes the ComboBox below
however this is only a short term solution and I would still like to set the
default value within the original combobox. Any suggestions?

> I think the reason it is zero is because of Late Binding. I have already
> attempted to use the SelectedIndex property which fails because the Items are
[quoted text clipped - 48 lines]
> > > I then need to set the default to the United Kingdom but at this point item
> > > count is zero, How do I set the default value on initialisation.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.