How do I bind a textbox to a simple string varaible with databinding?
I managed to do the binding but unfortnatedly the textvox does not get
updated when I change the string wich the textbox is bound to. I though
this automation is on of the purposes of the databinding ... ? What am
I doing wrong ?
See this little example code:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace DataBindingText2
{
public class Form1 : Form
{
private TextBox textBox1;
private Container components = null;
private Button button1;
private string mystr = "hallo";
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add(new Binding("Text",mystr,""));
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.textBox1 = new TextBox();
this.button1 = new Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new Point(8, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new Size(408, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new Point(8, 48);
this.button1.Name = "button1";
this.button1.Size = new Size(408, 48);
this.button1.TabIndex = 1;
this.button1.Text = "change bound data";
this.button1.Click += new EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new Size(5, 13);
this.ClientSize = new Size(432, 118);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
mystr += mystr;
}
}
}