[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]
public class Form1 : Form
{
BindingList<foo> bl = new BindingList<foo>();
public Form1()
{
InitializeComponent();
bl.AllowNew = true;
bl.Add(new foo("egg", 1));
bl.Add(new foo("sock", 2));
bl.Add(new foo("bottle", 3));
this.comboBox1.DataSource = bl;
this.comboBox1.SelectedIndex = 0;
this.comboBox1.SelectedValueChanged += new
EventHandler(comboBox1_SelectedValueChanged);
}
void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
this.Text = this.comboBox1.SelectedValue.ToString();
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed;
otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
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.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(34, 43);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ComboBox comboBox1;
}
class foo
{
public foo(string n, int v)
{
name = n;
value = v;
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
int value;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
public override string ToString()
{
return name +":"+ value.ToString();
}
}
--
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Fallen" <sw.tej...@gmail.com> wrote in message
news:1179717022.8...@36g2000prm.googlegroups.com...
Thanks, Bob.
On May 21, 2:22 pm, "Bob Powell [MVP]" <b...@spamkillerbobpowell.net>
wrote:
> Works fine when data binding is used instead of list initialisation...
>
regards,
Fallen