PC Review


Reply
Thread Tools Rate Thread

A complete program that doesn't work as I want

 
 
Tony Johansson
Guest
Posts: n/a
 
      11th Mar 2011
Hello!

This is a program that contains one DataGridView and two Buttons.
The program have the usual Form1 class and one Player class.
This Player class is bound to the DataGridView like this
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;

All this code is of cource included in this code listing because it's a
complete program.
The two buttons is called BtnCreateTextBox and BtnBindToTextBox.

So the following steps is that I follow.
1.Click on the BtnCreateTextBox. This will create a TextBox and put this
into the panel control. This works fine.
2.Enter a name into the DataGridView.
3.Click on the BtnBindToTextBox. This should display the entered name in the
created TextBox. This works fine
4 Go to 1 and create a new TextBox and enter a new name on a new row in the
DataGridView and click on the BtnBindToTextBox to bin the new name to the
new TextBox that you created

As you can see is that I want to bind each entered name in the DataGridView
to a TextBox that is created when I click on the BtnCreateTextBox.

These steps above 1 to 3 works fine for the first time but if I create a
second TextBox and enter a second name in the DataGridView and click on the
BtnBindToTextBox it doesn't work as I want.


//Complete program listing
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Testing
{
public partial class Form1 : Form
{
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
private int xpos = 0;
private int ypos = 0;
private TextBox textBox;

public Form1()
{
InitializeComponent();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
}

private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
textBox = new TextBox();
textBox.Location = new Point(xpos += 20, ypos += 20);
panel1.Controls.Add(textBox);
}

private void BtnBindToTextBox_Click(object sender, EventArgs e)
{
textBox.DataBindings.Add("text", bindingSource, "Name");
}
}

public class Player
{
private int playerID;
private string name;

public Player(int ID, string name)
{
this.playerID = ID;
this.name = name;
}

public Player()
{}

public string Name
{
get { return name; }
set { name = value; }
}

public int PlayerID
{
get { return playerID; }
set { playerID = value; }
}
}

//A copy paste from Form1.Designer.cs
partial class Form1
{
/// <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.dataGridView1 = new System.Windows.Forms.DataGridView();
this.BtnCreateTextBox = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.BtnBindToTextBox = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 137);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(267, 121);
this.dataGridView1.TabIndex = 0;
//
// BtnCreateTextBox
//
this.BtnCreateTextBox.Location = new System.Drawing.Point(285,
167);
this.BtnCreateTextBox.Name = "BtnCreateTextBox";
this.BtnCreateTextBox.Size = new System.Drawing.Size(153, 23);
this.BtnCreateTextBox.TabIndex = 1;
this.BtnCreateTextBox.Text = "CreateTextBox";
this.BtnCreateTextBox.UseVisualStyleBackColor = true;
this.BtnCreateTextBox.Click += new
System.EventHandler(this.BtnCreateTextBox_Click);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(405, 108);
this.panel1.TabIndex = 2;
//
// BtnBindToTextBox
//
this.BtnBindToTextBox.Location = new System.Drawing.Point(285,
226);
this.BtnBindToTextBox.Name = "BtnBindToTextBox";
this.BtnBindToTextBox.Size = new System.Drawing.Size(153, 23);
this.BtnBindToTextBox.TabIndex = 3;
this.BtnBindToTextBox.Text = "BindDGVNameToTextBox";
this.BtnBindToTextBox.UseVisualStyleBackColor = true;
this.BtnBindToTextBox.Click += new
System.EventHandler(this.BtnBindToTextBox_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(444, 270);
this.Controls.Add(this.BtnBindToTextBox);
this.Controls.Add(this.panel1);
this.Controls.Add(this.BtnCreateTextBox);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button BtnCreateTextBox;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button BtnBindToTextBox;
}

//A copy paste from program.cs
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());
}
}
}



 
Reply With Quote
 
 
 
 
kndg
Guest
Posts: n/a
 
      11th Mar 2011
On 3/11/2011 5:45 PM, Tony Johansson wrote:
> Hello!
>
> This is a program that contains one DataGridView and two Buttons.
> The program have the usual Form1 class and one Player class.
> This Player class is bound to the DataGridView like this
> private BindingSource bindingSource = new BindingSource();
> private List<Player> players = new List<Player>();
> bindingSource.DataSource = players;
> dataGridView1.DataSource = bindingSource;
>
> All this code is of cource included in this code listing because it's a
> complete program.
> The two buttons is called BtnCreateTextBox and BtnBindToTextBox.
>
> So the following steps is that I follow.
> 1.Click on the BtnCreateTextBox. This will create a TextBox and put this
> into the panel control. This works fine.
> 2.Enter a name into the DataGridView.
> 3.Click on the BtnBindToTextBox. This should display the entered name in the
> created TextBox. This works fine
> 4 Go to 1 and create a new TextBox and enter a new name on a new row in the
> DataGridView and click on the BtnBindToTextBox to bin the new name to the
> new TextBox that you created
>
> As you can see is that I want to bind each entered name in the DataGridView
> to a TextBox that is created when I click on the BtnCreateTextBox.
>
> These steps above 1 to 3 works fine for the first time but if I create a
> second TextBox and enter a second name in the DataGridView and click on the
> BtnBindToTextBox it doesn't work as I want.
>


IMO, I find what you are trying to do is little wierd... but anyway, the
problem with your current code is because you bind the textbox to the
same source - 'bindingSource' - which bind to the list of player. In
order to do what you want, you have to keep track the textbox instance
and bind to player object instead.

For example, declare an id and a list to keep track the textbox instance.

public partial class Form1 : Form
{
...
int selectedTextBoxId = -1;
List<TextBox> textBoxes = new List<TextBox>();


Each time you create a new textbox, add it to the list,

private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
...

textBoxes.Add(textBox);
selectedTextBoxId++;
}


Modify your binding to bind to the player object instead,

private void BtnBindToTextBox_Click(object sender, EventArgs e)
{
textBoxes[selectedTextBoxId].DataBindings.Add("Text",
players[selectedTextBoxId], "Name");
}

By the way, the above code just a demostration purpose. I still find it
wierd to do things like that...
 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      11th Mar 2011
Hello!

I have changed a bit from the previous version

This is a program that contains one DataGridView and one Buttons.
The program have the usual Form1 class and one Player class.
This Player class is bound to the DataGridView like this
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;

All this code is of cource included in this code listing because it's a
complete program.
The buttons is called BtnCreateTextBox

So the following steps is that I follow.
1.Click on the BtnCreateTextBox. This will create a TextBox and put this
into the panel control. This works fine.
2.Enter a name into the DataGridView.
3.When you move the cursor away from this cell the name is displayed in the
TextBox. This works fine
4 Go to 1 and create a new TextBox and enter a new name on a new row in the
DataGridView and move the cursor away from this cell where the name was
entered. This works fine.

As you can see I want to bind each entered name in the DataGridView
to a TextBox that is created when I click on the BtnCreateTextBox.

The steps above 1 to 3 works fine but I get an exception when I want to
change an existing name in the DataGridView.
I get an ArgumentException that say "This causes two bindings in the
collection to bind to the same property"

I'm very unsure how to solve this problem. I have tried with
textBox.DataBindings.Clear(); but that does't solve my problem
In my real program the TextBox control is a userControl where one control in
this user control is a TextBox so
the principle is the same if I managed to make it work in this test program
it will also work in my real applikation.


//Complete program listing
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Testing
{
public partial class Form1 : Form
{
private int xpos = 0;
private int ypos = 0;
private BindingSource bindingSource = new BindingSource();
private List<Player> players = new List<Player>();
private TextBox textBox;

public Form1()
{
InitializeComponent();
bindingSource.DataSource = players;
dataGridView1.DataSource = bindingSource;
}

private void BtnCreateTextBox_Click(object sender, EventArgs e)
{
textBox = new TextBox();
textBox.Location = new Point(xpos += 20, ypos += 20);
panel1.Controls.Add(textBox);
}

private void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (textBox == null)
return;

Player thisPlayer = players[e.RowIndex];
textBox.DataBindings.Add("Text", thisPlayer, "Name");
}
}

public class Player
{
private int playerID;
private string name;

public Player(int ID, string name)
{
this.playerID = ID;
this.name = name;
}

public Player()
{}

public string Name
{
get { return name; }
set { name = value; }
}

public int PlayerID
{
get { return playerID; }
set { playerID = value; }
}
}

//A copy paste from Form1.Designer.cs
partial class Form1
{
/// <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.dataGridView1 = new System.Windows.Forms.DataGridView();
this.BtnCreateTextBox = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 137);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(267, 121);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellValueChanged += new
System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
//
// BtnCreateTextBox
//
this.BtnCreateTextBox.Location = new System.Drawing.Point(285,
167);
this.BtnCreateTextBox.Name = "BtnCreateTextBox";
this.BtnCreateTextBox.Size = new System.Drawing.Size(153, 23);
this.BtnCreateTextBox.TabIndex = 1;
this.BtnCreateTextBox.Text = "CreateTextBox";
this.BtnCreateTextBox.UseVisualStyleBackColor = true;
this.BtnCreateTextBox.Click += new
System.EventHandler(this.BtnCreateTextBox_Click);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(405, 108);
this.panel1.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(444, 270);
this.Controls.Add(this.panel1);
this.Controls.Add(this.BtnCreateTextBox);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button BtnCreateTextBox;
private System.Windows.Forms.Panel panel1;
}


//A copy paste from program.cs
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());
}
}
}

//Tony


 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      11th Mar 2011
"Tony Johansson" <(E-Mail Removed)> skrev i meddelandet
news:ild9ed$b5s$(E-Mail Removed)...
> Hello!
>
> I have changed a bit from the previous version
>
> This is a program that contains one DataGridView and one Buttons.
> The program have the usual Form1 class and one Player class.
> This Player class is bound to the DataGridView like this
> private BindingSource bindingSource = new BindingSource();
> private List<Player> players = new List<Player>();
> bindingSource.DataSource = players;
> dataGridView1.DataSource = bindingSource;
>
> All this code is of cource included in this code listing because it's a
> complete program.
> The buttons is called BtnCreateTextBox
>
> So the following steps is that I follow.
> 1.Click on the BtnCreateTextBox. This will create a TextBox and put this
> into the panel control. This works fine.
> 2.Enter a name into the DataGridView.
> 3.When you move the cursor away from this cell the name is displayed in
> the TextBox. This works fine
> 4 Go to 1 and create a new TextBox and enter a new name on a new row in
> the
> DataGridView and move the cursor away from this cell where the name was
> entered. This works fine.
>
> As you can see I want to bind each entered name in the DataGridView
> to a TextBox that is created when I click on the BtnCreateTextBox.
>
> The steps above 1 to 3 works fine but I get an exception when I want to
> change an existing name in the DataGridView.
> I get an ArgumentException that say "This causes two bindings in the
> collection to bind to the same property"
>
> I'm very unsure how to solve this problem. I have tried with
> textBox.DataBindings.Clear(); but that does't solve my problem
> In my real program the TextBox control is a userControl where one control
> in this user control is a TextBox so
> the principle is the same if I managed to make it work in this test
> program it will also work in my real applikation.
>
>
> //Complete program listing
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Linq;
> using System.Text;
> using System.Windows.Forms;
>
> namespace Testing
> {
> public partial class Form1 : Form
> {
> private int xpos = 0;
> private int ypos = 0;
> private BindingSource bindingSource = new BindingSource();
> private List<Player> players = new List<Player>();
> private TextBox textBox;
>
> public Form1()
> {
> InitializeComponent();
> bindingSource.DataSource = players;
> dataGridView1.DataSource = bindingSource;
> }
>
> private void BtnCreateTextBox_Click(object sender, EventArgs e)
> {
> textBox = new TextBox();
> textBox.Location = new Point(xpos += 20, ypos += 20);
> panel1.Controls.Add(textBox);
> }
>
> private void dataGridView1_CellValueChanged(object sender,
> DataGridViewCellEventArgs e)
> {
> if (textBox == null)
> return;
>
> Player thisPlayer = players[e.RowIndex];
> textBox.DataBindings.Add("Text", thisPlayer, "Name");
> }
> }
>
> public class Player
> {
> private int playerID;
> private string name;
>
> public Player(int ID, string name)
> {
> this.playerID = ID;
> this.name = name;
> }
>
> public Player()
> {}
>
> public string Name
> {
> get { return name; }
> set { name = value; }
> }
>
> public int PlayerID
> {
> get { return playerID; }
> set { playerID = value; }
> }
> }
>
> //A copy paste from Form1.Designer.cs
> partial class Form1
> {
> /// <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.dataGridView1 = new System.Windows.Forms.DataGridView();
> this.BtnCreateTextBox = new System.Windows.Forms.Button();
> this.panel1 = new System.Windows.Forms.Panel();
>
> ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
> this.SuspendLayout();
> //
> // dataGridView1
> //
> this.dataGridView1.ColumnHeadersHeightSizeMode =
> System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
> this.dataGridView1.Location = new System.Drawing.Point(12, 137);
> this.dataGridView1.Name = "dataGridView1";
> this.dataGridView1.Size = new System.Drawing.Size(267, 121);
> this.dataGridView1.TabIndex = 0;
> this.dataGridView1.CellValueChanged += new
> System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
> //
> // BtnCreateTextBox
> //
> this.BtnCreateTextBox.Location = new System.Drawing.Point(285,
> 167);
> this.BtnCreateTextBox.Name = "BtnCreateTextBox";
> this.BtnCreateTextBox.Size = new System.Drawing.Size(153, 23);
> this.BtnCreateTextBox.TabIndex = 1;
> this.BtnCreateTextBox.Text = "CreateTextBox";
> this.BtnCreateTextBox.UseVisualStyleBackColor = true;
> this.BtnCreateTextBox.Click += new
> System.EventHandler(this.BtnCreateTextBox_Click);
> //
> // panel1
> //
> this.panel1.Location = new System.Drawing.Point(12, 12);
> this.panel1.Name = "panel1";
> this.panel1.Size = new System.Drawing.Size(405, 108);
> this.panel1.TabIndex = 2;
> //
> // Form1
> //
> this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
> this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
> this.ClientSize = new System.Drawing.Size(444, 270);
> this.Controls.Add(this.panel1);
> this.Controls.Add(this.BtnCreateTextBox);
> this.Controls.Add(this.dataGridView1);
> this.Name = "Form1";
> this.Text = "Form1";
>
> ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
> this.ResumeLayout(false);
>
> }
>
> #endregion
>
> private System.Windows.Forms.DataGridView dataGridView1;
> private System.Windows.Forms.Button BtnCreateTextBox;
> private System.Windows.Forms.Panel panel1;
> }
>
>
> //A copy paste from program.cs
> 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());
> }
> }
> }
>
> //Tony
>


I found the solution myself .

//Tony


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A complete program that doesn't work as I want to Tony Johansson Microsoft C# .NET 1 10th Apr 2010 05:21 PM
A very small complete program that doesn't work as expected Tony Johansson Microsoft C# .NET 5 5th Jul 2009 12:00 AM
Auto Complete Doesn't Work =?Utf-8?B?U25vYmJpc2hKYWRl?= Microsoft Outlook Discussion 6 16th Nov 2005 03:09 PM
auto complete doesn't work--- Windows XP Performance 2 6th Sep 2004 09:37 PM
Auto Complete Doesn't work =?Utf-8?B?RGF2aWQ=?= Windows XP Internet Explorer 2 9th Nov 2003 11:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:08 PM.