Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have mdi form containing TablayoutPannel,picturebox,child form.
When i clicks on MDI picturebox it fires picturebox click event.It will not fires MDI form click event.
how to fire MDI form_click event when user clicks on MDI picturebox control?
Posted
Comments
BillWoodruff 2-Jan-14 5:19am    
Are you saying you have a PictureBox and a TableLayoutPanel placed on the MDIParent Form ?
RhishikeshLathe 2-Jan-14 5:55am    
MDI form containing tablelayoutpannel,
in tablelayoutpannel there is picturebox
BillWoodruff 2-Jan-14 6:11am    
You've got me curious now; what do you do when the user drags the MDIChildForm over the TableLayoutPanel and part, or all, of it is hidden beneath the TableLayoutPanel.

That's not the way MDI was intended to be used, although it's fine with me if you use it that way :) If you want suggestions on alternative strategies for the UI, just ask.
RhishikeshLathe 2-Jan-14 7:25am    
Thanks. For your reference:- i have implemented functionality of shortcut tooltips, such as ms-word. In ms-word when we press Alt key it displays shortcut tooltips & when we clicks mouse anywhere on ms-word it hides all shortcut tooltips. I have done all things like ms-word but when i clicks elsewhere on mdi child it dont hides tooltips. so i need to handle form mouse click event for all mdi child clicks. And i got stuck here.

I am hesitant to show you this hack, since I am convinced it's not a good thing to rely on, but, since you want it ...

... edit ... version 2:

Here's a "cleaner way" to get the MdiClient:
C#
private MdiClient TheMdiClient;

 private void Form1_Load(object sender, EventArgs e)
 {
     foreach (Control theControl in this.Controls)
     {
         // cannot use the 'is operator here !
         if (theControl as MdiClient != null)
         {
             TheMdiClient = theControl as MdiClient;
             break;
         }
     }

     // error handling
     if (TheMdiClient == null)
     {
         throw new NullReferenceException("There is no MDIClient !");
     }
}
... end edit ... version 2

Version 1:
C#
private void Form1_Load(object sender, EventArgs e)
{
    // whatever other things you do in the Load Event

    Control theMdiClient = this.Controls[this.Controls.Count - 1];

    theMdiClient.Click += theMdiClient_Click;
}

private void theMdiClient_Click(object sender, EventArgs e)
{
    // for testing only
    MessageBox.Show("clicked on the MDI Parent");

    // whatever you want to do on a click on the MdiParent Form
}
The MdiClient is a nameless Control of a special Type which is created automatically by an MDI Win Form App: it is not "visible" at design-time.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Jan-14 9:08am    
It does not look like a reliable approach, if it works at all? Who will guarantee that the MdiControl is the last child (it is supposed to be the only one in nearly all cases, I guess). Did you try it?
—SA
BillWoodruff 6-Jan-14 8:43am    
I test every piece of code I post :)
Sergey Alexandrovich Kryukov 6-Jan-14 9:09am    
Just checking; thank you for answering. Your word is enough for me, but it looks weird that this non-standard window object (with HWND) is found as one of the this.Controls. It looks like Microsoft is being sloppy about it (it should have provided something like GetMdiClient and would not include it in controls).

Then more reliable solution would be traversing through all controls (better in reverse order :-), for each object, taking control as MdiClient and return the first which is not null. It's better to be on the safe side. Still, I never saw where this behavior (that MdiClient is found as one of Controls) was documented. Do you know that?

At this point, I think this issue can be closed. I already voted, OP accepted it... :-)

Thank you,
—SA
BillWoodruff 6-Jan-14 10:50am    
Hi Sergey, I think my edit, with a more "professional" code sample, and your last comment crossed in transit.

Mdi is weird ! But, think of how long it's been around, and what the OS was like around the time it was popular. Whether we like it or not, it's got a lot of people using it, and still has programmers coding to it, and students being assigned to write apps using it.

I think if WinForms had arrived with some Window Docking native facilities, or even a subset of the kind of multi-panel-management facilities found in Weifenluo's open-source DockPanel Suite, we wouldn't be seeing MDI questions here.
Sergey Alexandrovich Kryukov 6-Jan-14 10:55am    
One little problem of this code is that you don't reuse calculation of "as", but you calculate "as" for the same object twice, which is the considerable overhead. Better code should be

//...
MdiClient mdiClient = theControl as mdiClient;
if (mdiClient != null)
return mdiClient; // assuming you make it a function
//...


Cheers,
—SA
You don't really want to - or at least you shouldn't.
Either put the form Click event handler code onto a separate method and call it from both Click event handlers (if you want the Form Click handler within the child form) or create a new Event (with a suitable name) and signal it from both the Form Click and the PictureBox click events (if you want it handled in the MDI Parent form)

You can raise a Click event on a specific control (and a form is a control) if you really, really must - but it's not a good idea:
C#
this.InvokeOnClick(this, new EventArgs());
 
Share this answer
 
Comments
RhishikeshLathe 2-Jan-14 4:43am    
is there any way to capture form mouse click event
e.g. setting Form.KeyPreview=true property which captures all keypress on form and child.
OriginalGriff 2-Jan-14 5:04am    
I don't think so - what are you trying to do exactly?
Not "catch clicks" but why are you trying to do that?
RhishikeshLathe 2-Jan-14 5:36am    
i have implemented functionality of shortcut tooltips, such as ms-word.
In ms-word when we press Alt key it displays shortcut tooltips & when we clicks mouse anywhere on ms-word it hides all shortcut tooltips.
I have done all things like ms-word but when i clicks elsewhere on mdi child it dont hides tooltips. so i need to handle form mouse click event for all mdi child clicks.
And i got stuck here.
I have solved this problem as follws:

AssignClickEvent(Control cParent) function assigns click event to all its child controls recursively.
Call this function in MDI form Load event & in MdiChildActivate event.
MdiChildActivate event gets called on each child load.

C#
private void frmMDI_Load(object sender, EventArgs e) //Form Load event
       {
               //Call function to assign click event to all Child Controls ***
               AssignClickEvent(this);
               //***************************************
       }

 //(Recursive function) ***************
       public void AssignClickEvent(Control cParent)
       {
           foreach (Control c in cParent.Controls)
           {
               if (c.Controls.Count != 0)
                   AssignClickEvent(c);

               c.Click += new EventHandler(c_Click);
           }
       }

       void c_Click(object sender, EventArgs e)
       {
          MessageBox.Show("Mouse click event at MDI");
       }

      //Fires when MDI child is shown ******************
       private void frmMDI_MdiChildActivate(object sender, EventArgs e)//Registered event
       {
           //Call AssignClickEvent function to assign click event to all Child Controls ***************
           if (this.ActiveMdiChild != null)
               AssignClickEvent(this.ActiveMdiChild);
           //***************************************
       }
 
Share this answer
 

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