Solved

Assigning a ContextMenu to a UserControl

Posted on 2004-03-25
8
849 Views
Last Modified: 2006-11-17
I have a user control made up of a textbox, listview, scrollbar and a picturebox. It is basically a combo box but with extra functionality. The problem I have is when I assign a contextmenu to my usercontrol the only thing that will show the context menu is the picturebox.

I can assign it to individual items in the usercontrol but when the user clicks a menu item on the contextmenu I want to know which usercontrol the click came from and the sourcecontrol property is actually the indivdual item I assigned it to.

So what I want to know is how to assign a context menu to a usercontrol as a whole.

Regards

Simion
0
Comment
Question by:simion_tishler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 10682850
When you are designing your user control, create a ContextMenu called userControlMenu.  Inside your constructor, place the code, this.ContextMenu = this.userControlMenu.

The problem is, that some objects (like TextBoxes) respond to a right mouse click with their "own" menu.  If you want to override EVERY control on your form (user control) you will need to use some code as follows:

  foreach(Control control in this.Controls)
    control.ContextMenu = this.userControlMenu;

but I realize that is not what you would like to do.  I would suggest the first method.
0
 
LVL 1

Author Comment

by:simion_tishler
ID: 10683076
Thanks for your prompt reply. But this is not the solution I'm looking for because if I implemented your code I would have to create the menu items for the context menu when I created the user control which is not what I want. I want to create a context menu separately and assign it to the user control. That way my usercontrol has the kind of functionally that all controls have.
And the other reason I would like to do it this way is because not all of my user controls are used for the same thing and need different menu items.
Of course I could assign menu items to the context menu of the user control. Which would solve the problem but this is not really what I want either. Hmm but your comment was a good one. And I might give it to you if there is nothing else I can do.

Thanks

Simion
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10684006
Simion,

  Why can't you simply put a ContextMenu and your user control on a form, and assign the ContextMenu to the specific user control?  Maybe I didn't understand your problem...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:simion_tishler
ID: 10684073
Hi Eternal

Because it seems that the only control the reponses to that method is the picture box. I tried assigning it to the textbox once it had been passed in but the sourcecontrol of the contextmenu then reported the sourcecontrol as the textbox inside the usercontrol.

I need some way to override the textboxes contextmenu. Like you said it has it's own context menu for cutting and pasting. Heres the code as it stands inside my usercontrol. I'm using a property instead of building it into the contructor.

public override ContextMenu ContextMenu
{
      get
      {
            return base.ContextMenu;
      }
      set
      {
            base.ContextMenu = value;
            textBox1.ContextMenu =value;
      }
}

and I tried the code below and only the picturebox picked it up

public override ContextMenu ContextMenu
{
      get
      {
            return base.ContextMenu;
      }
      set
      {
            base.ContextMenu = value;
      }
}

I hope that I'm not confused by your response and wasting your time

Thanks

Simion
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10684497
I understand what the problem is now.  You could continue using your first example of the ContextMenu property override, and change the way that the control is determined. You are probably using ContextMenu.SourceControl to determine which control was clicked, right?  If so, change your code to something like this:

### C#.NET ###

  UserControl1 userControl;

  System.Windows.Forms.Control sourceControl;
  sourceControl = this.contextMenu1.SourceControl;
  while(sourceControl.GetType()!=typeof(UserControl1))
    sourceControl = sourceControl.Parent;

  userControl = sourceControl as UserControl1;

###

  // userControl is now the UserControl that opened the context menu.  If the control on your main form is not a UserControl1, userControl will be null.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10684501
Actually, if the control on your main form is not a UserControl1, this code will self-destruct.  I can post an updated version, but first would the process be suitable?
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 10684507
Wait - it was a quick fix!

This will do it perfectly:

### C#.NET ###

  UserControl1 userControl;

  System.Windows.Forms.Control sourceControl;
  sourceControl = this.contextMenu1.SourceControl;
  while(sourceControl.Parent!=this)
    sourceControl = sourceControl.Parent;

  userControl = sourceControl as UserControl1;

###
0
 
LVL 1

Author Comment

by:simion_tishler
ID: 10684604
You were close but this is the answer that works with my code. But you are right also. Heres the code

 if(String.Compare(StateComboMenu.SourceControl.Parent.Name,"CitTypeCbo")==0)
      CitTypeCbo.Text = "";

But really this is not an elegant way to do it because we should be able to use sourcecontrol without this parent stuff. My thoughts are that a usercontrol should act as a whole in situations like this and if anyone was to try and use my usercontrol they what not be expected to know that the funtionally was not all it seemed.

I will give this to you if no one give me a better answer. I'm going home now work is finished so I won't be providing a quick response if you comment.

Thanks eternal_21
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question