472,958 Members | 2,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

ComboBox OnPropertyChanged problem

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
3 5244
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chandra | last post by:
Hello Sir, This is Chandra and Working as Software Engineer. I got a really weird problem with ComboBoxes. I have set the ComboBox DroppedDown Property to true. Now the actual problem is...
7
by: Nicolae Fieraru | last post by:
Hi All, I am trying to change the rowsource of a combobox when I click on it. I played with many events, associated with the form and the combobox, but still haven't figured out what is the way...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
1
by: anonymous | last post by:
I've been trying to put a them, please help me out. Here's the major parts of my code: public Form1() { DataSet myDataSet = new DataSet("myDataSet"); DataTable testTable = new...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
0
by: tupolev | last post by:
Hi, I already post this item but now I know whats wrong: I want to allow the user to edit the items in a combobox. The problem is that the textchange event works just one time (for the first...
4
by: Keith | last post by:
Hello - this started out as a minor annoyance - and now is starting to bother me more and more - I'm hoping someone can help me. I would like to have a combobox display - NOT initially be blank...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.