Hi,
I try to learn programming in c# with databinding controls. Now I have a
problem with a ComboBox with the advanced properties for databinding, I
want to set the DataSourceUpdat eMode to OnPropertyChang ed, but then the
combobox does not display the right displaymember.
Below is my sample code (it is working, when you paste it in a new
windowsapplicat ion, in a form and delete some generated code, but it is not
a useful program but it shows my problem). When the checkbox
OnPropertyChang ed is unchecked, you have to click in an other control to see
the changes, but the right displaymember is showed. With OnPropertyChang ed
checked the program react immedealtly but not the right displaymember is
showed.
Who can give me a hint for this problem? Thanks.
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
namespace ComboBoxTest
{
public class Form1 : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er 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.Disp ose();
}
base.Dispose(di sposing);
}
#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.components = new System.Componen tModel.Containe r();
System.Windows. Forms.Label nameLabel;
System.Windows. Forms.Label kindLabel;
System.Windows. Forms.Label speedLabel;
this.nameTextBo x = new System.Windows. Forms.TextBox() ;
this.gameBindin gSource = new
System.Windows. Forms.BindingSo urce(this.compo nents);
this.kindComboB ox = new System.Windows. Forms.ComboBox( );
this.kindListBi ndingSource = new
System.Windows. Forms.BindingSo urce(this.compo nents);
this.speedTextB ox = new System.Windows. Forms.TextBox() ;
this.checkBox1 = new System.Windows. Forms.CheckBox( );
nameLabel = new System.Windows. Forms.Label();
kindLabel = new System.Windows. Forms.Label();
speedLabel = new System.Windows. Forms.Label();
((System.Compon entModel.ISuppo rtInitialize)(t his.gameBinding Source)).BeginI nit();
((System.Compon entModel.ISuppo rtInitialize)(t his.kindListBin dingSource)).Be ginInit();
this.SuspendLay out();
//
// nameLabel
//
nameLabel.AutoS ize = true;
nameLabel.Locat ion = new System.Drawing. Point(26, 22);
nameLabel.Name = "nameLabel" ;
nameLabel.Size = new System.Drawing. Size(38, 13);
nameLabel.TabIn dex = 1;
nameLabel.Text = "Name:";
//
// kindLabel
//
kindLabel.AutoS ize = true;
kindLabel.Locat ion = new System.Drawing. Point(33, 48);
kindLabel.Name = "kindLabel" ;
kindLabel.Size = new System.Drawing. Size(31, 13);
kindLabel.TabIn dex = 2;
kindLabel.Text = "Kind:";
//
// speedLabel
//
speedLabel.Auto Size = true;
speedLabel.Loca tion = new System.Drawing. Point(23, 75);
speedLabel.Name = "speedLabel ";
speedLabel.Size = new System.Drawing. Size(41, 13);
speedLabel.TabI ndex = 4;
speedLabel.Text = "Speed:";
//
// nameTextBox
//
this.nameTextBo x.DataBindings. Add(new
System.Windows. Forms.Binding(" Text", this.gameBindin gSource, "Name", true));
this.nameTextBo x.Location = new System.Drawing. Point(70, 19);
this.nameTextBo x.Name = "nameTextBo x";
this.nameTextBo x.Size = new System.Drawing. Size(100, 20);
this.nameTextBo x.TabIndex = 2;
//
// gameBindingSour ce
//
this.gameBindin gSource.DataSou rce = typeof(ComboBox Test.Game);
//
// kindComboBox
//
this.kindComboB ox.DataBindings .Add(new
System.Windows. Forms.Binding(" SelectedValue", this.gameBindin gSource,
"Kind", true));
this.kindComboB ox.DataSource = this.kindListBi ndingSource;
this.kindComboB ox.DisplayMembe r = "Descriptio n";
this.kindComboB ox.FormattingEn abled = true;
this.kindComboB ox.Location = new System.Drawing. Point(70, 45);
this.kindComboB ox.Name = "kindComboB ox";
this.kindComboB ox.Size = new System.Drawing. Size(121, 21);
this.kindComboB ox.TabIndex = 3;
this.kindComboB ox.ValueMember = "IdKind";
//
// kindListBinding Source
//
this.kindListBi ndingSource.Dat aMember = "KindList";
this.kindListBi ndingSource.Dat aSource = this.gameBindin gSource;
//
// speedTextBox
//
this.speedTextB ox.DataBindings .Add(new
System.Windows. Forms.Binding(" Text", this.gameBindin gSource, "Speed",
true));
this.speedTextB ox.DataBindings .Add(new
System.Windows. Forms.Binding(" Enabled", this.gameBindin gSource,
"IsSpeedEnabled ", true));
this.speedTextB ox.Location = new System.Drawing. Point(70, 72);
this.speedTextB ox.Name = "speedTextB ox";
this.speedTextB ox.Size = new System.Drawing. Size(100, 20);
this.speedTextB ox.TabIndex = 5;
//
// checkBox1
//
this.checkBox1. AutoSize = true;
this.checkBox1. Location = new System.Drawing. Point(70, 99);
this.checkBox1. Name = "checkBox1" ;
this.checkBox1. Size = new System.Drawing. Size(122, 17);
this.checkBox1. TabIndex = 6;
this.checkBox1. Text = "OnPropertyChan ged";
this.checkBox1. UseVisualStyleB ackColor = true;
this.checkBox1. CheckedChanged += new
System.EventHan dler(this.check Box1_CheckedCha nged);
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(213, 129);
this.Controls.A dd(this.checkBo x1);
this.Controls.A dd(speedLabel);
this.Controls.A dd(this.speedTe xtBox);
this.Controls.A dd(kindLabel);
this.Controls.A dd(this.kindCom boBox);
this.Controls.A dd(nameLabel);
this.Controls.A dd(this.nameTex tBox);
this.Name = "Form1";
this.Text = "Games";
((System.Compon entModel.ISuppo rtInitialize)(t his.gameBinding Source)).EndIni t();
((System.Compon entModel.ISuppo rtInitialize)(t his.kindListBin dingSource)).En dInit();
this.ResumeLayo ut(false);
this.PerformLay out();
}
#endregion
private System.Windows. Forms.BindingSo urce gameBindingSour ce;
private System.Windows. Forms.TextBox nameTextBox;
private System.Windows. Forms.ComboBox kindComboBox;
private System.Windows. Forms.TextBox speedTextBox;
private System.Windows. Forms.BindingSo urce kindListBinding Source;
public Form1()
{
InitializeCompo nent();
Game game = new Game();
game.Name = "Test game";
game.Kind = 1;
gameBindingSour ce.DataSource = game;
}
private CheckBox checkBox1;
private void checkBox1_Check edChanged(objec t sender, EventArgs e)
{
if (checkBox1.Chec ked)
this.kindComboB ox.DataBindings[0].DataSourceUpda teMode =
DataSourceUpdat eMode.OnPropert yChanged;
else
this.kindComboB ox.DataBindings[0].DataSourceUpda teMode =
DataSourceUpdat eMode.OnValidat ion;
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
Application.Run (new Form1());
}
}
public class Game
{
private string name;
private int kind;
private List<KindkindLi st = new List<Kind>();
private int speed;
private bool isSpeedEnabled;
public Game()
{
KindList.Add(ne w Kind(0, "Adventure" ));
KindList.Add(ne w Kind(1, "Racing"));
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Kind
{
get { return kind; }
set
{
kind = value;
isSpeedEnabled = (kind == 1);
}
}
public List<KindKindLi st
{
get { return kindList; }
set { kindList = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public bool IsSpeedEnabled
{
get
{
return isSpeedEnabled;
}
set { isSpeedEnabled = value; }
}
}
public class Kind
{
private int idKind;
private string description;
public Kind(int IdKind, string Description)
{
this.IdKind = IdKind;
this.Descriptio n = Description;
}
public int IdKind
{
get { return idKind; }
set { idKind = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
}
}
--
www.gsnel.nl