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 DataSourceUpdateMode to OnPropertyChanged, 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
windowsapplication, in a form and delete some generated code, but it is not
a useful program but it shows my problem). When the checkbox
OnPropertyChanged is unchecked, you have to click in an other control to see
the changes, but the right displaymember is showed. With OnPropertyChanged
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.Collections.Generic;
using System.ComponentModel;
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.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.components = new System.ComponentModel.Container();
System.Windows.Forms.Label nameLabel;
System.Windows.Forms.Label kindLabel;
System.Windows.Forms.Label speedLabel;
this.nameTextBox = new System.Windows.Forms.TextBox();
this.gameBindingSource = new
System.Windows.Forms.BindingSource(this.components );
this.kindComboBox = new System.Windows.Forms.ComboBox();
this.kindListBindingSource = new
System.Windows.Forms.BindingSource(this.components );
this.speedTextBox = 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.ComponentModel.ISupportInitialize)(this.g ameBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.k indListBindingSource)).BeginInit();
this.SuspendLayout();
//
// nameLabel
//
nameLabel.AutoSize = true;
nameLabel.Location = new System.Drawing.Point(26, 22);
nameLabel.Name = "nameLabel";
nameLabel.Size = new System.Drawing.Size(38, 13);
nameLabel.TabIndex = 1;
nameLabel.Text = "Name:";
//
// kindLabel
//
kindLabel.AutoSize = true;
kindLabel.Location = new System.Drawing.Point(33, 48);
kindLabel.Name = "kindLabel";
kindLabel.Size = new System.Drawing.Size(31, 13);
kindLabel.TabIndex = 2;
kindLabel.Text = "Kind:";
//
// speedLabel
//
speedLabel.AutoSize = true;
speedLabel.Location = new System.Drawing.Point(23, 75);
speedLabel.Name = "speedLabel";
speedLabel.Size = new System.Drawing.Size(41, 13);
speedLabel.TabIndex = 4;
speedLabel.Text = "Speed:";
//
// nameTextBox
//
this.nameTextBox.DataBindings.Add(new
System.Windows.Forms.Binding("Text", this.gameBindingSource, "Name", true));
this.nameTextBox.Location = new System.Drawing.Point(70, 19);
this.nameTextBox.Name = "nameTextBox";
this.nameTextBox.Size = new System.Drawing.Size(100, 20);
this.nameTextBox.TabIndex = 2;
//
// gameBindingSource
//
this.gameBindingSource.DataSource = typeof(ComboBoxTest.Game);
//
// kindComboBox
//
this.kindComboBox.DataBindings.Add(new
System.Windows.Forms.Binding("SelectedValue", this.gameBindingSource,
"Kind", true));
this.kindComboBox.DataSource = this.kindListBindingSource;
this.kindComboBox.DisplayMember = "Description";
this.kindComboBox.FormattingEnabled = true;
this.kindComboBox.Location = new System.Drawing.Point(70, 45);
this.kindComboBox.Name = "kindComboBox";
this.kindComboBox.Size = new System.Drawing.Size(121, 21);
this.kindComboBox.TabIndex = 3;
this.kindComboBox.ValueMember = "IdKind";
//
// kindListBindingSource
//
this.kindListBindingSource.DataMember = "KindList";
this.kindListBindingSource.DataSource = this.gameBindingSource;
//
// speedTextBox
//
this.speedTextBox.DataBindings.Add(new
System.Windows.Forms.Binding("Text", this.gameBindingSource, "Speed",
true));
this.speedTextBox.DataBindings.Add(new
System.Windows.Forms.Binding("Enabled", this.gameBindingSource,
"IsSpeedEnabled", true));
this.speedTextBox.Location = new System.Drawing.Point(70, 72);
this.speedTextBox.Name = "speedTextBox";
this.speedTextBox.Size = new System.Drawing.Size(100, 20);
this.speedTextBox.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 = "OnPropertyChanged";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new
System.EventHandler(this.checkBox1_CheckedChanged) ;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(213, 129);
this.Controls.Add(this.checkBox1);
this.Controls.Add(speedLabel);
this.Controls.Add(this.speedTextBox);
this.Controls.Add(kindLabel);
this.Controls.Add(this.kindComboBox);
this.Controls.Add(nameLabel);
this.Controls.Add(this.nameTextBox);
this.Name = "Form1";
this.Text = "Games";
((System.ComponentModel.ISupportInitialize)(this.g ameBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.k indListBindingSource)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.BindingSource gameBindingSource;
private System.Windows.Forms.TextBox nameTextBox;
private System.Windows.Forms.ComboBox kindComboBox;
private System.Windows.Forms.TextBox speedTextBox;
private System.Windows.Forms.BindingSource kindListBindingSource;
public Form1()
{
InitializeComponent();
Game game = new Game();
game.Name = "Test game";
game.Kind = 1;
gameBindingSource.DataSource = game;
}
private CheckBox checkBox1;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
this.kindComboBox.DataBindings[0].DataSourceUpdateMode =
DataSourceUpdateMode.OnPropertyChanged;
else
this.kindComboBox.DataBindings[0].DataSourceUpdateMode =
DataSourceUpdateMode.OnValidation;
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
public class Game
{
private string name;
private int kind;
private List<KindkindList = new List<Kind>();
private int speed;
private bool isSpeedEnabled;
public Game()
{
KindList.Add(new Kind(0, "Adventure"));
KindList.Add(new 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<KindKindList
{
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.Description = Description;
}
public int IdKind
{
get { return idKind; }
set { idKind = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
}
}
--
www.gsnel.nl