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;
}
Related
I have several numericupdown controls on a Winform. I have all the NUDs on the form coded to summarize all the NUDs inside a textbox. My issue is not with that, that happens perfectly. My issue is, that in order for the value from any of the NUDs to summarize inside the texbox, I have to either press enter or click inside any other NUD. Clicking on Tab will not work. I want the value inside the texbox to be updated as I type inside any of the NUDS, without having to press enter or give focus to another NUD. How can I do that? By the way, I've placed the code to summarize all the NUDs inside the ValueChanged event of each NUD. This is the code I have placed inside each NUD ValueChanged Event. Thank you
private void NudNumberOne_ValueChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NudNumberOne.Text) && !string.IsNullOrEmpty(NudNumberTwo.Text) && !string.IsNullOrEmpty(NudNumberThree.Text))
textBox1.Text = (double.Parse(NudNumberOne.Text) + double.Parse(NudNumberTwo.Text) + double.Parse(NudNumberThree.Text)).ToString();
}
The ValueChanged() event doesn't fire until the Text value has been parsed successfully into a numeric value and assigned to the NumericUpDown control. You've already found that this doesn't happen until you hit Enter or change controls.
The NumericUpDown doesn't expose a TextChanged() event...but it's easy enough to cast it back to Control and wire it up that way. Just do this in the Load() event of your Form:
private void Form1_Load(object sender, EventArgs e)
{
((Control)NudNumberOne).TextChanged += Ctl_TextChanged;
((Control)NudNumberTwo).TextChanged += Ctl_TextChanged;
((Control)NudNumberThree).TextChanged += Ctl_TextChanged;
}
private void Ctl_TextChanged(object sender, EventArgs e)
{
try
{
textBox1.Text = (double.Parse(NudNumberOne.Text) + double.Parse(NudNumberTwo.Text) + double.Parse(NudNumberThree.Text)).ToString();
} catch (Exception ex) {
textBox1.Text = "";
}
}
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.
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();
}
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've got a program with a lot of text boxes that I've got text in that I want to be cleared on _click and then reset to default if nothing is entered and the user clicks away.
The way I was going to do it is clearly inefficient, having to name the text box each time and I'd like to know how I could go about streamlining it.
this is what I've got at the minute, and I'd have to change the txtUserName for the text box field name each time
private void txtUserName_Click(object sender, EventArgs e)
{
txtUserName.Text = ""
txtUserName.ForeColor = Color.Black;
}
is there a way I can do essentially
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
string caller = //Get this textbox name
this.ClearBoxes(caller)
}
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
Yes, you can try this (though it's not generic but there is no need for generics in this case):
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if(tb != null) tb.Text = "";
}
And you can attach this method to all your textBoxes Click event.
textBox1.Click += txtAnyTextBox_Click;
textBox2.Click += txtAnyTextBox_Click;
I don't think this is gonna work:
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
If you want to use ClearBoxes method you should pass it your TextBox element.But there is no need for this,you can directly clear your textBox as shown above code.
Also if you want to clear all TextBoxes in the same time,for example one button click you can use this:
private void button1_Click(object sender, EventArgs e)
{
foreach (var tBox in this.Controls.OfType<TextBox>())
{
tBox.Text = "";
}
}
You can use the sender argument for that.
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
var textbox = sender as TextBox;
this.ClearTextbox(textbox)
}
private void ClearTextbox(TextBox textbox)
{
textbox.Text = "";
//...
}
You can get name of textbox from sender of event:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
string caller = textBox.Name;
this.ClearBoxes(caller); // call your custom method
}
If you want to simply clear textbox text, then you don't need to get its name - you can use it's Clear() method:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Clear();
}
Also you can consider creation of custom textbox, which will have some default value and will resent itself to default when clicked:
public class CustomTextBox : TextBox
{
public string DefaultText { get; set; }
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
Text = DefaultText;
}
}
Use custom textboxes instead of default textboxes, and provide DefaultText value for each custom textbox which should reset itself to something more meaningful than empty string (you can use Properties window for that).
This would be quite nasty - as you'd cause a page reload every time someone clicked in the text box.
A far simpler way would be to do it in javascript.
just add a function to clear the text box, and then maybe use a css selector to enable the function for every text box you want to use it in.
e.g.
<input type="text" class="clearme" />
$(".clearme").click(function() {
$(this).val('');
});
this will do it all client side without causing any post backs.