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.
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) { }
Is there a "generic" event for a Windows Form that will trigger any time the user clicks anything on a form? I need to make sure that a certain text box has input focus at all times, so if the user clicks on a button (or anything else, for that matter), the input focus needs to be redirected to this text box.
Is there an event in the Form class that will handle this, or do I have to handle a Click event to all controls recursively on the form?
I did some digging but I can't find what I want so far.
I actually figured something out - every time the TextBox loses focus, just refocus it.
textBox.LostFocus += TextBox_LostFocus;
private void TextBox_LostFocus(object sender, EventArgs e)
{
textBox.Focus();
}
It seems like a straight forward thing, but I can't figure it out.
How can I check if a certain button was clicked from user control?
my user control is uc_test and button name is btnTest .
I assume its some sort of event handler added to uc_test.btnTest ?
I'm working in WinForms.
It sounds like you've created a button but want to make it do something on click. I don't often use Winforms but from memory double-clicking on the control in the form should automatically create a btnTest_Click method. If not, just go to code and put in a method like:
protected void btnTest_Click(object sender, Eventargs e)
{
//do something
}
Then set in the properties of the button the OnClick event to btnTest_Click.
here's my problem .. i'm doing a calculator in C# and i don't want to click every single button to make a operation, i wanna handle it with my num pad .. like
if i press "1" , show me in the textbox "1".
i changed
private void cmd1_Click(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '1')
{
txtShow.Text='1';
}
}
and i'm having this error :
No overload for 'cmd1_Click' matches delegate "System.EventHandler"
this.cmd1.Click += new System.EventHandler(this.cmd1_Click);
what the hack is wrong with this?
Cheers.
change
this.cmd1.Click += new System.EventHandler(this.cmd1_Click);
to
this.cmd1.KeyPress += new System.EventHandler(this.cmd1_Click);
You'll probably want to rename cmd1_Click too.
And as mentioned in the answer above, this would be better on the Form itself, rather than each button.
You are trying to attach an event handler that corresponds to a KeyPress event to a Click event.
There is something wrong here (bad copy/paste?):
private void cmd1_Click(object sender, KeyPressEventArgs e)
It's named as an auto-generated event handler for the Click event on cmd1, but its definition is the definition for a KeyPress event handler.
Which event do you want to handle? KeyPress or Click or both?
Click is a mouse event, you need to attach to a keyboard event if you want to receive keyboard event args, you'd have to put all your calculator buttons in a common pannel and handle both the button click "and" the text being sent to the panel, that way you could react to both keypresses anywhere and to click for the same result.
An easy way to handling events for all the buttons without doing it one by one is to have a single button click handler and check the text property of the control to know how to act (cast the sender to a button and check the text, do a switch on that)
Not tested:
switch(((button)sender).Text)
{
case "1":
// react to user having pressed 1 etc etc
}
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