System.Windows.Forms.ToolStripDropDown.OnLayout C# (CSharp) Method

OnLayout() protected method

protected OnLayout ( System.Windows.Forms.LayoutEventArgs e ) : void
e System.Windows.Forms.LayoutEventArgs
return void
		protected override void OnLayout (LayoutEventArgs e)
		{
			// Find the widest menu item, so we know how wide to make our dropdown
			int widest = 0;

			foreach (ToolStripItem tsi in this.Items) {
				if (!tsi.Available) 
					continue;
					
				tsi.SetPlacement (ToolStripItemPlacement.Main);
				
				widest = Math.Max (widest, tsi.GetPreferredSize (Size.Empty).Width + tsi.Margin.Horizontal);
			}
			
			// Add any padding our dropdown has set
			widest += this.Padding.Horizontal;
			
			int x = this.Padding.Left;
			int y = this.Padding.Top;

			foreach (ToolStripItem tsi in this.Items) {
				if (!tsi.Available)
					continue;

				y += tsi.Margin.Top;

				int height = 0;

				Size preferred_size = tsi.GetPreferredSize (Size.Empty);

				if (preferred_size.Height > 22)
					height = preferred_size.Height;
				else if (tsi is ToolStripSeparator)
					height = 7;
				else
					height = 22;

				tsi.SetBounds (new Rectangle (x, y, preferred_size.Width, height));
				y += height + tsi.Margin.Bottom;
			}

			this.Size = new Size (widest, y + this.Padding.Bottom);
			this.SetDisplayedItems ();
			this.OnLayoutCompleted (EventArgs.Empty);
			this.Invalidate ();
		}