locked
Click event on a Form doesn't work RRS feed

  • Question

  • Hi everyone,

    I have an issue and I don't know how to deal with (that's why I'm here).

    I have a complete application with different kind of boxes and buttons (I'm not familiar with all this part of programming). One thing I want is catching every click on my HMI. I am able to catch the event ("Click") when the user clicks on a "System.Windows.Forms.Panel" since I configured the properties of this panel to fire an event and do something when "Click" action occurs. I actually did the same with my "System.Windows.Forms.Form", but it doesn't work

    Is there something I should know about Properties of this kind of Event or Class ?

    I use the Visual Studio 2010 designer editor (the tool which lets you drag and drop buttons and all you need to make your HMI).

    Here's the code which is generated by VS and which should enables to fire something once a click occurs:

    this->Click += gcnew System::EventHandler(this, &Frm_Main::Frm_Main_MouseClick);

    This part of code is also present for the other boxes and buttons of the HMI and it does work for them.

    I can also catch Events such as KeyPressed, FormClosing or Loading [...] on that Form... But it doesn't work with "System::Windows::Forms::MouseEventArgs" such as MouseDown, MouseUp...

    I hope I'm clear enough.

    Thanks

    (This post has already been submitted here : http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/1642f13c-118a-4d77-b94e-9afab17a214a)



    Thursday, January 3, 2013 2:43 PM

Answers

  • Override WndProc and catch: 

    "msg=0x210 (WM_PARENTNOTIFY) hwnd=0x1de045e wparam=0x201 lparam=0x19a0153 (WM_LBUTTONDOWN) result=0x0"

    Friday, January 4, 2013 9:42 AM

All replies

  • The event for the code that you posted should fire when someone clicks on the form, which means literally click on the form such as in an empty space of the user area of a form.  It is not a catch all for detecting any mouse click at any location on the form, if that is what you were looking for.

    For more subtle control over detecting events or messages being sent to your form, you may wish to look at overiding the following method.

    Form.ProcessCmdKey method

    Hope this helps.

    Rudy    =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    Thursday, January 3, 2013 4:01 PM
  • Hi Rudy,

    Thanks for your answer.

    I just want to fire a basic event (count the clicks) when someone click on my form, somewhere on the form where there is nothing but a typical grey background. 
    I use other events and methods to catch clicks on other forms such as buttons or Panel.

    If the code I posted above is supposed to do what I want it currently does not.

    I'm going to have a look at the Form.ProcessCmdKey method.
    Thursday, January 3, 2013 4:29 PM
  • If your click event fires with a totally empty form, that is the Form's Control collection is empy, the problem is in your code.  Otherwise you have a problem with your installation.
    Thursday, January 3, 2013 8:50 PM
  • This form worked for me.  It is a blank form with no controls.  File is "Form1.h"

    #pragma once
    
    namespace CPP_WinForms_Click {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary>
    	/// Summary for Form1
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
                this->Click += gcnew System::EventHandler(this, &Form1::Form1_Click);
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
                this->SuspendLayout();
                // 
                // Form1
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(284, 262);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                this->ResumeLayout(false);
    
            }
    #pragma endregion
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 }
    
                 System::Void Form1_Click(System::Object^  sender, System::EventArgs^  e) {
                     this->Text = L"Click";
                 }
        };
    }
    

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/


    • Edited by Rudedog2 Thursday, January 3, 2013 8:52 PM
    Thursday, January 3, 2013 8:51 PM
  • Thanks for your answers.

    I think I got the reason why it doesn't work: my form is a MDIcontainer...
    Is there a way to catch my click on a mdicontainer ?


    Otherwise I'm going to follow this : http://social.msdn.microsoft.com/Forums/en/vbinterop/thread/b498ac7b-9fc5-43cd-9b35-84a3518875c0
    • Edited by Grandblack Friday, January 4, 2013 9:24 AM
    Friday, January 4, 2013 9:10 AM
  • Override WndProc and catch: 

    "msg=0x210 (WM_PARENTNOTIFY) hwnd=0x1de045e wparam=0x201 lparam=0x19a0153 (WM_LBUTTONDOWN) result=0x0"

    Friday, January 4, 2013 9:42 AM
  • Wow thanks a lot, it works fine !

    EDIT: I'd like to mark your answer "as answer" but it doesn't work. I hope a moderator could do it.

    • Edited by Grandblack Friday, January 4, 2013 1:07 PM
    Friday, January 4, 2013 1:05 PM