Groups | Blog | Home
all groups > dotnet windows forms > may 2007 >

dotnet windows forms : ComboBox SelectedValue is null


T
5/20/2007 10:12:31 AM
m_list.SelectedValue returns null in MainForm.valueChanged regardless
of which value is selected. Is this expected behaviour? SelectedItem
*does* return the correct value.

[code]
using System;
using System.Windows.Forms;

class Attr { // a simple name value class
string m_name;
string m_value;
public Attr(string name, string value){ m_name = name; m_value =
value; }
public string Name { get { return m_name; } }
public string Value { get { return m_value; } }
}

public class MainForm : Form {
public static void Main(string[] args){
Application.Run(new MainForm());
}

ComboBox m_list;

public MainForm(){
m_list = new ComboBox();
Controls.Add(m_list);
m_list.DisplayMember = "Name";
m_list.ValueMember = "Value";

m_list.Items.Add(new Attr("one", "1"));
m_list.Items.Add(new Attr("two", "2"));
m_list.Items.Add(new Attr("three", "3"));

m_list.SelectedValueChanged += new EventHandler(valueChanged);
}

public void valueChanged(object sender, EventArgs args){
if(m_list.SelectedIndex != -1)
Text = m_list.SelectedValue.ToString();
}
}
[/code]
T
5/20/2007 10:28:36 AM
Sorry about the repeated posts...Google Groups was acting weird
lately.

[quoted text, click to view]

Erick
5/21/2007 5:26:57 PM
Please try to change the

Text = m_list.SelectedValue.ToString();

to

Text = m_list.Text;



using System;
using System.Windows.Forms;


class Attr
{ // a simple name value class
string m_name;
string m_value;
public Attr(string name, string value)
{
m_name = name; m_value =
value; }
public string Name { get { return m_name; } }
public string Value { get { return m_value; } }



}


public class MainForm : Form
{
public static void Main(string[] args)
{
Application.Run(new MainForm());
}

ComboBox m_list;


public MainForm()
{
m_list = new ComboBox();
Controls.Add(m_list);
m_list.DisplayMember = "Name";
m_list.ValueMember = "Value";


m_list.Items.Add(new Attr("one", "1"));
m_list.Items.Add(new Attr("two", "2"));
m_list.Items.Add(new Attr("three", "3"));


m_list.SelectedValueChanged += new EventHandler(valueChanged);
}


public void valueChanged(object sender, EventArgs args)
{
if(m_list.SelectedIndex != -1)
Text = m_list.Text;
}


}
Fallen
5/21/2007 9:25:34 PM
Thanks Eric, but m_list.Text will give me "one", "two" or "three"...
What I want is the value "1", "2" or "3".

I am yet to find an answer. See
http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/browse_thread/thread/f7a201607ee38fae/#

[quoted text, click to view]
AddThis Social Bookmark Button