473,725 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nasty bug with OwnerDrawn context menus on a ComboBox

Have a look at the code below. It's a really simple example where I
have added two controls to a form, a textbox and a combo box. Both add
owner drawn context menus to the controls, but the combobox will NOT
draw custom drawn context menu item. I have tried EVERYTHING (deriving
from MenuItem and even tried calling TrackPopupMenuE x directly), but
it just won't work!

I am currently reflecting over the combobox source code (oh joy!), but
if anyone can help me, I will BE SOOOOO VERY GRATEFUL!!!!

thanks
andrew
using System;
using System.Drawing;
using System.Diagnost ics;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace ComboContextBug
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private ComboTest comboBox1;
private TextBoxTest textBox1;
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.comboBox1 = new ComboContextBug .ComboTest();
this.textBox1 = new ComboContextBug .TextBoxTest();
this.label1 = new System.Windows. Forms.Label();
this.label2 = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// comboBox1
//
this.comboBox1. Location = new System.Drawing. Point(180, 32);
this.comboBox1. Name = "comboBox1" ;
this.comboBox1. Size = new System.Drawing. Size(121, 21);
this.comboBox1. TabIndex = 0;
this.comboBox1. Text = "comboBox1" ;
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(180, 98);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(121, 20);
this.textBox1.T abIndex = 1;
this.textBox1.T ext = "textBox1";
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(8, 20);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(168, 52);
this.label1.Tab Index = 2;
this.label1.Tex t = "Right click this combo and you will see that
the OwnerDrawn item does NOT appear!" +
"";
//
// label2
//
this.label2.Loc ation = new System.Drawing. Point(8, 86);
this.label2.Nam e = "label2";
this.label2.Siz e = new System.Drawing. Size(168, 52);
this.label2.Tab Index = 3;
this.label2.Tex t = "Right click this textbox and you will see that
the OwnerDrawn item DOES appear!";
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(336, 157);
this.Controls.A dd(this.label2) ;
this.Controls.A dd(this.label1) ;
this.Controls.A dd(this.textBox 1);
this.Controls.A dd(this.comboBo x1);
this.Name = "Form1";
this.Text = "Combo Context Menu Bug";
this.ResumeLayo ut(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
}
public class ComboTest: ComboBox
{
public ComboTest()
{
base.ContextMen u = new ContextMenu();

MenuItem mnuOwnerDraw = new MenuItem();
mnuOwnerDraw.Te xt = "This text WILL NOT appear on a ComboBox!";
mnuOwnerDraw.Ow nerDraw = true;
mnuOwnerDraw.Dr awItem +=new
DrawItemEventHa ndler(mnuOwnerD raw_DrawItem);
mnuOwnerDraw.Me asureItem +=new
MeasureItemEven tHandler(mnuOwn erDraw_MeasureI tem);
base.ContextMen u.MenuItems.Add (0, mnuOwnerDraw);

MenuItem mnuNormalDraw = new MenuItem();
mnuNormalDraw.T ext = "This text WILL appear!";
mnuNormalDraw.O wnerDraw = false;
base.ContextMen u.MenuItems.Add (0, mnuNormalDraw);
}

private void mnuOwnerDraw_Dr awItem(object sender, DrawItemEventAr gs
e)
{
MenuItem menu = sender as MenuItem;
e.Graphics.Draw String( menu.Text, this.Font,
SystemBrushes.C ontrolText, e.Bounds.X + 15, e.Bounds.Y );
}

private void mnuOwnerDraw_Me asureItem(objec t sender,
MeasureItemEven tArgs e)
{
MenuItem menu = sender as MenuItem;
string text = menu.Text;
e.ItemWidth = (int)(e.Graphic s.MeasureString (text,
this.Font).Widt h) + 3;
e.ItemHeight = (int)(e.Graphic s.MeasureString (text,
this.Font).Heig ht) + 2;
}
}

public class TextBoxTest: TextBox
{
public TextBoxTest()
{
base.ContextMen u = new ContextMenu();

MenuItem mnuOwnerDraw = new MenuItem();
mnuOwnerDraw.Te xt = "This text WILL appear on a Textbox!";
mnuOwnerDraw.Ow nerDraw = true;
mnuOwnerDraw.Dr awItem +=new
DrawItemEventHa ndler(mnuOwnerD raw_DrawItem);
mnuOwnerDraw.Me asureItem +=new
MeasureItemEven tHandler(mnuOwn erDraw_MeasureI tem);
base.ContextMen u.MenuItems.Add (0, mnuOwnerDraw);

MenuItem mnuNormalDraw = new MenuItem();
mnuNormalDraw.T ext = "This text WILL appear!";
mnuNormalDraw.O wnerDraw = false;
base.ContextMen u.MenuItems.Add (0, mnuNormalDraw);
}

private void mnuOwnerDraw_Dr awItem(object sender, DrawItemEventAr gs
e)
{
MenuItem menu = sender as MenuItem;
e.Graphics.Draw String( menu.Text, this.Font,
SystemBrushes.C ontrolText, e.Bounds.X + 15, e.Bounds.Y );
}

private void mnuOwnerDraw_Me asureItem(objec t sender,
MeasureItemEven tArgs e)
{
MenuItem menu = sender as MenuItem;
string text = menu.Text;
e.ItemWidth = (int)(e.Graphic s.MeasureString (text,
this.Font).Widt h) + 3;
e.ItemHeight = (int)(e.Graphic s.MeasureString (text,
this.Font).Heig ht) + 2;
}
}
}
Nov 17 '05 #1
0 1914

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4777
by: Eddie de Bear | last post by:
Hi, A project I am working on has a requirement for dynamic menus. For the most part this works really well. The menus I'm creating a based on files and directories, so naturally the menu creation takes some time. The approach I took was to override the OnSelect method of the MenuItem class, which had code to populate the
1
1548
by: Doug Bell | last post by:
Hi, If I modify my Registry adding a new key HKEY_CLASSES_ROOT\*\shell\Use My App\command with a (Default) Value of C:\Program Files\Doug\DotNetApps\MyApp.exe "%1" then right clicking on files in the Explorer Windows opens the context menu with the option "Use My App" and selecting this runs My App with the selected file name. Just as I wanted. The problem I now have is that double clicking a Visual Studio file (.sln) also starts My...
1
3577
by: Abhi | last post by:
I've created a class "MRP123MenuItem" derived from inbuilt MenuItem class to give customized look and feel to the user. I've created context menus for different controls using mine MRP123MenuItem's and it all works fine except for ComboBox control. It only displays a strip of context menus without any content. I'm unable to understand why it is happening though it works fine when used along with any other control. Please help me out.
4
9124
by: Beeeeeeeeeeeeves | last post by:
I have an ownerdrawn combo box which I am drawing with an image and some text, this is all working beautifully apart from the difference in the Brush I have to draw the background and the text with if the item is the currently selected one. I have selected a blue (SystemColors.Highlight) background brush and white (SystemColors.HighlightText) text brush if the item is the highlighted one, but when I do that instead of functioning like a...
1
1636
by: Beeeeeeeeeeeeves | last post by:
I have an ownerdrawn combo box which I am drawing with an image and some text, this is all working beautifully apart from the difference in the Brush I have to draw the background and the text with if the item is the currently selected one. I have selected a blue (SystemColors.Highlight) background brush and white (SystemColors.HighlightText) text brush if the item is the highlighted one, but when I do that instead of functioning like a...
1
1670
by: mnsindhu74 | last post by:
hi I am creating an ownerdrawn combo box. I will need to display icons/images in the edit portion of the combobox. I retreived the handles of Edit and List portions with ComboBoxInfo structure. Then I created a class inherited from NativeWindow. This class is assigned the handle of EditBox of combo so that I can handle the WM_PAINT event only for the Edit portion. So far everything good. But the problem I am facing is that The EditBox...
4
1715
by: ssg31415926 | last post by:
I've got a ListView which can show many different types of object. I need to display a ContextMenu whose MenuItems depend on the object type. I was planning to pre-create the ContextMenus when the app is started and then assign them when the object is clicked on. The first few items in the menus will all be the same (Large Icons, Small Icons, etc.). I create all of those objects, then add them to the first context menu and all is...
4
1282
by: Andre Nogueira | last post by:
Hi there. I have created a Windows Control Library project within my solution (using VB.Net 2003) and created a control which inherits the MenuItem class but takes cares of the drawing of the menu. So far so good. It takes care of all the drawing of the menuitems if they are part of the form's mainmenu or if they are part of a context menu of an item of the form (Listview, etc). However, if the context menu is assigned to the Notify Icon...
3
1267
by: André Nogueira | last post by:
Hi there! I have successfully create my own ownerdrawn menus. However, I decided to add some code for the menus to detect if their parent is a MainMenu object and, if it is, as it is the menuitem beeing displayed in the form's menu bar, it draws it differently. So far so good. But there are two thing I can't get around. 1 - How can I draw a custom menu when the mouse is over the menu item but the user hasn't clicked it, just like in...
0
8872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9392
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9246
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9162
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8069
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4773
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3211
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.