Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 3-Feb-13 6:34am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
milenalukic at 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 at 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 at 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 at 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 at 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.
Sergey Alexandrovich Kryukov at 3-Feb-13 18:38pm
   
Look, if you don't know what control should be clicked, simulating the click cannot help you. Your question was about the simulation of the click, and I clearly explained to you what should you do instead. But now you are shifting it to "determine if there is an underlying control".
 
By the way, one technical detail on event instance, as opposed to the "regular" delegate instance. An even instance cannot not be invoked anywhere except its declaring class. It cannot even be invoked by any derived class. Consider it as some fool-proof feature of event instances. Some controls may expose a method used to invoke some events, but such methods are still the methods of the original declaring class. For example, you such method is not available, there is no a way to add it. Again, this is an important fool-proof feature; it should tell you that "perform the on-click even" makes no sense. In my answer, I explained what makes sense...
 
Everything else is the matter of your design. As you did not explained what is the ultimate purpose of such "click", so it remains unclear what control should be "clicked", by what criteria it should be selected, and, most importantly, why doing that. But even not knowing that, I can say that the situation where the choice of some control should be calculated, suggests that the whole design is wrong; and you probably trying to make things more complex than they should be.
 
The root problem here is this: you are not starting your question from the beginning. Instead of sharing your goal and explaining what you want to achieve, you just to the middle, to some technical question based on your idea about how it should be implemented. What is your idea is wrong (which is quite likely)? That's why it's a good idea to start any question from formulation of your ultimate goal.
 
—SA

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

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 325
2 OriginalGriff 313
3 ClimerChinna 222
4 Zoltán Zörgő 170
0 Sergey Alexandrovich Kryukov 8,243
1 OriginalGriff 7,109
2 CPallini 2,598
3 Richard MacCutchan 1,980
4 Abhinav S 1,778


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100