By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

ComboBox OnPropertyChanged problem

P: n/a
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
Oct 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Gerrit,

If you want answers from these newsgroups, than create a piece of sample
code and show that to us, not a complete program. Mostly if you do that you
see often yourself the problem.

The change that you get an answer now, is probably only from somebody who is
learning as well C# and tries your program.

As well tell what you want to achieve, not a piece of code that does not
work in the way you think that it should work.

Cor

Oct 5 '07 #2

P: n/a
GS

"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:4D**********************************@microsof t.com...
Gerrit,

If you want answers from these newsgroups, than create a piece of sample
code and show that to us, not a complete program. Mostly if you do that
you see often yourself the problem.

The change that you get an answer now, is probably only from somebody who
is learning as well C# and tries your program.

As well tell what you want to achieve, not a piece of code that does not
work in the way you think that it should work.

Cor
Ok, but I described my problem / what I want in the first rows of my
question. But my english is not so good, so I post the code with it.
I try again: I want that the selectionchange of the combobox do directly
something when the selection changed. And not by leaving the combobox with
tab or mouseclick. So I try to set the DataSourceUpdateMode to
OpPropertyChanged. I have a databound combobox, bounded and in the bounded
object, I see the change, but the selected item of the combobox does not
change.

I fill the combobox items with a list property:

public List<KindKindList
{
get { return kindList; }
set { kindList = value; }
}

And the value is set in al property:

public int Kind
{
get { return kind; }
set
{
kind = value;
isSpeedEnabled = (kind == 1);
}
}

And when Kind is 1, then I set an other property to true

public bool IsSpeedEnabled
{
get
{
return isSpeedEnabled;
}
set { isSpeedEnabled = value; }
}

So with this property, I can enable fields, that only must be enabled when
Kind == 1.


Oct 5 '07 #3

P: n/a
GS
I fill the combobox items with a list property:

public List<KindKindList
{
get { return kindList; }
set { kindList = value; }
}

And the value is set in al property:

public int Kind
{
get { return kind; }
set
{
kind = value;
isSpeedEnabled = (kind == 1);
}
}

And when Kind is 1, then I set an other property to true

public bool IsSpeedEnabled
{
get
{
return isSpeedEnabled;
}
set { isSpeedEnabled = value; }
}

So with this property, I can enable fields, that only must be enabled when
Kind == 1.
Some more information:

I bound the ComboBox with a BindingSource, then I have the problem. But when
I bound it to my object, then I have not the problem.

Is the right way, binding to the object or is BindingSource better and how
can I do that?
Oct 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.