Problem setting "SelectedValue" in ComboBox using .Net CF

G

Guest

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,
 
T

Tim Wilson

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/...11-194b-4c00-b445-f92bec03032f&displaylang=en
 
G

Guest

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

Tim Wilson

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?CompactFrameworkFAQ/DeterminingVersion)?
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.
 
G

Guest

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

}

}
 
T

Tim Wilson

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

Guest

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!
 
T

Tim Wilson

Glad to help. And just for future reference, the CF has its own official
newsgroup (microsoft.public.dotnet.framework.compactframework), and since
this is a more targeted newsgroup you will typically get a faster response.
 
Top