Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Need When I Write text in TextBox1

TextBox2 = have The Same Text



For Example
TextBox I write 12345

TextBox2 Automatic Have The Same Number
Posted
Updated 5-Dec-15 1:31am
v2
Comments
[no name] 5-Dec-15 7:41am    
You can use the Event "TextChanged" of your TextBox1 and copy there the Content of box1 to box 2.
BillWoodruff 5-Dec-15 7:49am    
The key question here is "when" ... when do you want to update TextBox2: every time a character is typed into TextBox1 ? every time the Focus is moved away from TextBox1 ? every time the uses presses the 'Enter/Return' key in TextBox1 ?

Can the user type into TextBox2: if so, do you want to do anything with that ?

Handle the TextBox.TextChanged event for the first textbox, and set the text in the second in the handler:
C#
void TextBox1_TextChanged(object sender, EventArgs e)
    {
    TextBox2.Text = TextBox1.Text;
    }
 
Share this answer
 
Comments
Arasappan 5-Dec-15 8:07am    
U forgot to said Autopostback="true" Griff
[no name] 5-Dec-15 11:23am    
A 5 for a pragmatic Approach.
Here is a nice way to do this-
C#
TextBox1.TextChanged += (s, _) =>
        {
            if (!TextBox2.Focused && TextBox1.Text != TextBox2.Text)
            {
                TextBox2.Text = TextBox1.Text;
            }
        };

        TextBox2.TextChanged += (s, _) =>
        {
            if (!TextBox1.Focused && TextBox2.Text != TextBox1.Text)
            {
                TextBox1.Text = TextBox2.Text;
            }
        };


Check here for detailed explanation about why it is better than other methods-
Reference: How can I sync two TextBox control's Text properties?[^]

Hope, it helps :)
 
Share this answer
 
v2
Comments
BillWoodruff 5-Dec-15 7:54am    
My vote of #1. Copying and pasting code you get from StackOverFlow without your adding some value to it, is really not a good idea. The entry on StackOverflow does not "as you claim" contain any detailed explanation of why it is "better" than any other method.

Newcomers to C# are liable to confused by code that uses lambda expressions for EventHandlers. Also, defining EventHandlers with lambdas means you will never be able to unsubscribe from that event.
Suvendu Shekhar Giri 5-Dec-15 8:21am    
Partly agree.. Partly disagree.

1. You are always welcome to downvote if you think it's not useful.
2. I believe copying correct code never a bad idea. I haven't claimed it as mine, provided the link of original answer in StackOverflow.
3. "Basically I'm responding to the TextChanged even on each text box, but making sure that the target text box doesn't have focus and that the text actually has changed. This prevents infinite back-and-forth loop trying to update the text and it makes sure that the current insertion point isn't changed by the text being overwritten."
I thought, it says a lot, my apologies if it doesn't.
4. Completely agree that new comers will get confused of such code. Will keep this in mind in my future answers.
5. If this section requires a complete descriptive answer then I am confused why they named it "Quick Answer".

Lastly, I respect your thoughts. I will definitely follow your suggestions which I feel, I should have and sorry for others.

Thanks a lot :)
BillWoodruff 5-Dec-15 13:46pm    
Sri Suvendu, You make a very cogent argument, and I like the way you make it ! I think you are right, and I was too harsh in my evaluation. I am changing my vote to #3. Namaste, Bill
[no name] 5-Dec-15 11:22am    
A 5 after start reading the useless hypothetical *) comments/discussions.....

[Edit]*)I forgot paranoid [/Edit]
Suvendu Shekhar Giri 5-Dec-15 11:45am    
Thanks :)
Actually Bill is right on few things and may be a user from StackOverflow, where these kind of answers are not appreciated and advised to post more descriptive and reasonable answers. I really agree some of his thoughts on this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900