Simulate mouse click on a focused TextBox - c#

Is this possible? I have a button with onClick() Method. If i click the Button i want to simulate a mouse click event inside a TextBox. With Focus() i can focus inside the TextBox but how to simulate left mouse click?
EDIT I have an AutoCompleteExtender that should show a list of names in a certain TextBox after the user clicks a Button. The TextBox can work both:the user writes a character and the list appears or he clicks the button and a whole list appears.
EDIT I have used AjaxControlToolkit and from my experience i have to click on the TextBox if i want the list to appear even though i have a focis on the TextBox and MinimumPrefixLength=0

Create a "logic" method where you do your work. When the user clicks on the TextBox, call your logic method. When you want to simulate a click, call your logic method also.
// Attach to the event
myTextBox.Click += new EventHandler(myTextBox_Click);
// Simulate a "click" event
MyLogic();
protected void myTextBox_Click(object o, EventArgs e) {
MyLogic();
}
private void MyLogic() {
// Do work
}

Related

Excluding autocompleting textbox from form accept property?

I have a form in which I've linked the accept property to a button called submit on the form so that whenever enter key is pressed on any control in the form it fires the submit button's click event. I also have an auto completing textbox (to/from) on the form with auto complete mode set to SuggestAppend. I want to move to the next control(textbox amount) when the user presses the enter key. I tried the textbox's keyup event like this:
private void tb_to_from_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
{
tb_amount.Focus();
}
}
This moves the control to the other textbox, however, the submit button is also pressed. How do I exclude tb_to_from from the accept property of the form so that it just moves to the next control?

Focus in parent even if click on child element

I have a UserControl (let's call it "PresentationCell") which contains a label, and an PictureBox.
In another control, which is using this PresentationCell, I have added an event
presentationCell.GotFocus += OnFocus;
private void OnFocus(object sender, EventArgs e)
{
if (sender is PresentationCell current)
current.BackColor = Color.Azure;
}
This will not be fired, if I click / focus on the Label or PictureBox that is within the PresentationCell.
How can I make it fire, when just something within the PresentationCell is in focus?
The problem here is, that the Label and PictureBox controls aren't selectable controls, so they aren't able to receive focus from mouse clicks.
What you could to instead, is to handle the mouse click event and check if you have hit the PresentationCell. If the PresentationCell is hit you can programatically set the focus like so:
hitPresentationCell.Focus();
This will then fire the GotFocus event.
In your OnFocus method you will have to switch the focus to another control or the event will fire endlessly.

Windows Forms event for any action performed on the form?

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();
}

Onclick event for textbox in csharp form application

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.

Windows Forms - detecting button click from a panel inside the form

I have Windows Form named - Form1 and inside Form1 I have a panel named panel1. I use this panel only to add buttons in him. For now there are exactly 9 buttons but I intend to change their number dynamicly if this has something to do with my current problem. What I need is way to detect a when a button from this panel is clicked (I have other buttons too but, they are in Form1 outside the panel) and also to know exactly which button was clicked.
I tried this:
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("HI" + sender);
}
As you can see, it's not much, but was enough to see that I can't do that using pnael1's_click event. Using this code I get the message box when I click anywhere in the panel except the buttons. So how can I do that. Is it possible to do it from inside panel1 or I should group those buttons using another approach but it's important to be able to keep the difference between those buttons which are now in panel1 and the other buttons I may (and in in fact I do have)?
When creating the dynamic buttons, you register that button instance's Click event and attach to an event handler (a single handler can handle all buttons' click event):
var dynamicButton1 = new Button();
dynamicButton1.Click += MyButtonClickHandler;
As long as MyButtonClickHandler has a signature that's suitable for a Click event (that's any method returning void and taking an object and an EventArgs, the handler should respond to a dynamic button's click event for as long as the button instance exists.
As long as you aren't adding controls dynamically over time, and the number of buttons is fixed as soon as the form is initialized, you can use this to add a click event handler to all buttons within a panel:
foreach (var button in panel.Controls.OfType<Button>())
{
button.Click += HandleClick;
}

Categories

Resources