Hi there, I've got a problem while trying to build a custom popup using ToolStripDropDown and ToolStripControlHost. For some reason, when the dropdown is displayed, it is repainting the background of the entire parent form, once the popup is shown and focus is set to the control within the popup.
Simply showing the popup does not cause the repaint. But as soon as you set focus to the contorl within the ToolStripControlHost, then the parent form loses activatino and completely redraws itself. This in turn means that all custom drawn controls on the form must be invalidated and repainted as well, otherwise they disappear.
As a result, every time the user deploys the dropdown, the entire form has to flash and repaint. That or else all of the other controls disappear.
Below is a small example that demonstrates the problem. If you left-click anywhere in the form, it will deploy the popup AND set focus to the list box, which as you can see issues an immediate repaint on the parent. If you just right-click, it won't set focus, and thus no repaint. For comparison, the middle-click will show a simple plain form dialog. Note that these last two cases do not cause parent to immediately do a full repaint.
Is this normal expected behavior? Why would the entire parent form be invalidated? And most importantly, how can I prevent this so that the popup does not cause the entire parent to have to be repainted?
Thanks,
Ryan
using System; using System.Drawing; using System.Windows.Forms; namespace Amp.Launcher { class TestForm2 : Form { private ToolStripDropDown _popup; private ListBox _list; public TestForm2() { _list = new ListBox(); _list.Size = new Size(100, 100); ToolStripControlHost host = new ToolStripControlHost(_list); host.AutoSize = false; _popup = new ToolStripDropDown(); _popup.AutoClose = true; _popup.Items.Add(host); this.MouseClick += new MouseEventHandler(_popup_MouseClick); } private void _popup_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { _popup.Show(PointToScreen(e.Location)); _list.Focus(); // BUG: setting focus will *immediately* REPAINT entire parent } else if (e.Button == MouseButtons.Right) { _popup.Show(PointToScreen(e.Location)); } else if (e.Button == MouseButtons.Middle) { Form form = new Form(); ListBox list = new ListBox(); list.Size = new Size(100, 100); list.Location = new Point(50, 50); form.Controls.Add(list); form.Show(this); list.Focus(); // NO BUG: setting focus causes no *immediate* repaint } } protected override void OnPaint(PaintEventArgs e) { Random random = new Random(); Color[] colors = new Color[10] {Color.Red, Color.Green, Color.Blue, Color.Pink, Color.Yellow, Color.Orange, Color.Black, Color.Brown, Color.Purple, Color.Cyan }; using (SolidBrush brush = new SolidBrush(colors[random.Next(10)])) { e.Graphics.FillRectangle(brush, e.ClipRectangle); } } [STAThread] static void Main() { Application.Run(new TestForm2()); } } }Edit: clarified the post and added Form popup behavior to code so you can easily see this issue doesn't exist with form being shown, but does with popups oonce they get focus.