Simultaneously update two textbox in C# - c#

When I insert data in textbox1, I need that in textbox2 is placed same data simultaneously.
I dont know which event permits this feature. Im using visual studio 2010.
thanks in advance!

Use the TextChanged event on textbox1 and use it to update textbox2:
// This can be done via designer too.
this.textbox1.TextChanged += new EventHandler(textbox1_TextChanged);
Now, in your form class:
void textBox1_TextChanged(object sender, EventArgs e)
{
textbox2.Text = textbox1.Text;
}
Hope this helps.

Add this handler to the TextChanged event of textBox1:
private void OnTextChanged(object sender, EventArgs e)
{
var textBox = sender as TextBox;
textBox2.Text = textBox.Text;
}

Related

C# Windows forms Calculator - Selecting textbox per click

First of all, I'm german, 2. here is a screenshot of my calculator.. kinda weird but well i know xD
So, I'm looking for a Command:
I got 4 Textboxes, the 1st one(textBox1) for the 1. Number,
the 2. one(textBox2) for the Operator (+-*/),
the 3. one for the 2. Number
and the 4. one for the Result
if i do:
private void button3_Click(object sender, EventArgs e)
{
textBox1.SelectedText += "1";
textBox3.SelectedText += "1";
}
or
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
textBox3.Text = textBox3.Text + "1";
}
the Number from the button I click on is shown in both textboxes now (1. and 3. one).
But I want that the Number of the button i clicked on is going to be in the Textbox i selected first.
So i first do select the textbox1 or textbox3 with simply clicking in it and then i click on a button(for example 1) and then the number 1 is going to be in the textbox i selected/clicked.
On the _Activate Event for both Textboxes, store which box was activated. Then use that in the _Clicked event:
private TextBox currentTextBox;
// Both textboxes can use this function
private void textbox_Activate(object sender, EventArgs e)
{
this.currentTextBox = (TextBox) sender;
}
private void button3_Click(object sender, EventArgs e)
{
currentTextBox.Text = textBox1.Text + "1";
}
You can have a global variable TextBox textBoxSelected then in a event textBox1_Click and textBox3_Click set the variable. Later in button3_Click choose the textboxselected and add your logic.
Try this:
TextBox textBoxSelected;
private void txtBox1_Click(object sender, EventArgs e)
{
textBoxSelected = textBox1;
}
private void txtBox3_Click(object sender, EventArgs e)
{
textBoxSelected = textBox3;
}
private void button3_Click(object sender, EventArgs e)
{
textBoxSelected.Text += "1";
}
I can't make much sense of your question, but I have noticed an issue in your logic.
C# will be adding them as strings, which results in concatenation.
Convert the values to integers first and it will add correctly.
textBox1.Text = int.Parse(textBox1.Text) + 1;
As for you actual question, if you want to have a way of "remembering" what text box you clicked, add an event to the Click event to store the text box control that you have selected in a variable, and then use that in your logic.
So here are some recos:
1/ Naming convention: Use clear names that refer to the button function or the textbox content. Say for example: TextboxLeftOperand, ButtonN1, TextboxOperator, ...
2/ Use a new variable called SelectedTextbox
var SelectedTextbox = new Textbox();
3/ Add to the click event of both textboxes an assignment of the SelectedTextbox.
For the left textbox:
SelectedTextbox = TextboxLeftOperand // textbox1 in your case
And for the right textbox
SelectedTextbox = TextboxRightOperand // textbox1 in your case
4. Now all you have is work with your new variable.

update labels when the text box is updated

I made a small calculator and i need to make the labels update when textbox is changed ,
and want some help to do that
Thanks
Use the TextChanged event of the TextBox and apply your logic and label setting there
private void textBox1_TextChanged(object sender, EventArgs e)
{
lable1.Text = "Xyz";
}

How to change text of Label while typing in TextBox

Please I'm new to C#, I created a textBox and a label. What i am expecting is, if I type a value into the textBox, I want it to display on the label and if I change the value it should also change immediately on the label.
it work with the code below and i press enter key
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
label1.Text = textBox1.Text;
}
}
But I want it without press Enter/Return Key on keyboard.
Thanks for understanding
This works for VisualStudio
Select your TextBox in the Designer, go to the it's properties and click on the events (teh icon with the lightning). Then make a double click on the event that is called: TextChanged.
This creates a new function, that will always be called when the text of your TextBox changes. Insert following code into the function:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
label1.Text = tb.Text;
}
That's it.
label.DataBindings.Add("Text", textBox, "Text");
textbox KeyDown/Up/Press events may help.
For example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
label1.Text += e.KeyData.ToString();
}

ASP.net Textbox that adds "ING"

I have a textbox for verbs and I want it to auto add "ing" to any word the user tries typing in that box if its not there. Is this possible? And how?
My verb textbox is:
tbVerb
Think this should do what you want.
if(textbox.text.substring(textbox.text.lenght-3) != "ing")
textbox.text += "ing";
But you will need a validate or event as the guy in the other answer suggests.
Use the TextChanged Event:
private void tbVerb_TextChanged(object sender, EventArgs e)
{
// add your value to checkbox
// this code is WinForms
// tbVerb.Text = tbVerb.Text + "ing";
}
private void tbVerb_TextChanged(object sender, EventArgs e)
{
if (!tbVerb.Text.EndsWith("ing"))
tbVerb.Text += "ing";
}

C# - How to write a Leave Event for a textbox that has been created dynamically

I have a dynamically created text box. I want to register a function on the Leave event. How do I do that?
textbox = new TextBox();
try:
textbox.Leave += new EventHandler(textBoxLeave);
actual handler:
private void textBoxLeave(object sender, EventArgs e)
{
// put your code here
}

Categories

Resources