Solved

Assigning a ContextMenu to a UserControl

Posted on 2004-03-25
8
836 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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