Welcome Guest Search | Active Topics | Members | Log In | Register

ComboBox OnPropertyChanged problem Options · View
Gerrit
Posted: Thursday, October 04, 2007 9:51:40 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 04/10/2007 21:51:40
Date: Thu, 4 Oct 2007 19:51:40 +0200

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.gameBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.kindListBindingSource)).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.gameBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.kindListBindingSource)).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(false);
Application.Run(new Form1());
}
}

public class Game
{
private string name;
private int kind;
private List<Kind> kindList = 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<Kind> KindList
{
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


Cor Ligthert[MVP]
Posted: Friday, October 05, 2007 7:54:21 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 07:54:21
Date: Fri, 5 Oct 2007 05:54:21 +0200

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

GS
Posted: Friday, October 05, 2007 10:43:12 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 10:43:12
Date: Fri, 5 Oct 2007 08:43:12 +0200


"Cor Ligthert[MVP]" <notmyfirstname@planet.nl> schreef in bericht
news:4D260ECF-C9E4-406E-9621-A8C73160A651@microsoft.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<Kind> KindList
{
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.




GS
Posted: Friday, October 05, 2007 1:22:51 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 13:22:51
Date: Fri, 5 Oct 2007 11:22:51 +0200


> I fill the combobox items with a list property:
>
> public List<Kind> KindList
> {
> 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?


Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.083 seconds.