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.
Related
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;
}
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.
In a TextBox I'm monitoring the text changes. I need to check the text before doing some stuff. But I can only check the old text in the moment. How can I get the new Text ?
private void textChanged(object sender, EventArgs e)
{
// need to check the new text
}
I know .NET Framework 4.5 has the new TextChangedEventArgs class but I have to use .NET Framework 2.0.
Getting the NEW value
You can just use the Text property of the TextBox. If this event is used for multiple text boxes then you will want to use the sender parameter to get the correct TextBox control, like so...
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
}
}
Getting the OLD value
For those looking to get the old value, you will need to keep track of that yourself. I would suggest a simple variable that starts out as empty, and changes at the end of each event:
string oldValue = "";
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
// Do something with OLD value here.
// Finally, update the old value ready for next time.
oldValue = theText;
}
}
You could create your own TextBox control that inherits from the built-in one, and adds this additional functionality, if you plan to use this a lot.
Have a look at the textbox events such as KeyUp, KeyPress etc. For example:
private void textbox_KeyUp(object sender, KeyEventArgs e)
{
// Do whatever you need.
}
Maybe these can help you achieve what you're looking for.
Even with the older .net fw 2.0 you should still have the new and old value in the eventArgs if not in the textbox.text property itself since the event is fired after and not during the text changing.
If you want to do stuff while the text is being changed then try the KeyUp event rather then the Changed.
private void stIDTextBox_TextChanged(object sender, EventArgs e)
{
if (stIDTextBox.TextLength == 6)
{
studentId = stIDTextBox.Text; // Here studentId is a variable.
// this process is used to read textbox value automatically.
// In this case I can read textbox until the char or digit equal to 6.
}
}
I want to call a generic handler function for all textBox on GotFocus and LostFocus.
For GotFocus I could create:
private void GotFocus()
{
TextBox textBox = ((TextBox)FocusManager.GetFocusedElement());
textBox.Text = "";
}
and call it like this:
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
//Instead of this in every TextBox
//TextBox textBox = (TextBox)sender;
//textBox.Text = "";
GotFocus();
}
But for LostFocus I can't do the same to get some symetry handler ? Am I obliged to manage the memorization of the control in GotFocus in a private member so as to be able to use it later in LostFocus ?
Aren't there any way to do this more globally by hooking the .NET system and create this missing GetPreviousFocusedElement function ?
I like the Law of Symetry, that's how Physicians discover new laws and I think this principle could also apply in IT.
The parameter object sender contains a reference to the control.
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = "";
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = "";
}
or whatever you want the LostFocus method to do.