473,382 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 5277
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.