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
Solved

Assigning a ContextMenu to a UserControl

Posted on 2004-03-25
8
843 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Suggested Solutions

Title # Comments Views Activity
Wav problem 4 28
Hey!! 5 37
JQuery Detecting page post back 6 10
How can I do Unit Testing with Session ? 1 10
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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