CheckedListBox or DataBinding Bug?

J

Jeff

Try this... Run the form. Check a few items in the CheckedListBox. Go to Tab
2 and then back to Tab 1. Uh... what happened? The check marks are
cleared?!!

Any ideas???

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestCheck
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.label1 = new System.Windows.Forms.Label();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(16, 16);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(680, 432);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.checkedListBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 25);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(672, 403);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
//
// tabPage2
//
this.tabPage2.Controls.Add(this.label1);
this.tabPage2.Location = new System.Drawing.Point(4, 25);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(672, 403);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
//
// checkedListBox1
//
this.checkedListBox1.Location = new System.Drawing.Point(24, 24);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(624, 344);
this.checkedListBox1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(248, 168);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Nothing Here";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(712, 464);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt = new DataTable("BindingTable");
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");

dt.Rows.Add(new object[] {"Hello", "World"});
dt.Rows.Add(new object[] {"Goodbye", "Cruel World"});
dt.Rows.Add(new object[] {"Greetings", "Earthling"});

this.checkedListBox1.DataSource = dt;
this.checkedListBox1.DisplayMember = "Column2";
this.checkedListBox1.ValueMember = "Column1";


}
}
}
 
J

Jeff

Thanks Herfried...

There were some solutions there. I took the checkedListBox.BindingContext =
new BindingContext();
approach, but I don't know why that works...

Anyhow Thanks for the help. I really appreciate it!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top