Reputation: 459
i have two text box when i press a button text of textbox one can automatically display on textbox2. i used V S 2010 I need this code in c#
Upvotes: 0
Views: 296
Reputation: 1494
You can do this without using codebehind such as TextBox2.Text = TextBox1.Text;
and without a button. You can do it all in XAML with a single property in your C# code.
your C# code (A.K.A. the ViewModel)
private string _textBoxContent;
public string TextBoxContent
{
get { return _textBoxContent; }
set
{
_textBoxContent = value;
OnPropertyChanged("TextBoxContent");
}
}
and your XAML will look like this:
<TextBox Name="tb1" Text="{Binding TextBoxContent, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Name="tb2" Text="{Binding TextBoxContent, Mode=OneWay}"/>
This will cause changes to tb1 to show in tb2 as you type in tb1. However, it will not change the value of tb1 as you type in tb2.
To have tb1 and tb2 both update each other's value, just use the same binding statement from tb1.
Upvotes: 1
Reputation: 47038
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
public Form1()
{
InitializeComponent();
tb1.Top = 100;
tb2.Top = 100 + tb1.Height;
tb1.TextChanged += new EventHandler(tb1_TextChanged);
this.Controls.Add(tb1);
this.Controls.Add(tb2);
}
void tb1_TextChanged(object sender, EventArgs e)
{
tb2.Text = tb1.Text;
}
Upvotes: 2
Reputation: 9519
on the button click event write this line:
textbox2.Text = textbox.Text;
PS. try to read a book like C# for dummies
Upvotes: 2
Reputation: 49988
If you want the value to change as you are typing it, check out the KeyPress event
textbox1.KeyPress += new KeyPressEventHandler(KeyPressedEvent);
private void KeyPressedEvent(Object o, KeyPressEventArgs e)
{
textbox2.Text = textbox1.Text;
}
Upvotes: 2