ComboBox Setting A Default Value

G

Guest

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.
 
G

Guest

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.
 
J

Joey Calisay

Where are you setting the default value of the combo? You should set it on
the Load event of the form.
 
G

Guest

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?
 
G

Guest

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";
}
}
 
G

Guest

I am pleased to oblige in this case as the code is not that extensive and in
the hope that somebody out there knows a solution to setting a default value
within the combobox.

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Globalization;

namespace DLCS.WinControl
{
/// <summary>
/// Summary description for CountryComboControl.
/// </summary>
public class CountryComboControl : System.Windows.Forms.ComboBox
{
private ArrayList _pList = null;
public class CountryCodeItem: IComparable
{

private string _code;
private string _description;
public CountryCodeItem(string code, string description)
{
_code = code;
_description = description;
}
public string Code
{
get
{
return _code;
}
}
public string Description
{
get
{
return _description;
}
}
public int CompareTo (object o)
{
if (!(o is CountryCodeItem)) throw new ArgumentException ("o must be of
type 'MyType'");

CountryCodeItem v = (CountryCodeItem) o;
return _description.CompareTo(v._description);
}

}
private System.ComponentModel.Container components = null;
public CountryComboControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
_pList = new ArrayList();
_pList.Add(new CountryCodeItem("ALB","Albania"));
_pList.Add(new CountryCodeItem("DZA","Algeria"));
_pList.Add(new CountryCodeItem("USA","United States"));
_pList.Sort();
this.DataSource = _pList;
this.ValueMember = "Code";
this.DisplayMember = "Description";
this.SelectedValueChanged +=new
EventHandler(CountryComboControl_SelectedValueChanged);
}
public string CountryCode
{
get
{
if(this.SelectedIndex < 0)
{
this.SelectedValue = "GBR";
}
return (string)this.SelectedValue;
}
set
{
this.SelectedValue = value;
}
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here

// Calling the base class OnPaint
base.OnPaint(pe);
}

private void CountryComboControl_SelectedValueChanged(object sender,
EventArgs e)
{
if(this.SelectedIndex < 0)
{
this.SelectedValue = "GBR";
}
}
}
}
 
S

Sijin Joseph

Hi bob,

I looked through your code and used it in a sample solution, i can see
what your problem is. I think the problem is that inside the ctor of the
control itself you are setting the DataSource of the control, now
when your control is added to a Form, this code will get called before
it is added to the Controls collection of the form, hence the control
does not have a bindingContext at the time the ctor is running which is
causing ur problems.

I have a suggestion, why don't you add items using Items.Add() and to
prevent duplicate values you can set the ComboBox DropDownStyle to
DropDownList.

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

Guest

Unfortunately you don't appear to have read my previous thread. Their are
issues with construction when using the Add (although the default value can
be set) within the IDE the range is captured within the construction as a
result duplicate values can be created as the IDE adds this range to its
InitialiseComponent Code. This code was an attempt to remove that problem but
I still have the problem with setting the default value!!!
 
S

Sijin Joseph

Hi Bob,

I had read your previous post in which you had mentioned the problem
about the IDE, i thought what you were trying to say was that while
editing duplicate values get entered into the combo and for that the
solution i gave was of using a DropDownList style. Although i am unable
to reproduce the IDE duplication problem you mention, this is the code
for the Combo that i am using, i was able to use it on a form without
any problems

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Globalization;

namespace DLCS.WinControl
{
/// <summary>
/// Summary description for CountryComboControl.
/// </summary>
public class CountryComboControl : System.Windows.Forms.ComboBox
{
public class CountryCodeItem: IComparable
{

private string _code;
private string _description;
public CountryCodeItem(string code, string description)
{
_code = code;
_description = description;
}
public string Code
{
get
{
return _code;
}
}
public string Description
{
get
{
return _description;
}
}
public int CompareTo (object o)
{
if (!(o is CountryCodeItem))
throw new ArgumentException ("o must be of type 'MyType'");
CountryCodeItem v = (CountryCodeItem) o;
return _description.CompareTo(v._description);
}

}
private System.ComponentModel.Container components = null;
public CountryComboControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

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

//Store the index of "GBR"
CountryCodeItem defaultItem = new CountryCodeItem("GBR","Great Britain");
_pList.Add(defaultItem);

_pList.Sort();
this.Items.AddRange(_pList.ToArray());
this.ValueMember = "Code";
this.DisplayMember = "Description";
this.SelectedItem = defaultItem;

}

public string CountryCode
{
get
{
return (string)this.SelectedValue;
}
set
{
this.SelectedValue = value;
}
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// CountryComboControl
//
this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

}
#endregion
}
}

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

Guest

There are two elements that you may not be duplicating. Try using it as a
component in the tool kit rather than independantly. Also this does not occur
on first use but is a result of editing the form a number of times. Try
changing some of the properties of the combobox a few times.

I have not spent sufficient time on this to provide a foolproof method of
generating the problem as I have a system to get out the door and hoped for
some simple workaround like the use of the datasource which would allow this
combobox to be used as a black box component between projects.
 
G

Guest

Having applied this change shown I have been unable to reproduce the former
errant behaviour. So this solution has been found. I still do not understand
why a similar code segment using Items.Add failed but I think this is a
subject for a new posting in
microsoft.public.dotnet.framework.windowsforms.controls once I have done some
further research
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top