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.
Related
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;
}
Im making a calculator.and for the buttons that type numbers, I wrote a condition that if the focus was on text box 1, it would enter the text there, if not, it would enter text box 2. But unfortunately the code does not work and I dont understand the problem.
(WindosForm(.Net framework))
if (textBox1.Focus() == true)
{
textBox1.Text = textBox1.Text + "1";
}
else
{
textBox2.Text = textBox2.Text + "1";
}
Subscribe to "Enter" event for your two textbox and save it. Use the same method for the two textboxes.
TextBox focusedTB;
private void textBox_Enter(object sender, EventArgs e)
{
focusedTB = sender as TextBox;
}
...
this.textBox1.Enter += new System.EventHandler(this.textBox_Enter);
...
this.textBox2.Enter += new System.EventHandler(this.textBox_Enter);
Now you know the last textbox that got focus.
private void button1_Click(object sender, EventArgs e)
{
focusedTB.Text += "1";
}
Your code appears to be attempting to check if the control is focused. The correct way to do that is:
if (textBox1.Focused)
{
// Because 'Focused' is a property. 'Focus()' is a method.
textBox1.Text = textBox1.Text + "1";
}
.
.
.
The answer to your question Why I can not change focus? is that textBox1 receives the focus every time you call this:
if (textBox1.Focus())
As mentioned in one of the comments, here's how the Focus method works:
// Summary:
// Sets input focus to the control.
//
// Returns:
// true if the input focus request was successful; otherwise, false.
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool Focus();
Note: This is a copy-paste of metadata that you can look at by right-clicking over Focus() in your code and selecting Go to Definition then expanding the definition.
I think you talk about Windows Form ?
You cannot manage like this but use event "Enter" of your textboxes, when you click inside the textbox, you give the focus to this textbox and you can do anything inside. Here I put the right focuses TextBox in a variable.
private TextBox _textBoxFocused; //this is always the righ TextBox
private void textBox1_Enter(object sender, EventArgs e)
{
_textBoxFocused = textBox1;
}
private void textBox2_Enter(object sender, EventArgs e)
{
_textBoxFocused = textBox2;
}
I have a form that loads 3 pre-defined scores in a list box. I want to convert a selected score into a string, and then output that string in a textbox. So far i think i've converted the item to a string, and tried setting it to the textbox but it doesn't seem to be working.
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
if (this.lstStudents.SelectedIndex >= 0)
{
string a = lstStudents.Items.Cast<string>().ToString();
txtDisplay.Text = a;
}
btnUpdate.Enabled = false;
Assuming your question is about Windows Forms, One way to get the selected item is to use code like this:
txtDisplay.Text =lstStudents.SelectedItem.ToString();
It is common to want to get the selected item that the user has selected, to do this, you need to place the above code in an event to look like this for example:
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = this.lstStudents.SelectedItem.ToString();
}
An event can be wired to the control either by code or via the VS IDE, you can't just copy and paste the above code. Ask me if you don't know how to do that.
If you want to grab the first item only, then Plutonix comment above applies. You don't need the IF statement.
Since this is the process at load time, why not try just :
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
txtDisplay.Text = lstStudents.Items[0].ToString();
btnUpdate.Enabled = false;
EDIT
then add at the listbox's event SelectedIndexChanged :
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = lstStudents.Items[lstStudents.SelectedIndex].ToString();
}
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;
}