[Mono-winforms-list] ToolStripDropDown + UserControl

Alex Shulgin alexander.shulgin at yessoftware.com
Tue Feb 24 11:10:07 EST 2009


Hi,

I'm trying to add some custom control to ToolStripDropDown using 
ToolStripControlHost.

This works fine with .NET, but with Mono (2.2) the control is always 
resized to 22 pixels tall...  Too bad I can't find a workaround for a 
few days now.

Any help & suggestions are much appreciated. :)

Below is sample code to demonstrate the problem.

--
Thanks.
Alex

--8<----8<----8<----8<----8<----8<----8<----8<--

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace winforms_toolstrip
{
     static class Program
     {
         [STAThread]
         static void Main()
         {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new Form1());
         }
     }

     public class PopupWindow : ToolStripDropDown
     {
         private Control _content;
         private ToolStripControlHost _host;

         public PopupWindow(Control content)
         {
             this.AutoSize = false;
             this.DoubleBuffered = true;
             this.ResizeRedraw = true;

             this._content = content;
             this._host = new ToolStripControlHost(content);

             this.MinimumSize = content.MinimumSize;
             this.MaximumSize = content.GetPreferredSize(Size.Empty);
             this.Size = this.MaximumSize;
             content.Location = Point.Empty;

             this.Items.Add(this._host);
         }
     }

     public class Form1 : Form
     {
         public Form1()
         {
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(292, 266);
             this.Name = "Form1";
             this.Text = "Form1";

             this.myControl = new MyControl();

             this.MouseClick += new MouseEventHandler(form1_MouseClick);
         }

         internal void form1_MouseClick(object sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
             {
                 PopupWindow popup = new PopupWindow(myControl);
                 popup.Show(this.Location.X + e.X, this.Location.Y + e.Y);
             }
         }

         internal class MyControl : UserControl
         {
             public MyControl()
             {
                 Size = new Size(50, 50);
             }

             protected override void OnResize(EventArgs e)
             {
                 Console.WriteLine("MyControl.OnResize: Bounds={0}", 
Bounds);
             }

             protected override void OnPaint(PaintEventArgs e)
             {
                 Console.WriteLine("MyControl.OnPaint: Bounds={0}", Bounds);
                 Graphics g = e.Graphics;
                 using (Brush b = new SolidBrush(Color.Black))
                 {
                     g.FillRectangle(b, Bounds);
                 }
             }

             public override Size GetPreferredSize(Size constr)
             {
                 Console.WriteLine("MyControl.GetPreferredSize");
                 return new Size(50, 50);
             }

             public override Size MinimumSize
             {
                 get
                 {
                     return GetPreferredSize(Size.Empty);
                 }
                 set
                 {
                     base.MinimumSize = value;
                 }
             }
         };

         internal MyControl myControl;
     }
}


More information about the Mono-winforms-list mailing list