473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ComboBox OnPropertyChang ed 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 DataSourceUpdat eMode to OnPropertyChang ed, 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
windowsapplicat ion, in a form and delete some generated code, but it is not
a useful program but it shows my problem). When the checkbox
OnPropertyChang ed is unchecked, you have to click in an other control to see
the changes, but the right displaymember is showed. With OnPropertyChang ed
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.Collecti ons.Generic;
using System.Componen tModel;
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.Componen tModel.IContain er 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.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.components = new System.Componen tModel.Containe r();
System.Windows. Forms.Label nameLabel;
System.Windows. Forms.Label kindLabel;
System.Windows. Forms.Label speedLabel;
this.nameTextBo x = new System.Windows. Forms.TextBox() ;
this.gameBindin gSource = new
System.Windows. Forms.BindingSo urce(this.compo nents);
this.kindComboB ox = new System.Windows. Forms.ComboBox( );
this.kindListBi ndingSource = new
System.Windows. Forms.BindingSo urce(this.compo nents);
this.speedTextB ox = 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.Compon entModel.ISuppo rtInitialize)(t his.gameBinding Source)).BeginI nit();
((System.Compon entModel.ISuppo rtInitialize)(t his.kindListBin dingSource)).Be ginInit();
this.SuspendLay out();
//
// nameLabel
//
nameLabel.AutoS ize = true;
nameLabel.Locat ion = new System.Drawing. Point(26, 22);
nameLabel.Name = "nameLabel" ;
nameLabel.Size = new System.Drawing. Size(38, 13);
nameLabel.TabIn dex = 1;
nameLabel.Text = "Name:";
//
// kindLabel
//
kindLabel.AutoS ize = true;
kindLabel.Locat ion = new System.Drawing. Point(33, 48);
kindLabel.Name = "kindLabel" ;
kindLabel.Size = new System.Drawing. Size(31, 13);
kindLabel.TabIn dex = 2;
kindLabel.Text = "Kind:";
//
// speedLabel
//
speedLabel.Auto Size = true;
speedLabel.Loca tion = new System.Drawing. Point(23, 75);
speedLabel.Name = "speedLabel ";
speedLabel.Size = new System.Drawing. Size(41, 13);
speedLabel.TabI ndex = 4;
speedLabel.Text = "Speed:";
//
// nameTextBox
//
this.nameTextBo x.DataBindings. Add(new
System.Windows. Forms.Binding(" Text", this.gameBindin gSource, "Name", true));
this.nameTextBo x.Location = new System.Drawing. Point(70, 19);
this.nameTextBo x.Name = "nameTextBo x";
this.nameTextBo x.Size = new System.Drawing. Size(100, 20);
this.nameTextBo x.TabIndex = 2;
//
// gameBindingSour ce
//
this.gameBindin gSource.DataSou rce = typeof(ComboBox Test.Game);
//
// kindComboBox
//
this.kindComboB ox.DataBindings .Add(new
System.Windows. Forms.Binding(" SelectedValue", this.gameBindin gSource,
"Kind", true));
this.kindComboB ox.DataSource = this.kindListBi ndingSource;
this.kindComboB ox.DisplayMembe r = "Descriptio n";
this.kindComboB ox.FormattingEn abled = true;
this.kindComboB ox.Location = new System.Drawing. Point(70, 45);
this.kindComboB ox.Name = "kindComboB ox";
this.kindComboB ox.Size = new System.Drawing. Size(121, 21);
this.kindComboB ox.TabIndex = 3;
this.kindComboB ox.ValueMember = "IdKind";
//
// kindListBinding Source
//
this.kindListBi ndingSource.Dat aMember = "KindList";
this.kindListBi ndingSource.Dat aSource = this.gameBindin gSource;
//
// speedTextBox
//
this.speedTextB ox.DataBindings .Add(new
System.Windows. Forms.Binding(" Text", this.gameBindin gSource, "Speed",
true));
this.speedTextB ox.DataBindings .Add(new
System.Windows. Forms.Binding(" Enabled", this.gameBindin gSource,
"IsSpeedEnabled ", true));
this.speedTextB ox.Location = new System.Drawing. Point(70, 72);
this.speedTextB ox.Name = "speedTextB ox";
this.speedTextB ox.Size = new System.Drawing. Size(100, 20);
this.speedTextB ox.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 = "OnPropertyChan ged";
this.checkBox1. UseVisualStyleB ackColor = true;
this.checkBox1. CheckedChanged += new
System.EventHan dler(this.check Box1_CheckedCha nged);
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(213, 129);
this.Controls.A dd(this.checkBo x1);
this.Controls.A dd(speedLabel);
this.Controls.A dd(this.speedTe xtBox);
this.Controls.A dd(kindLabel);
this.Controls.A dd(this.kindCom boBox);
this.Controls.A dd(nameLabel);
this.Controls.A dd(this.nameTex tBox);
this.Name = "Form1";
this.Text = "Games";
((System.Compon entModel.ISuppo rtInitialize)(t his.gameBinding Source)).EndIni t();
((System.Compon entModel.ISuppo rtInitialize)(t his.kindListBin dingSource)).En dInit();
this.ResumeLayo ut(false);
this.PerformLay out();

}

#endregion

private System.Windows. Forms.BindingSo urce gameBindingSour ce;
private System.Windows. Forms.TextBox nameTextBox;
private System.Windows. Forms.ComboBox kindComboBox;
private System.Windows. Forms.TextBox speedTextBox;
private System.Windows. Forms.BindingSo urce kindListBinding Source;

public Form1()
{
InitializeCompo nent();

Game game = new Game();
game.Name = "Test game";
game.Kind = 1;
gameBindingSour ce.DataSource = game;
}

private CheckBox checkBox1;

private void checkBox1_Check edChanged(objec t sender, EventArgs e)
{
if (checkBox1.Chec ked)
this.kindComboB ox.DataBindings[0].DataSourceUpda teMode =
DataSourceUpdat eMode.OnPropert yChanged;
else
this.kindComboB ox.DataBindings[0].DataSourceUpda teMode =
DataSourceUpdat eMode.OnValidat ion;
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
Application.Run (new Form1());
}
}

public class Game
{
private string name;
private int kind;
private List<KindkindLi st = new List<Kind>();
private int speed;
private bool isSpeedEnabled;

public Game()
{
KindList.Add(ne w Kind(0, "Adventure" ));
KindList.Add(ne w 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<KindKindLi st
{
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.Descriptio n = 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 5321
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.nlschre ef in bericht
news:4D******** *************** ***********@mic rosoft.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 DataSourceUpdat eMode to
OpPropertyChang ed. 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<KindKindLi st
{
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<KindKindLi st
{
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
2491
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 When the ComboBox DroppedDown is Shown I have to actually click twice to get out of the form or need to click twice to do anything.
7
8528
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 of doing it. I have a table with products, tblProducts, some of them are Active while others are Inactive. The form shows all the products purchased by a customer, both Active and Inactive in a ComboBox, cbProducts. My client wants to view all...
8
12105
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 combobox. What is the solution? Thank you in advance.
1
2497
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 DataTable("table"); testTable.Columns.Add("Col1", typeof(Int32)); testTable.Columns.Add("Col2", typeof(String)); testTable.Rows.Add(testTable.NewRow());
3
6834
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: http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx I have a problem when there are two DataGrid's on one form, and when I switch focus from one grid to the other. To be more precise, when I'm editing a combo box column in one grid, and then click in the combo column of...
0
2380
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 changed letter) because he reset the selectedindex of the combobox to -1. In debug mode I can't see where he does this. How can I solve this? My code sofar: contactlijst is a structure and the volgnummer is the index of the item in the combobox
4
4018
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 - but contain a value. However the value is just for user reference - not really one of the combobox choices. For example intially the combobox should equal the word Select - and the item values can be say 'one', 'two', and 'three'.
30
4604
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 corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
4
4620
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 bound to a table as a lookup, drawing values from another table to populate the available selections. This all worked fine in VB6. I have distilled the problem down to a simple form drawing data from the Northwind database for a representative...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.