473,856 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem setting "SelectedVa lue" in ComboBox using .Net CF

Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list
to a ComboBox control using the "DataSource " property. I have set the
DisplaySource and ValueMember properties as well. The control populates well,
both the display values, and selected values.

However, when i try to "set" the SelectedValue or SelectedIndex properties,
nothing happens.... The default blank value in the ComboBox is always
selected. My code is:

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight) ;
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.GotF ocus += new EventHandler(Hi deSIP);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;

Any ideas why this doesn't work?

Thanks,
Nov 16 '05 #1
7 20774
The following code worked for me.

// Bind to the ComboBox. Called from a Button Click handler.
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
this.comboBox1. DataSource = ar_Gender;
this.comboBox1. DisplayMember = "LongText";
this.comboBox1. ValueMember = "DefaultVal ue";

// CreateArrayList s definition. Inside the main Form scope.
private class CreateArrayList s
{
private string longText;
private string shortText;
private int defaultValue;

public CreateArrayList s(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultVal ue = defaultValue;
}

public string LongText
{
get
{
return longText;
}
set
{
longText = value;
}
}

public string ShortText
{
get
{
return shortText;
}
set
{
shortText = value;
}
}

public int DefaultValue
{
get
{
return defaultValue;
}
set
{
defaultValue = value;
}
}
}

// Called from another Button Click handler.
// Selects the "Female" item.
this.comboBox1. SelectedValue = 1;

If this code does not work as expected for you, and if you haven't already,
download and install SP3.
http://www.microsoft.com/downloads/d...displaylang=en

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource " property. I have set the
DisplaySource and ValueMember properties as well. The control populates well, both the display values, and selected values.

However, when i try to "set" the SelectedValue or SelectedIndex properties, nothing happens.... The default blank value in the ComboBox is always
selected. My code is:

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight) ;
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.GotF ocus += new EventHandler(Hi deSIP);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;

Any ideas why this doesn't work?

Thanks,

Nov 16 '05 #2
I have installed SP3 and this still does not work. I have tested this on an
external device and my emulator with the same negative results (the emulator
does not have SP3 installed however).

When i try force the selected item via the "SelectedIn dex" property i get
the following error: A first chance exception of type 'System.Excepti on'
occurred in System.Windows. Forms.dll

Could there be a bug w/ my VS 2003?

"Tim Wilson" wrote:
The following code worked for me.

// Bind to the ComboBox. Called from a Button Click handler.
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
this.comboBox1. DataSource = ar_Gender;
this.comboBox1. DisplayMember = "LongText";
this.comboBox1. ValueMember = "DefaultVal ue";

// CreateArrayList s definition. Inside the main Form scope.
private class CreateArrayList s
{
private string longText;
private string shortText;
private int defaultValue;

public CreateArrayList s(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultVal ue = defaultValue;
}

public string LongText
{
get
{
return longText;
}
set
{
longText = value;
}
}

public string ShortText
{
get
{
return shortText;
}
set
{
shortText = value;
}
}

public int DefaultValue
{
get
{
return defaultValue;
}
set
{
defaultValue = value;
}
}
}

// Called from another Button Click handler.
// Selects the "Female" item.
this.comboBox1. SelectedValue = 1;

If this code does not work as expected for you, and if you haven't already,
download and install SP3.
http://www.microsoft.com/downloads/d...displaylang=en

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this

list
to a ComboBox control using the "DataSource " property. I have set the
DisplaySource and ValueMember properties as well. The control populates

well,
both the display values, and selected values.

However, when i try to "set" the SelectedValue or SelectedIndex

properties,
nothing happens.... The default blank value in the ComboBox is always
selected. My code is:

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight) ;
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.GotF ocus += new EventHandler(Hi deSIP);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;

Any ideas why this doesn't work?

Thanks,


Nov 16 '05 #3
If it happens on the device then I wouldn't think that VS.Net 2003 would
have anything to do with it. What device are you using? What is the OS on
the device? And you are sure that you have successfully installed SP3
(http://wiki.opennetcf.org/ow.asp?Com...iningVersion)?
If everything looks to be in place then if you could come up with a small
project that reliably reproduces the behavior and post it back to this
newsgroup then I'll work through it with you. Also post back a description
of what you are doing and what you expect to happen.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:F5******** *************** ***********@mic rosoft.com...
I have installed SP3 and this still does not work. I have tested this on an external device and my emulator with the same negative results (the emulator does not have SP3 installed however).

When i try force the selected item via the "SelectedIn dex" property i get
the following error: A first chance exception of type 'System.Excepti on'
occurred in System.Windows. Forms.dll

Could there be a bug w/ my VS 2003?

"Tim Wilson" wrote:
The following code worked for me.

// Bind to the ComboBox. Called from a Button Click handler.
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
this.comboBox1. DataSource = ar_Gender;
this.comboBox1. DisplayMember = "LongText";
this.comboBox1. ValueMember = "DefaultVal ue";

// CreateArrayList s definition. Inside the main Form scope.
private class CreateArrayList s
{
private string longText;
private string shortText;
private int defaultValue;

public CreateArrayList s(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultVal ue = defaultValue;
}

public string LongText
{
get
{
return longText;
}
set
{
longText = value;
}
}

public string ShortText
{
get
{
return shortText;
}
set
{
shortText = value;
}
}

public int DefaultValue
{
get
{
return defaultValue;
}
set
{
defaultValue = value;
}
}
}

// Called from another Button Click handler.
// Selects the "Female" item.
this.comboBox1. SelectedValue = 1;

If this code does not work as expected for you, and if you haven't already, download and install SP3.
http://www.microsoft.com/downloads/d...displaylang=en
--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Using .Net CF, i have created a 2 dimension ArrayList, and "binded"
this list
to a ComboBox control using the "DataSource " property. I have set the
DisplaySource and ValueMember properties as well. The control
populates well,
both the display values, and selected values.

However, when i try to "set" the SelectedValue or SelectedIndex

properties,
nothing happens.... The default blank value in the ComboBox is always
selected. My code is:

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight) ;
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.GotF ocus += new EventHandler(Hi deSIP);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;

Any ideas why this doesn't work?

Thanks,


Nov 16 '05 #4
Thanks for the fast response. Here's some more info:

Device: HP iPAQ 4300
OS: Windows Mobile 2003
..Net CF: 1.0.4292.00

A simple project example is:

using System;
using System.Drawing;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;

namespace ComboBoxTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
LoadSubjectData Form();
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.mainMenu1 = new System.Windows. Forms.MainMenu( );
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run (new Form1());
}

/// <summary>
/// Load patient data form to edit info or add info
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadSubjectData Form()
{

// Declare and instantialize main controls
Label lblGender = new Label();
ComboBox cboxGender = new ComboBox();

lblGender.Size = new Size(100, 20);
lblGender.Locat ion = new Point(10, 50);
lblGender.Text = "Gender:";

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));

cboxGender.Size = new Size(100, 20);
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Sele ctedIndex = 1;
// Add controls to panelMain
this.Controls.A dd(lblGender);
this.Controls.A dd(cboxGender);
}

}

public class CreateArrayList s
{

#region Create Array Lists

private string myLongText ;
private string myShortText ;
private int myDefaultValue ;

public CreateArrayList s(string strLongText, string strShortText, int
strDefaultValue )
{
this.myLongText = strLongText;
this.myShortTex t = strShortText;
this.myDefaultV alue = strDefaultValue ;
}

public string LongText
{
get
{
return myLongText;
}
}

public string ShortText
{

get
{
return myShortText ;
}
}

public int DefaultValue
{
get
{
return myDefaultValue;
}
}

#endregion

}

}

"Tim Wilson" wrote:
If it happens on the device then I wouldn't think that VS.Net 2003 would
have anything to do with it. What device are you using? What is the OS on
the device? And you are sure that you have successfully installed SP3
(http://wiki.opennetcf.org/ow.asp?Com...iningVersion)?
If everything looks to be in place then if you could come up with a small
project that reliably reproduces the behavior and post it back to this
newsgroup then I'll work through it with you. Also post back a description
of what you are doing and what you expect to happen.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:F5******** *************** ***********@mic rosoft.com...
I have installed SP3 and this still does not work. I have tested this on

an
external device and my emulator with the same negative results (the

emulator
does not have SP3 installed however).

When i try force the selected item via the "SelectedIn dex" property i get
the following error: A first chance exception of type 'System.Excepti on'
occurred in System.Windows. Forms.dll

Could there be a bug w/ my VS 2003?

"Tim Wilson" wrote:
The following code worked for me.

// Bind to the ComboBox. Called from a Button Click handler.
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
this.comboBox1. DataSource = ar_Gender;
this.comboBox1. DisplayMember = "LongText";
this.comboBox1. ValueMember = "DefaultVal ue";

// CreateArrayList s definition. Inside the main Form scope.
private class CreateArrayList s
{
private string longText;
private string shortText;
private int defaultValue;

public CreateArrayList s(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultVal ue = defaultValue;
}

public string LongText
{
get
{
return longText;
}
set
{
longText = value;
}
}

public string ShortText
{
get
{
return shortText;
}
set
{
shortText = value;
}
}

public int DefaultValue
{
get
{
return defaultValue;
}
set
{
defaultValue = value;
}
}
}

// Called from another Button Click handler.
// Selects the "Female" item.
this.comboBox1. SelectedValue = 1;

If this code does not work as expected for you, and if you haven't already, download and install SP3.
http://www.microsoft.com/downloads/d...displaylang=en
--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
> Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list
> to a ComboBox control using the "DataSource " property. I have set the
> DisplaySource and ValueMember properties as well. The control populates well,
> both the display values, and selected values.
>
> However, when i try to "set" the SelectedValue or SelectedIndex
properties,
> nothing happens.... The default blank value in the ComboBox is always
> selected. My code is:
>
> // Get Gender Array List
> ArrayList ar_Gender = new ArrayList();
> ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
> ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
> ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
> cboxGender.Size = new Size(100, kControlHeight) ;
> cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
> cboxGender.GotF ocus += new EventHandler(Hi deSIP);
> cboxGender.Data Source = ar_Gender;
> cboxGender.Disp layMember = "LongText";
> cboxGender.Valu eMember = "DefaultVal ue";
> cboxGender.Font = kFontSmall;
> if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;
>
> Any ideas why this doesn't work?
>
> Thanks,


Nov 16 '05 #5
Set the SelectedIndex in the Forms Load event instead. In other words, move
the line "cboxGender.Sel ectedIndex = 1;" into the Form Load event or the
OnLoad override method. You'll also need to ensure that the cboxGender
ComboBox is scoped to the Form so that you can access it from Load. It
appeared to work for me since I set the SelectedIndex in a Button Click
event handler and not in the constructor.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:E5******** *************** ***********@mic rosoft.com...
Thanks for the fast response. Here's some more info:

Device: HP iPAQ 4300
OS: Windows Mobile 2003
.Net CF: 1.0.4292.00

A simple project example is:

using System;
using System.Drawing;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;

namespace ComboBoxTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
LoadSubjectData Form();
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.mainMenu1 = new System.Windows. Forms.MainMenu( );
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run (new Form1());
}

/// <summary>
/// Load patient data form to edit info or add info
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadSubjectData Form()
{

// Declare and instantialize main controls
Label lblGender = new Label();
ComboBox cboxGender = new ComboBox();

lblGender.Size = new Size(100, 20);
lblGender.Locat ion = new Point(10, 50);
lblGender.Text = "Gender:";

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));

cboxGender.Size = new Size(100, 20);
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Sele ctedIndex = 1;
// Add controls to panelMain
this.Controls.A dd(lblGender);
this.Controls.A dd(cboxGender);
}

}

public class CreateArrayList s
{

#region Create Array Lists

private string myLongText ;
private string myShortText ;
private int myDefaultValue ;

public CreateArrayList s(string strLongText, string strShortText, int
strDefaultValue )
{
this.myLongText = strLongText;
this.myShortTex t = strShortText;
this.myDefaultV alue = strDefaultValue ;
}

public string LongText
{
get
{
return myLongText;
}
}

public string ShortText
{

get
{
return myShortText ;
}
}

public int DefaultValue
{
get
{
return myDefaultValue;
}
}

#endregion

}

}

"Tim Wilson" wrote:
If it happens on the device then I wouldn't think that VS.Net 2003 would
have anything to do with it. What device are you using? What is the OS on
the device? And you are sure that you have successfully installed SP3
(http://wiki.opennetcf.org/ow.asp?Com...iningVersion)? If everything looks to be in place then if you could come up with a small project that reliably reproduces the behavior and post it back to this
newsgroup then I'll work through it with you. Also post back a description of what you are doing and what you expect to happen.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:F5******** *************** ***********@mic rosoft.com...
I have installed SP3 and this still does not work. I have tested this on
an
external device and my emulator with the same negative results (the

emulator
does not have SP3 installed however).

When i try force the selected item via the "SelectedIn dex" property i
get the following error: A first chance exception of type 'System.Excepti on' occurred in System.Windows. Forms.dll

Could there be a bug w/ my VS 2003?

"Tim Wilson" wrote:

> The following code worked for me.
>
> // Bind to the ComboBox. Called from a Button Click handler.
> ArrayList ar_Gender = new ArrayList();
> ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
> ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
> ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
> this.comboBox1. DataSource = ar_Gender;
> this.comboBox1. DisplayMember = "LongText";
> this.comboBox1. ValueMember = "DefaultVal ue";
>
> // CreateArrayList s definition. Inside the main Form scope.
> private class CreateArrayList s
> {
> private string longText;
> private string shortText;
> private int defaultValue;
>
> public CreateArrayList s(string longText, string shortText, int
> defaultValue)
> {
> this.longText = longText;
> this.shortText = shortText;
> this.defaultVal ue = defaultValue;
> }
>
> public string LongText
> {
> get
> {
> return longText;
> }
> set
> {
> longText = value;
> }
> }
>
> public string ShortText
> {
> get
> {
> return shortText;
> }
> set
> {
> shortText = value;
> }
> }
>
> public int DefaultValue
> {
> get
> {
> return defaultValue;
> }
> set
> {
> defaultValue = value;
> }
> }
> }
>
> // Called from another Button Click handler.
> // Selects the "Female" item.
> this.comboBox1. SelectedValue = 1;
>
> If this code does not work as expected for you, and if you haven't

already,
> download and install SP3.
>

http://www.microsoft.com/downloads/d...displaylang=en
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "charliewes t" <ch*********@di scussions.micro soft.com> wrote in message > news:87******** *************** ***********@mic rosoft.com...
> > Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this
> list
> > to a ComboBox control using the "DataSource " property. I have set

the > > DisplaySource and ValueMember properties as well. The control

populates
> well,
> > both the display values, and selected values.
> >
> > However, when i try to "set" the SelectedValue or SelectedIndex
> properties,
> > nothing happens.... The default blank value in the ComboBox is always > > selected. My code is:
> >
> > // Get Gender Array List
> > ArrayList ar_Gender = new ArrayList();
> > ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
> > ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
> > ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
> > cboxGender.Size = new Size(100, kControlHeight) ;
> > cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
> > cboxGender.GotF ocus += new EventHandler(Hi deSIP);
> > cboxGender.Data Source = ar_Gender;
> > cboxGender.Disp layMember = "LongText";
> > cboxGender.Valu eMember = "DefaultVal ue";
> > cboxGender.Font = kFontSmall;
> > if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;
> >
> > Any ideas why this doesn't work?
> >
> > Thanks,
>
>
>


Nov 16 '05 #6
Yes this worked. What i actually ended up doing was moving the
"selectedva lue" property to the very last line in the same function. How very
strange... Thanks for your help!

"Tim Wilson" wrote:
Set the SelectedIndex in the Forms Load event instead. In other words, move
the line "cboxGender.Sel ectedIndex = 1;" into the Form Load event or the
OnLoad override method. You'll also need to ensure that the cboxGender
ComboBox is scoped to the Form so that you can access it from Load. It
appeared to work for me since I set the SelectedIndex in a Button Click
event handler and not in the constructor.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:E5******** *************** ***********@mic rosoft.com...
Thanks for the fast response. Here's some more info:

Device: HP iPAQ 4300
OS: Windows Mobile 2003
.Net CF: 1.0.4292.00

A simple project example is:

using System;
using System.Drawing;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;

namespace ComboBoxTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
LoadSubjectData Form();
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.mainMenu1 = new System.Windows. Forms.MainMenu( );
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run (new Form1());
}

/// <summary>
/// Load patient data form to edit info or add info
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadSubjectData Form()
{

// Declare and instantialize main controls
Label lblGender = new Label();
ComboBox cboxGender = new ComboBox();

lblGender.Size = new Size(100, 20);
lblGender.Locat ion = new Point(10, 50);
lblGender.Text = "Gender:";

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));

cboxGender.Size = new Size(100, 20);
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Sele ctedIndex = 1;
// Add controls to panelMain
this.Controls.A dd(lblGender);
this.Controls.A dd(cboxGender);
}

}

public class CreateArrayList s
{

#region Create Array Lists

private string myLongText ;
private string myShortText ;
private int myDefaultValue ;

public CreateArrayList s(string strLongText, string strShortText, int
strDefaultValue )
{
this.myLongText = strLongText;
this.myShortTex t = strShortText;
this.myDefaultV alue = strDefaultValue ;
}

public string LongText
{
get
{
return myLongText;
}
}

public string ShortText
{

get
{
return myShortText ;
}
}

public int DefaultValue
{
get
{
return myDefaultValue;
}
}

#endregion

}

}

"Tim Wilson" wrote:
If it happens on the device then I wouldn't think that VS.Net 2003 would
have anything to do with it. What device are you using? What is the OS on the device? And you are sure that you have successfully installed SP3
(http://wiki.opennetcf.org/ow.asp?Com...iningVersion)? If everything looks to be in place then if you could come up with a small project that reliably reproduces the behavior and post it back to this
newsgroup then I'll work through it with you. Also post back a description of what you are doing and what you expect to happen.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:F5******** *************** ***********@mic rosoft.com...
> I have installed SP3 and this still does not work. I have tested this on an
> external device and my emulator with the same negative results (the
emulator
> does not have SP3 installed however).
>
> When i try force the selected item via the "SelectedIn dex" property i get > the following error: A first chance exception of type 'System.Excepti on' > occurred in System.Windows. Forms.dll
>
> Could there be a bug w/ my VS 2003?
>
> "Tim Wilson" wrote:
>
> > The following code worked for me.
> >
> > // Bind to the ComboBox. Called from a Button Click handler.
> > ArrayList ar_Gender = new ArrayList();
> > ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
> > ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
> > ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
> > this.comboBox1. DataSource = ar_Gender;
> > this.comboBox1. DisplayMember = "LongText";
> > this.comboBox1. ValueMember = "DefaultVal ue";
> >
> > // CreateArrayList s definition. Inside the main Form scope.
> > private class CreateArrayList s
> > {
> > private string longText;
> > private string shortText;
> > private int defaultValue;
> >
> > public CreateArrayList s(string longText, string shortText, int
> > defaultValue)
> > {
> > this.longText = longText;
> > this.shortText = shortText;
> > this.defaultVal ue = defaultValue;
> > }
> >
> > public string LongText
> > {
> > get
> > {
> > return longText;
> > }
> > set
> > {
> > longText = value;
> > }
> > }
> >
> > public string ShortText
> > {
> > get
> > {
> > return shortText;
> > }
> > set
> > {
> > shortText = value;
> > }
> > }
> >
> > public int DefaultValue
> > {
> > get
> > {
> > return defaultValue;
> > }
> > set
> > {
> > defaultValue = value;
> > }
> > }
> > }
> >
> > // Called from another Button Click handler.
> > // Selects the "Female" item.
> > this.comboBox1. SelectedValue = 1;
> >
> > If this code does not work as expected for you, and if you haven't
already,
> > download and install SP3.
> >
http://www.microsoft.com/downloads/d...displaylang=en > >
> > --
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "charliewes t" <ch*********@di scussions.micro soft.com> wrote in message > > news:87******** *************** ***********@mic rosoft.com...
> > > Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this
> > list
> > > to a ComboBox control using the "DataSource " property. I have set the > > > DisplaySource and ValueMember properties as well. The control
populates
> > well,
> > > both the display values, and selected values.
> > >
> > > However, when i try to "set" the SelectedValue or SelectedIndex
> > properties,
> > > nothing happens.... The default blank value in the ComboBox is always > > > selected. My code is:
> > >
> > > // Get Gender Array List
> > > ArrayList ar_Gender = new ArrayList();
> > > ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
> > > ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
> > > ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
> > > cboxGender.Size = new Size(100, kControlHeight) ;
> > > cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
> > > cboxGender.GotF ocus += new EventHandler(Hi deSIP);
> > > cboxGender.Data Source = ar_Gender;
> > > cboxGender.Disp layMember = "LongText";
> > > cboxGender.Valu eMember = "DefaultVal ue";
> > > cboxGender.Font = kFontSmall;
> > > if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;
> > >
> > > Any ideas why this doesn't work?
> > >
> > > Thanks,
> >
> >
> >


Nov 16 '05 #7
Glad to help. And just for future reference, the CF has its own official
newsgroup (microsoft.publ ic.dotnet.frame work.compactfra mework), and since
this is a more targeted newsgroup you will typically get a faster response.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:4B******** *************** ***********@mic rosoft.com...
Yes this worked. What i actually ended up doing was moving the
"selectedva lue" property to the very last line in the same function. How very strange... Thanks for your help!

"Tim Wilson" wrote:
Set the SelectedIndex in the Forms Load event instead. In other words, move
the line "cboxGender.Sel ectedIndex = 1;" into the Form Load event or the
OnLoad override method. You'll also need to ensure that the cboxGender
ComboBox is scoped to the Form so that you can access it from Load. It
appeared to work for me since I set the SelectedIndex in a Button Click
event handler and not in the constructor.

--
Tim Wilson
..Net Compact Framework MVP

"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:E5******** *************** ***********@mic rosoft.com...
Thanks for the fast response. Here's some more info:

Device: HP iPAQ 4300
OS: Windows Mobile 2003
.Net CF: 1.0.4292.00

A simple project example is:

using System;
using System.Drawing;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;

namespace ComboBoxTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
LoadSubjectData Form();
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.mainMenu1 = new System.Windows. Forms.MainMenu( );
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run (new Form1());
}

/// <summary>
/// Load patient data form to edit info or add info
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadSubjectData Form()
{

// Declare and instantialize main controls
Label lblGender = new Label();
ComboBox cboxGender = new ComboBox();

lblGender.Size = new Size(100, 20);
lblGender.Locat ion = new Point(10, 50);
lblGender.Text = "Gender:";

// Get Gender Array List
ArrayList ar_Gender = new ArrayList();
ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));

cboxGender.Size = new Size(100, 20);
cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top);
cboxGender.Data Source = ar_Gender;
cboxGender.Disp layMember = "LongText";
cboxGender.Valu eMember = "DefaultVal ue";
cboxGender.Sele ctedIndex = 1;
// Add controls to panelMain
this.Controls.A dd(lblGender);
this.Controls.A dd(cboxGender);
}

}

public class CreateArrayList s
{

#region Create Array Lists

private string myLongText ;
private string myShortText ;
private int myDefaultValue ;

public CreateArrayList s(string strLongText, string strShortText, int
strDefaultValue )
{
this.myLongText = strLongText;
this.myShortTex t = strShortText;
this.myDefaultV alue = strDefaultValue ;
}

public string LongText
{
get
{
return myLongText;
}
}

public string ShortText
{

get
{
return myShortText ;
}
}

public int DefaultValue
{
get
{
return myDefaultValue;
}
}

#endregion

}

}

"Tim Wilson" wrote:

> If it happens on the device then I wouldn't think that VS.Net 2003 would > have anything to do with it. What device are you using? What is the OS
on
> the device? And you are sure that you have successfully installed
SP3 >

(http://wiki.opennetcf.org/ow.asp?Com...iningVersion)?
> If everything looks to be in place then if you could come up with a

small
> project that reliably reproduces the behavior and post it back to this > newsgroup then I'll work through it with you. Also post back a

description
> of what you are doing and what you expect to happen.
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "charliewes t" <ch*********@di scussions.micro soft.com> wrote in message > news:F5******** *************** ***********@mic rosoft.com...
> > I have installed SP3 and this still does not work. I have tested this on
> an
> > external device and my emulator with the same negative results
(the > emulator
> > does not have SP3 installed however).
> >
> > When i try force the selected item via the "SelectedIn dex" property i
get
> > the following error: A first chance exception of type

'System.Excepti on'
> > occurred in System.Windows. Forms.dll
> >
> > Could there be a bug w/ my VS 2003?
> >
> > "Tim Wilson" wrote:
> >
> > > The following code worked for me.
> > >
> > > // Bind to the ComboBox. Called from a Button Click handler.
> > > ArrayList ar_Gender = new ArrayList();
> > > ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
> > > ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
> > > ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
> > > this.comboBox1. DataSource = ar_Gender;
> > > this.comboBox1. DisplayMember = "LongText";
> > > this.comboBox1. ValueMember = "DefaultVal ue";
> > >
> > > // CreateArrayList s definition. Inside the main Form scope.
> > > private class CreateArrayList s
> > > {
> > > private string longText;
> > > private string shortText;
> > > private int defaultValue;
> > >
> > > public CreateArrayList s(string longText, string shortText, int
> > > defaultValue)
> > > {
> > > this.longText = longText;
> > > this.shortText = shortText;
> > > this.defaultVal ue = defaultValue;
> > > }
> > >
> > > public string LongText
> > > {
> > > get
> > > {
> > > return longText;
> > > }
> > > set
> > > {
> > > longText = value;
> > > }
> > > }
> > >
> > > public string ShortText
> > > {
> > > get
> > > {
> > > return shortText;
> > > }
> > > set
> > > {
> > > shortText = value;
> > > }
> > > }
> > >
> > > public int DefaultValue
> > > {
> > > get
> > > {
> > > return defaultValue;
> > > }
> > > set
> > > {
> > > defaultValue = value;
> > > }
> > > }
> > > }
> > >
> > > // Called from another Button Click handler.
> > > // Selects the "Female" item.
> > > this.comboBox1. SelectedValue = 1;
> > >
> > > If this code does not work as expected for you, and if you
haven't > already,
> > > download and install SP3.
> > >
>

http://www.microsoft.com/downloads/d...displaylang=en
> > >
> > > --
> > > Tim Wilson
> > > ..Net Compact Framework MVP
> > >
> > > "charliewes t" <ch*********@di scussions.micro soft.com> wrote in

message
> > > news:87******** *************** ***********@mic rosoft.com...
> > > > Using .Net CF, i have created a 2 dimension ArrayList, and

"binded"
> this
> > > list
> > > > to a ComboBox control using the "DataSource " property. I have set the
> > > > DisplaySource and ValueMember properties as well. The control
> populates
> > > well,
> > > > both the display values, and selected values.
> > > >
> > > > However, when i try to "set" the SelectedValue or

SelectedIndex > > > properties,
> > > > nothing happens.... The default blank value in the ComboBox is

always
> > > > selected. My code is:
> > > >
> > > > // Get Gender Array List
> > > > ArrayList ar_Gender = new ArrayList();
> > > > ar_Gender.Add(n ew CreateArrayList s(" ---- ", " - ", -1));
> > > > ar_Gender.Add(n ew CreateArrayList s("Male", "M", 0));
> > > > ar_Gender.Add(n ew CreateArrayList s("Female", "F", 1));
> > > > cboxGender.Size = new Size(100, kControlHeight) ;
> > > > cboxGender.Loca tion = new Point(lblGender .Right, lblGender.Top); > > > > cboxGender.GotF ocus += new EventHandler(Hi deSIP);
> > > > cboxGender.Data Source = ar_Gender;
> > > > cboxGender.Disp layMember = "LongText";
> > > > cboxGender.Valu eMember = "DefaultVal ue";
> > > > cboxGender.Font = kFontSmall;
> > > > if (!bIsNewSubject ) cboxGender.Sele ctedValue = 1;
> > > >
> > > > Any ideas why this doesn't work?
> > > >
> > > > Thanks,
> > >
> > >
> > >
>
>
>


Nov 16 '05 #8

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

Similar topics

2
3624
by: Fred | last post by:
Hi, I am having a problem setting a combobox value in code on form load using comboBox.SelectedValue method. The code below when debugging show the combobox equal to an undefined value. The value I am trying to set is present the items collection. I also have tried using .SelectedItem method and I get an object reference error. Any ideas? comboBox.SelectedValue = comboBoxValue;
1
3630
by: Gary Shell | last post by:
I have a pair of combo boxes on a form. Both have their SelectedValue property bound to a column on a table called "Input_Output". One column is called "Class" and the second is called "SubClass". Each combobox has its datasource, displaymember and SelectedValue member bound to separate tables thru individual datatsets thru individual data adapters. The two tables are "Class" and "SubClass". I use parameterized query to populate the...
30
4619
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
7
4572
by: Simon Verona | last post by:
I posted this in dotnet.languages.vb.controls but thought I'd post here as well.. I have a combobox that is bound to a dataview generated from a dataset. The dataset has a single table (called "Data") with two columns "Id" and "Description". Id contains a code and description contains the description that is displayed in the combobox.
5
3193
by: Peter M. | last post by:
I'm struggling with combobox databinding with something I consider a bug... I'm binding my combobox to an array of structs. The struct exposes two public properties, ID and Name, to be used as the value and displaymember properties. This works fine. My combobox contains the correct data. However I'm also binding my SelectedValue property to a column in a datatable. Here's where I'm lost. The combobox doesn't show the correct
0
2023
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number of different screens down to a minimum, I'm trying to use the same Windows Forms for both browsing and for updating. This works fine for TextBoxes, but I'm running into problems with my DropDownLists (ComboBoxes).
2
7161
by: DesCF | last post by:
I have a textbox and a combobox on a toolstrip. The user enters either an ID in the textbox or selects a name from the combobox. When the user selects a name from the combobox the textbox is filled in automatically by setting its .Text property equal to the .SelectedValue of the combobox. When the user enters an ID in the textbox the combobox's .SelectedValue is set to the .Text value in the ID textbox. All works fine but it is ...
5
5712
by: Crazy Cat | last post by:
Hi all, I have combobox that is bound to a custom object collection thusly Dim collection As List(Of StructureType) = StructureType.FindStructureTypes(SharedObjects.StructureTypes, DropDownListStructure.SelectedValue, DropDownListType.SelectedValue) DropDownListSubType.DataSource = collection DropDownListSubType.DisplayMember = "StructureName"
11
3336
Frinavale
by: Frinavale | last post by:
This question is going to sound a little crazy but.........How do you set the selected item in a ComboBox? I am populating a ComboBox with a bunch of instances of a custom private class: For Each s as Something In MyListOfSomethings MyComboBox.Items.Add(New XYZListItem(s.ID,s.description)) Next MyComboBox.ValueMember = "ID" MyComboBox.DisplayMember = "Description"
0
9918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11060
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10389
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9532
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4578
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4175
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3202
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.