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:
    217
    Ahmad A. Rahman
    Dec 14, 2003
  2. Pranav Shah

    DataBinding TextBox

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

    simple databinding of a simple string to textbox

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

    databinding a textbox to a string

    Kyle Rowe, Feb 9, 2005, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    233
    Frans Bouma [C# MVP]
    Feb 10, 2005
  5. AEgir Sveinsson

    Trouble with databinding on a textbox

    AEgir Sveinsson, Oct 11, 2005, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    148
    AEgir Sveinsson
    Oct 11, 2005
  6. Ivan Sammut

    Webform textbox Databinding

    Ivan Sammut, Mar 6, 2006, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    279
    Ivan Sammut
    Mar 6, 2006
  7. listviewitem textbox databinding

    , Mar 30, 2007, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    186
  8. Chakkaradeep

    WPF Databinding TextBox with a property

    Chakkaradeep, Dec 6, 2007, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    340
    Nicholas Paldino [.NET/C# MVP]
    Dec 6, 2007
Loading...