Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have a windows forms application. When I click on a window this activates the form and then I need to click again to call the particular control click event. For example if I click on a button this activates the form and then I need to click the button again.

Is there a way to perform the control click and window activation in one click? Preferably I would want this to work with whatever the clickable control is (menu,button, label etc)

Thanks
Posted

1 solution

Yes, there are such ways, but you never should do it for "regular" UI development. This is a total abuse which makes your code unreliable and bad for support. This is only the indication that you have no idea how to develop UI legitimately, nothing else.

All you need is to learn some UI development.

In your particular case, you have more then enough, without click simulation: you have the event System.Windows.Forms.Control.Click of a button, and the event System.Windows.Form.Activated, or, alternatively, as you are writing you form class anyway, you can simply override the virtual form method System.Windows.Forms.Form.OnActivated.

In your question, you never explain why would you like to do some action on activation, and why such action should be the same as on the button click. I doubt you really need exactly that, but here, you also already have everything you need. For this, you only need to be able to… write code, in particular, method (instead of just filling in those created by the designer).

Just create some form instance method doing what you need. Call this method from the button click event and from the activation event. Got the idea?

—SA
 
Share this answer
 
Comments
milenalukic 3-Feb-13 13:42pm    
Thanks for your reply but sorry I don't get the idea.

In my application users may have multiple forms open. When they click on say a menu item of an inactive form this does not work (because the first click activates the form). The request is to make the menu work with the first click. Apart from a menu this could be any other clickable control.

On form activated I will not know which control has been clicked (or I do not know how to find out). Some sample code would be appreciated as I have spent a week on google and got no where so far.
Sergey Alexandrovich Kryukov 3-Feb-13 13:45pm    
You need to design UI clearly. My explanation is way to simple to illustrate it. I am not sure you are ready to develop UI at all, at this moment. You are getting lost in simple things....
—SA
milenalukic 3-Feb-13 13:50pm    
If I knew I would'n be asking!!!

If on an menu strip I use theSystem.Windows.Forms.Control.Click how do I tell it to click again. That is what I need to know.

So far the best I managed was to activate the form on mouse over and then the control click works fine. But this is not ideal and I would like the form to be activated when it is clicked on and if there is an underlying control (button or menu etc) to perform that click event.

Maybe you are not understanding what I need to do.
Sergey Alexandrovich Kryukov 3-Feb-13 14:25pm    
You should not "click" (only hardware can truly click), you should call the same method you call from your click event handler. Which part of this statement is not clear?
—SA
milenalukic 3-Feb-13 14:36pm    
Yes the hardware will perform the click. I need to identify the control that the user clicked on to run the onclick code. What I would expect is some code to go under the form activated event that will determine if there is an underlying control and if so to perform the on click event of that control. apologies if this was not clear in the first place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900