Solved

Assigning a ContextMenu to a UserControl

Posted on 2004-03-25
8
828 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now