473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem setting "SelectedValue" 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(new CreateArrayLists(" ---- ", " - ", -1));
ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.GotFocus += new EventHandler(HideSIP);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject) cboxGender.SelectedValue = 1;

Any ideas why this doesn't work?

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

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

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

public CreateArrayLists(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.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(new CreateArrayLists(" ---- ", " - ", -1));
ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.GotFocus += new EventHandler(HideSIP);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject) cboxGender.SelectedValue = 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 "SelectedIndex" property i get
the following error: A first chance exception of type 'System.Exception'
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(new CreateArrayLists(" ---- ", " - ", -1));
ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
this.comboBox1.DataSource = ar_Gender;
this.comboBox1.DisplayMember = "LongText";
this.comboBox1.ValueMember = "DefaultValue";

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

public CreateArrayLists(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.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(new CreateArrayLists(" ---- ", " - ", -1));
ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.GotFocus += new EventHandler(HideSIP);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject) cboxGender.SelectedValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.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 "SelectedIndex" property i get
the following error: A first chance exception of type 'System.Exception'
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(new CreateArrayLists(" ---- ", " - ", -1));
ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
this.comboBox1.DataSource = ar_Gender;
this.comboBox1.DisplayMember = "LongText";
this.comboBox1.ValueMember = "DefaultValue";

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

public CreateArrayLists(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.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(new CreateArrayLists(" ---- ", " - ", -1));
ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
cboxGender.Size = new Size(100, kControlHeight);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.GotFocus += new EventHandler(HideSIP);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.Font = kFontSmall;
if (!bIsNewSubject) cboxGender.SelectedValue = 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.Collections;
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
//
InitializeComponent();

//
LoadSubjectDataForm();
//
}
/// <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 InitializeComponent()
{
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 LoadSubjectDataForm()
{

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

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

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

cboxGender.Size = new Size(100, 20);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.SelectedIndex = 1;
// Add controls to panelMain
this.Controls.Add(lblGender);
this.Controls.Add(cboxGender);
}

}

public class CreateArrayLists
{

#region Create Array Lists

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

public CreateArrayLists(string strLongText, string strShortText, int
strDefaultValue)
{
this.myLongText = strLongText;
this.myShortText = strShortText;
this.myDefaultValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.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 "SelectedIndex" property i get
the following error: A first chance exception of type 'System.Exception'
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(new CreateArrayLists(" ---- ", " - ", -1));
ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
this.comboBox1.DataSource = ar_Gender;
this.comboBox1.DisplayMember = "LongText";
this.comboBox1.ValueMember = "DefaultValue";

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

public CreateArrayLists(string longText, string shortText, int
defaultValue)
{
this.longText = longText;
this.shortText = shortText;
this.defaultValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.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(new CreateArrayLists(" ---- ", " - ", -1));
> ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> cboxGender.Size = new Size(100, kControlHeight);
> cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
> cboxGender.GotFocus += new EventHandler(HideSIP);
> cboxGender.DataSource = ar_Gender;
> cboxGender.DisplayMember = "LongText";
> cboxGender.ValueMember = "DefaultValue";
> cboxGender.Font = kFontSmall;
> if (!bIsNewSubject) cboxGender.SelectedValue = 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.SelectedIndex = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.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.Collections;
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
//
InitializeComponent();

//
LoadSubjectDataForm();
//
}
/// <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 InitializeComponent()
{
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 LoadSubjectDataForm()
{

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

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

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

cboxGender.Size = new Size(100, 20);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.SelectedIndex = 1;
// Add controls to panelMain
this.Controls.Add(lblGender);
this.Controls.Add(cboxGender);
}

}

public class CreateArrayLists
{

#region Create Array Lists

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

public CreateArrayLists(string strLongText, string strShortText, int
strDefaultValue)
{
this.myLongText = strLongText;
this.myShortText = strShortText;
this.myDefaultValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.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 "SelectedIndex" property i
get the following error: A first chance exception of type 'System.Exception' 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(new CreateArrayLists(" ---- ", " - ", -1));
> ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> this.comboBox1.DataSource = ar_Gender;
> this.comboBox1.DisplayMember = "LongText";
> this.comboBox1.ValueMember = "DefaultValue";
>
> // CreateArrayLists definition. Inside the main Form scope.
> private class CreateArrayLists
> {
> private string longText;
> private string shortText;
> private int defaultValue;
>
> public CreateArrayLists(string longText, string shortText, int
> defaultValue)
> {
> this.longText = longText;
> this.shortText = shortText;
> this.defaultValue = 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
>
> "charliewest" <ch*********@discussions.microsoft.com> wrote in message > news:87**********************************@microsof t.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(new CreateArrayLists(" ---- ", " - ", -1));
> > ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> > ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> > cboxGender.Size = new Size(100, kControlHeight);
> > cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
> > cboxGender.GotFocus += new EventHandler(HideSIP);
> > cboxGender.DataSource = ar_Gender;
> > cboxGender.DisplayMember = "LongText";
> > cboxGender.ValueMember = "DefaultValue";
> > cboxGender.Font = kFontSmall;
> > if (!bIsNewSubject) cboxGender.SelectedValue = 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
"selectedvalue" 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.SelectedIndex = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.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.Collections;
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
//
InitializeComponent();

//
LoadSubjectDataForm();
//
}
/// <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 InitializeComponent()
{
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 LoadSubjectDataForm()
{

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

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

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

cboxGender.Size = new Size(100, 20);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.SelectedIndex = 1;
// Add controls to panelMain
this.Controls.Add(lblGender);
this.Controls.Add(cboxGender);
}

}

public class CreateArrayLists
{

#region Create Array Lists

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

public CreateArrayLists(string strLongText, string strShortText, int
strDefaultValue)
{
this.myLongText = strLongText;
this.myShortText = strShortText;
this.myDefaultValue = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.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 "SelectedIndex" property i get > the following error: A first chance exception of type 'System.Exception' > 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(new CreateArrayLists(" ---- ", " - ", -1));
> > ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> > ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> > this.comboBox1.DataSource = ar_Gender;
> > this.comboBox1.DisplayMember = "LongText";
> > this.comboBox1.ValueMember = "DefaultValue";
> >
> > // CreateArrayLists definition. Inside the main Form scope.
> > private class CreateArrayLists
> > {
> > private string longText;
> > private string shortText;
> > private int defaultValue;
> >
> > public CreateArrayLists(string longText, string shortText, int
> > defaultValue)
> > {
> > this.longText = longText;
> > this.shortText = shortText;
> > this.defaultValue = 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
> >
> > "charliewest" <ch*********@discussions.microsoft.com> wrote in message > > news:87**********************************@microsof t.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(new CreateArrayLists(" ---- ", " - ", -1));
> > > ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> > > ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> > > cboxGender.Size = new Size(100, kControlHeight);
> > > cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
> > > cboxGender.GotFocus += new EventHandler(HideSIP);
> > > cboxGender.DataSource = ar_Gender;
> > > cboxGender.DisplayMember = "LongText";
> > > cboxGender.ValueMember = "DefaultValue";
> > > cboxGender.Font = kFontSmall;
> > > if (!bIsNewSubject) cboxGender.SelectedValue = 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.public.dotnet.framework.compactframewor k), and since
this is a more targeted newsgroup you will typically get a faster response.

--
Tim Wilson
..Net Compact Framework MVP

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
Yes this worked. What i actually ended up doing was moving the
"selectedvalue" 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.SelectedIndex = 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

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.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.Collections;
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
//
InitializeComponent();

//
LoadSubjectDataForm();
//
}
/// <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 InitializeComponent()
{
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 LoadSubjectDataForm()
{

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

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

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

cboxGender.Size = new Size(100, 20);
cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
cboxGender.DataSource = ar_Gender;
cboxGender.DisplayMember = "LongText";
cboxGender.ValueMember = "DefaultValue";
cboxGender.SelectedIndex = 1;
// Add controls to panelMain
this.Controls.Add(lblGender);
this.Controls.Add(cboxGender);
}

}

public class CreateArrayLists
{

#region Create Array Lists

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

public CreateArrayLists(string strLongText, string strShortText, int
strDefaultValue)
{
this.myLongText = strLongText;
this.myShortText = strShortText;
this.myDefaultValue = 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
>
> "charliewest" <ch*********@discussions.microsoft.com> wrote in message > news:F5**********************************@microsof t.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 "SelectedIndex" property i
get
> > the following error: A first chance exception of type

'System.Exception'
> > 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(new CreateArrayLists(" ---- ", " - ", -1));
> > > ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> > > ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> > > this.comboBox1.DataSource = ar_Gender;
> > > this.comboBox1.DisplayMember = "LongText";
> > > this.comboBox1.ValueMember = "DefaultValue";
> > >
> > > // CreateArrayLists definition. Inside the main Form scope.
> > > private class CreateArrayLists
> > > {
> > > private string longText;
> > > private string shortText;
> > > private int defaultValue;
> > >
> > > public CreateArrayLists(string longText, string shortText, int
> > > defaultValue)
> > > {
> > > this.longText = longText;
> > > this.shortText = shortText;
> > > this.defaultValue = 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
> > >
> > > "charliewest" <ch*********@discussions.microsoft.com> wrote in

message
> > > news:87**********************************@microsof t.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(new CreateArrayLists(" ---- ", " - ", -1));
> > > > ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> > > > ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> > > > cboxGender.Size = new Size(100, kControlHeight);
> > > > cboxGender.Location = new Point(lblGender.Right, lblGender.Top); > > > > cboxGender.GotFocus += new EventHandler(HideSIP);
> > > > cboxGender.DataSource = ar_Gender;
> > > > cboxGender.DisplayMember = "LongText";
> > > > cboxGender.ValueMember = "DefaultValue";
> > > > cboxGender.Font = kFontSmall;
> > > > if (!bIsNewSubject) cboxGender.SelectedValue = 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
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...
1
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"....
30
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...
7
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...
5
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...
0
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...
2
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...
5
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,...
11
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...
0
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,...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.