I have 3 textboxes and I need to clear the text once the text box is clicked.
The only thing is that I have to do that by reusing the same event handler. I created the event handler, gave it a general name to fit all, but now I don't know what statement to write inside it.
if I write : txtBox1.Clear(); it will only clear that one textbox, but if I write: txtBox1.Clear(); txtBox2.Clear(); txtBox3.Clear();
It will clear all of them when only one is clicked.
In other words, I need it to clear only the text of the textbox that has been clicked, but they all have to be under the same event handler.
Anything will help! thanks!
You can assign the same event handler to all the textBoxes and use sender parameter to get the real sender.
private void textBox_Click(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
textBox.Clear();
}
}
Related
I want method to run (event to occur) while i am typing something in textbox ( i dont want to click button) for example i have a a textbox and a listbox in a same form and while i am writing text in textbox it should simultaneously print that in listbox without clicking an button
use Text Changed event for your textbox
Text Changed event
Utilize the TextBox control's TextChanged event.
This event is raised if the Text property is changed by either a programmatic modification or user interaction.
For more information about handling events, see Handling and Raising Events.
You can access it through the properties list, or you can create it at Initialize or Form_Load or even in the Form constructor using:
textBox1.TextChanged += textBox1_TextChanged;
private void textBox1_TextChanged(object sender, EventArgs e) { }
I'm creating form application on c# . I have dragged a textbox with some text in it.
private void textBox1_Click(object sender, EventArgs e)
{
}
Now what is the event for the onlick on textBox1 ?
I need to add this on that function textBox1.Clear();
P.S I searched everywhere. But all i can find is jquery and javascripts... No c#.
EDIT
I tried onfocus like below..but its not working
private void textBox1_OnFocus(object sender, EventArgs e)
{
MessageBox.Show("dsd");
}
If you want to do something when the control is clicked the handle the Click event, not the TextChanged event. Presumably you just double-clicked the control in the designer. That will only handle the default event. To handle other events, open the Properties window, click the Events button at the top and then double-click the appropriate event.
That said, is Click really appropriate? What if the user enters the TextBox using the Tab key? If what you actually want to do is act when the control gets focus then you should handle the Enter event.
You can handle OnFocus/GotGocus event in the TextBox, and clear the text in the textbox.
Hope this helps.
I currently have a form with about 13 buttons on it.
I want to be able to perform a function when one of those buttons are clicked. But I am trying to keep from having 13 different button click events.
Is there some way for me to be able to determine when any button click event is fired, and be able to tell which button fired it?
Thanks!
Have one function that handles all of the click events and use the properties of the 'sender' object to identify the specific button.
You can have different button click handlers, and name them according to button actions, also you can have single event handler for all of them, in this case parameter sender can be cast to button and for example by it's name, finding related button.
But I offer if you have similar behavior on group of buttons map them to a single function, but if actions are different using different method is better, but in all one form with 13 button is not good, you can change them to menu, Tab, ...
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
(ctl as Button).Click += MyButtonHandler;
}
protected void MyButtonHandler(object sender, EventArgs e)
{
Button clickedButton = sender as Button;
//...
}
The first parameter to the callback function is the button that was pressed.
private void OnButtonClicked(object sender, EventArgs e){
Button oButton = sender as Button;
if (oButton != null){
// your logic goes here !
}
}
oButton variables is your current button. (important to check if oButton != null when using as operator)
on the click event you get a reference to the Sender - this is the clicked button so inside that you could test for the Content or Tag and act based on the value:
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as Button).Tag.ToString());
}
I think that in this situation using delgates would be proper. Check these links out for further information on implementation.
The C# Station Tutorial: Introduction to Delegates and Events
Delegates and Events in C# / .NET
Every proper eventhandler has an Object sender parameter. Give all 13 buttons the same handler by typing the same name in the Properties window OnClick event, for instance: OnAnyButtonClicked. The created function will be:
private void OnAnyButtonClicked(object sender, ...)
{
// sender is the button that was clicked,
// find out which button is clicked
// call the corresponding function
}
Tip:
To find out which button is pressed you could use Object.ReferenceEquals
A faster way to avoid if ... then ... else if ... then ... else if...
is using the Tag property of each Button.
Give eacht Button.Tag an enum value corresponding to the action that has to be performed and use a switch statement to find out what has to be done.
You could also assing a delegate to the button.Tag, but that is almost the same as making a different onClick event handler for every button.
Is it possible to know if any of the textbox values have changed in the application.
I have around 30 textboxes and I want to run a part of code only if, any of the textboxes value has changed out of the 30. Is there a way I can know that.
Each text box will raise an event TextChanged when it's contents have changed. However, that requires you to subscribe to each and every event.
The good news is that you can subscribe to the event with the same method multiple times. The handler has a parameter sender which you can use to determine which of your 30 text boxes has actually raised the event.
You can also use the GotFocus and LostFocus events to keep track of actual changes. You would need to store the original value on GotFocus and then compare to the current value on LostFocus. This gets round the problem of two TextChanged events cancelling each other out.
You can assign an event handler to each of the TextBox's TextChanged events. All of them can be assigned to the same event handler in code. Then you'll know when the text changes. You can set a boolean flag field in your class to record that a change occurred.
This is perhaps on the rough and ready side, but I did it this way.
In the constructor, I created
bool bChanged = false;
In the TextChanged event handler of each control (actually same for each), I put
bChanged = true;
When appropriate, I could do some processing, and set bChanged back to false.
You can also just do this:
In your Constructor:
MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
And Then this Method:
private void TextChanged(object Sender, TextChangedEventArgs e){
//Do something
}
try this. Add this code to the load/constructor. no need to specify the event in the XAML explicitly
this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextChanged));
private void TextChanged(object Sender, TextChangedEventArgs e)
{
//ToDO (use sender to identify the actuale text from where it fired }
}
In WinForms I could handle the Validated event to do something after the user changed text in a TextBox. Unlike TextChanged, Validated didn't fire for every character change; it only fired when the user was done.
Is there anything in WPF I can use to get the same result, an event raised only after the user is done changing the text?
LostFocus will fire when the user moves from your textbox onto any other control.
It seems that there is no native solution.
The LostFocus event is a good idea. But when the user click on Enter, he wants the TextBox to validate the change.
So here is my suggestion : use the LostFocus event and the KeyDown event when the key is Enter.
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
// code to lauch after validation
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// call the LostFocus event to validate the TextBox
((TextBox)sender).RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
}
}
LostFocus is not equivalent to Validate. It creates lots of problem when you have multiple text boxes on one screen and every text box has some logic written in Validate. In validate event you can control focus easily but not in LostFocus so easily.
You can also try Binding.ValidationRules
Documented at : http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx
Here is the article to get started:
How to implement binding validations :
http://msdn.microsoft.com/en-us/library/ms753962.aspx