i have a page with 2 textbox items and a button
textbox1 contains a word , and textbox2 is empty
now i want to put content of TextBox1.Text in TextBox2.Text with button click,
i tried:
protected void Button1_Click(object sender, EventArgs e)
{ Page.FindControl("TextBox2").Text = TextBox1.Text; }
this code don't work ,how to make this work?
TextBox textbox2= (TextBox)FindControlRecursive(Page, "TextBox2");
try using this, referencing this article
you need to define it first then apply properties
protected void Button1_Click(object sender, EventArgs e)
{
TextBox textBox2 = (TextBox)Page.FindControl("TextBox2");
textBox2.Text = TextBox1.Text;
}
Related
Is it possible to have a click event that displays the text of buttons in a label in c#.
I want to write single code that will work for:
public button1_Click(Object sender, EventArgs e){
label1.Text = button1.Text;
}
public button2_Click(Object sender, EventArgs e){
label2.Text = button2.Text;
}
Is this what you are looking for?
On a button click, you can handle setting text as below
you can add two buttons. Have same click events for both buttons.
Take help of parameter sender to get which button is clicked.
code (sender as Button) gives you all the details of clicked button.
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = (sender as Button).Text;
}
1) Add a Button and a Label to the blank Form in a new project.
2) Double-click the Button.
This will generate the following Click event method...
private void button1_Click(object sender, EventArgs e) {
}
3) Put the following line of code in the button1_Click method body:
label1.Text = button1.Text;
4) Profit.
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.
I'm having a problem with making a button display the input in a textbox to a label. This is how it looks like.
Now forget about the radio buttons and the check boxes. I want what the user types inside the text box with the placeholder "Name:" to display to the label that's to the right of the button.
// Name TextBox
//***********************************************************
//Enter your name textbox
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//Empties the textbox once it's focused
private void textBox1_Enter(object sender, EventArgs e)
{
if(textBox1.Focus())
textBox1.Text = String.Empty;
}
//Resets the placeholder text for password textbox
private void textBox1_Leave(object sender, EventArgs e)
{
if(!textBox1.Focused)
textBox1.Text = "Name: ";
}
//***********************************************************
// Password TextBox
//***********************************************************
//Enter your password textbox
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
//Empties the password textbox once it's focused
private void textBox2_Enter(object sender, EventArgs e)
{
if(textBox2.Focus())
textBox2.Text = String.Empty;
}
//Resets the placeholder text for password textbox
private void textBox2_Leave(object sender, EventArgs e)
{
if(!textBox2.Focused)
textBox2.Text = "Password: ";
}
//***********************************************************
//Display Name button
private void button1_Click(object sender, EventArgs e)
{
label5.Text = textBox1.Text;
}
//Display password button
private void button2_Click(object sender, EventArgs e)
{
label6.Text = textBox2.Text;
}
1) Just to clarify: I've made the initial text inside the text boxes a placeholder. And the placeholder text will reappear inside the text box once it loses focus. I've used both focus() method and focused property because I simply do not know the difference.
(I don't know if I should ask about the difference between the two in another question, so please let me know)
2) Now when I input whateva into the text box, then press the display button, the default placeholder text reappears and the input do not appear in the label. Naturally, I also don't want the placeholder text to ever appear at the label.
Obviously I'm new at Windows Forms and worse, I find it hard to articulate my questions when writing WinForms applications. So if any code is missing from my question, let me know.
You just need to change the check, if user has made any changes then you won't reappear the placeholder:
//Resets the placeholder text for password textbox
private void textBox2_Leave(object sender, EventArgs e)
{
if(!textBox2.Focused && textBox2.Text.Trim() == String.Empty)
textBox2.Text = "Password: ";
}
//Resets the placeholder text for password textbox
private void textBox1_Leave(object sender, EventArgs e)
{
if(!textBox1.Focused && textBox1.Text.Trim() == String.Empty)
textBox1.Text = "Name: ";
}
TextBox.Focus() is a method which make textbox an active control of the form. It also sets TextBox.Focused property to true.
This does look like a homework question, so I won't give you the answer, but I will help you out with some suggestions.
You need to stop being lazy with Control names. We all do it. But it has to stop. Six months from now you won't remember what label5 is.
I suggest you begin renaming the controls on your form. For example, change the Name textbox to nameTextBox, and change the Name button to nameButton, and change the Name label to nameLabel.
Breakpoints. Use them. When something doesn't work out as expected, set a breakpoint on the line where you are expecting something to happen. For example, if you write:
nameLabel.Text = nameTextBox.Text; then you should set a breakpoint on that line, Debug your app and step through, watching the output window for anything that doesn't look right.
I just need to do what the user types into a textbox repeat a label in c # on the same webform
Use the Text property of the label to set the text of the label, based on what is in textBox1 at the point in time when the TextChanged event fires. You should be able to access the controls in the code behind.The general form is
private void textBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = textBox1.Text;
}
Try
get the textbox's text to a string and then set the string to the label
string text=textbox1.Text;
label1.Text=text;
or
label1.Text=textbox1.Text; // in a single line
it can be put inside a button click
private void button1_Click(object sender, EventArgs e)
{
Label1.Text = textBox1.Text;
}
I'm trying to do something very simple ( at least I do think so). I have a Form with a Split Container and each part (they are two at all) has only a textBox and the idea is while I'm writing at textBox1 to visualize this text at textBox2. I figure out how to pass some data from the one box to the other but I don't know how to get the value of the textBox and pass it to the other. An again I want to mention that the 2 textBoxes are in one form, so it should be a pretty easy task I suppose.
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2.AppendText("I want to pass the text form textBox1");
}
}
And also, I'm using Visual Studio 2010.
Is it as simple as:-
textBox2.Text = textBox1.Text;
?
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
try this
`private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2 = textBox1.Text;
}`
If you just want to get the text from textbox1
use this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2 = textBox1.Text;
}
but if you want to add the text from textbox1 to textbox2
use this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2.AppendText(textbox1.text);
}