about user control

T

Tony Johansson

Hello!

I have a user control that is put on the form when I click this event
handler.

private void BtnNewPlayer_Click(object sender, EventArgs e)
{
pg = new PlayerGUI();

int xPos = (guiCounter % 2) * pg.Width + 1;
int yPos = (guiCounter / 2) * pg.Height + 1;
pg.Location = new Point(xPos,yPos);

guiCounter++;
gameManager.PlayerGUIs.Add(pg);
pg.DrawHandle = false;
pg.StandHandle = false;
panel1.Controls.Add(pg);
dgvAllPlayers.DataSource = bindingSource;
}

This user control PlayerGUI contains several controls but one of these is a
TextBox called Name with a set and a get.
But what is the best solution to enter data into this created usercontrol
without creating a button to click on and then use
the instance of the user control to access the Name property. I hope it
exist some other way then to use a button that I mentioned in the previous
row.
I mean just to write somthing in the TextBox of the created user control
isn't of much use.

//Tony


Now to my problem I can't figure what the best solution is to enter data
into the created user control TextBox
 
T

Tony Johansson

Hello!

Here I bind the dataGridView to the user control.
The DataGridView is called dgvAllPlayers and the created user control is
called PlayerGUI
It works fine from the DataGridView to the user control.It update the user
control immediately
But if I write something in the user control namn it doesn't update the
dataGridView but if I just click on the row
in the DataGridView that is bound to the user control it update the
dataGridView to the same value that I entered in the user control.
So is it possible to cause a change in the User control name to update the
DatagridView immediately or is
the way that it works that I mentioned ?

private void dgvAllPlayers_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (pg != null)
{
Player thisPlayer = gameManager.Players[e.RowIndex];

foreach (PlayerGUI pGUI in gameManager.PlayerGUIs)
{
if (pGUI.Namn == thisPlayer.Name)
{
pGUI.DataBindings.Remove(pGUI.DataBindings["Namn"]);
pg = pGUI;

if (string.Equals(thisPlayer.Name, Croupier,
StringComparison.CurrentCultureIgnoreCase))
BtnStartGame.Enabled = true;

break;
}
}

if (gameManager.Players.Count <= gameManager.PlayerGUIs.Count)
pg.DataBindings.Add("Namn", thisPlayer, "Name");
}

//Tony
 
T

Tony Johansson

Peter Duniho said:
I'm not sure I really understand the question. But it seems to me that the
DataGridView cell corresponding to the bound TextBox field on the
UserControl should get updated as soon as the TextBox loses focus.

Of course, data binding relies on having the necessary ".Changed" events
or supporting the INotifyPropertyChanged interface, or similar mechanisms.
Make sure you have all that configured correctly.

Pete

The textbox of the user control get updated which is fine as soon as the
cell in the DataGridView loose focus
but the other way around when the TextBox in the user control change an
existing namn the cell in the DataGridView doesn't
get updated but if I just click on the cell in the DataGridView the cell is
updated with the value from the user control textbox.

So my question is if it's possible to get an update of the dataGridView cell
when I change an existing name in the user control.
Now as I mentioned previously I must click the cell in the DataGridView to
establish an update in the DataGridView.

//Tony
 
T

Tony Johansson

Peter Duniho said:
Like I said, make sure your data binding is set up correctly.

You didn't bother to post enough code to show how it's being configured,
so there's nothing anyone else can do to ensure it's correct. You need to
do that yourself.

Pete


Here is a simple binding example where I have bound a TextBox1 to another
TextBox2 on the Text property.
When this program is run and you write something in TextBox2 this text is
automatically written in TextBox1.

Now if I write something in TextBox1 instead this text is NOT written in
TextBox2 but if I just click in the TextBox2 the text is updated in this
TextBox2 control. Is this the normal behaviour ?
I thought that when you write something in TextBox1 this should update
TextBox2 in the same way as it does when you write something in TextBox2
this text is updated in TextBox1 so why does it work perfect in one
direction but not in the other direction ?

Is it possible to make it work in both directions ?


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 Slask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add("text", textBox2, "text");
}
}

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.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(180, 58);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(27, 58);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 4;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 270);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.textBox2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}

#endregion

private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
}

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());
}
}
}
 

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