public static ListBox AddListBox(_Choices choices, Font font, int top, int left, AnchorStyles anchors, Panel panel, int tabidx) { int gap = 4; int maxWidth = 0; ListBoxPP lstBox = new ListBoxPP(); lstBox.Anchor = anchors; lstBox.Font = font; lstBox.TabIndex = tabidx; lstBox.BorderStyle = BorderStyle.FixedSingle; lstBox.Name = "listBox" + tabidx.ToString(); foreach (_Choice choice in choices) { string txt = choice.Text; if (txt != "") { lstBox.Items.Add(txt); int itemWidth = Tools.GetLabelWidth(txt, font); if (itemWidth > maxWidth) { maxWidth = itemWidth; } } } if (lstBox.Items.Count == 0) { return(null); } lstBox.Height = lstBox.PreferredHeight + gap; int prefWidth = maxWidth + gap + (lstBox.Width - lstBox.ClientSize.Width); if (prefWidth < panel.Width * 0.8) { if (prefWidth < panel.Width * 0.4) { prefWidth = (int)(panel.Width * 0.4); } lstBox.Width = prefWidth; } else { lstBox.Width = (int)(panel.Width * 0.8); } lstBox.Top = top; lstBox.Left = left; panel.Controls.Add(lstBox); lstBox.SelectedIndexChanged += new System.EventHandler(Preview_Event); return(lstBox); }
public static ListBoxPP AddListBox(_Choices choices, Font font, int top, int left, Panel panel, int maxHgt) { int gap = 4; int maxWidth = 0; ListBoxPP lstBox = new ListBoxPP(); lstBox.Font = font; lstBox.ForeColor = Color.Blue; lstBox.BackColor = Color.AliceBlue; lstBox.EvenItemColor = Color.AliceBlue; lstBox.ShowLines = false; lstBox.ItemHeight = (int)(font.Size * 2.25); foreach (_Choice choice in choices) { string txt = choice.Text; if (txt != "") { lstBox.Items.Add(txt); int itemWidth = Tools.GetLabelWidth(txt, font); if (itemWidth > maxWidth) { maxWidth = itemWidth; } } } if (lstBox.Items.Count == 0) { return(null); } int prefWidth = maxWidth + gap * 2; lstBox.Height = lstBox.ItemHeight * lstBox.Items.Count; if (lstBox.Height > maxHgt * 3 / 4) { lstBox.Height = (int)Math.Round(Convert.ToDouble(maxHgt / 2 / lstBox.ItemHeight)) * lstBox.ItemHeight; lstBox.ShowScrollbar = true; prefWidth += 14; } if (prefWidth < panel.Width * 0.9) // Make sure it's not too wide { if (prefWidth < panel.Width * 0.4) // Make sure it's not too narrow { prefWidth = (int)(panel.Width * 0.4); } lstBox.Width = prefWidth; } else { lstBox.Width = (int)(panel.Width * 0.9); } lstBox.Top = top; lstBox.Left = left; panel.Controls.Add(lstBox); lstBox.SelectedIndexChanged += new System.EventHandler(PanelChoices_Event); return(lstBox); }