simple databinding of a simple string to textbox

Discussion in 'Microsoft C# .NET' started by Robert Ludig, Oct 4, 2004.

  1. Robert Ludig

    Robert Ludig Guest

    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;
    }
    }
    }
     
    Robert Ludig, Oct 4, 2004
    #1
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmad A. Rahman

    TextBox Databinding and Triggered Event (Very Urgent!)

    Ahmad A. Rahman, Dec 14, 2003, in forum: Microsoft C# .NET
    Replies:
    6
    Views:
    182
    Ahmad A. Rahman
    Dec 14, 2003
  2. Pranav Shah

    Re: Textbox and Checkbox databinding

    Pranav Shah, Dec 27, 2003, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    251
    Pranav Shah
    Dec 27, 2003
  3. Pranav Shah

    DataBinding TextBox

    Pranav Shah, Jan 12, 2004, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    234
    Pranav Shah
    Jan 12, 2004
  4. Robert Ludig

    simple databinding of a simple string to textbox

    Robert Ludig, Oct 4, 2004, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    696
    Robert Ludig
    Oct 4, 2004
  5. Kyle Rowe

    databinding a textbox to a string

    Kyle Rowe, Feb 9, 2005, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    166
    Frans Bouma [C# MVP]
    Feb 10, 2005
Loading...

Share This Page