simple databinding of a simple string to textbox

R

Robert Ludig

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;
}
}
}
 
R

Ravichandran J.V.

textBox1.DataBindings.Add(new Binding("Text",mystr,""));

In VB.net, you can simply pass the Property name and the object as
parameters than as a new Binding object. Maybe, it is the same in C#.
Why don't you try

textBox1.DataBindings.Add("Text",mystr);


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
R

Ravichandran J.V.

A correction to my previous post.

"textBox1.DataBindings.Add(new Binding("Text",mystr,""));"

In VB.net, you can simply pass the Property name and the object as
parameters than as a new Binding object. Maybe, it is the same in C#.
Why don't you try

textBox1.DataBindings.Add("Text",mystr,"PropertyNameOftheObject");

"PropertyNameOftheObject" is the datastore/member of the mystr object.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
R

Robert Ludig

Ravichandran said:
"PropertyNameOftheObject" is the datastore/member of the mystr object.

Now that is exactly my problem. mystr is a string object, how can it
have datastore/member. What do I need to specify as
"PropertyNameOftheObject"

???
 

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