public InputBox(string text, string caption, string defaultValue) { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); this.Text = caption; acceptButton.Text = "确定"; cancelButton.Text = "取消"; Size size; using (Graphics g = this.CreateGraphics()) { Rectangle screen = Screen.PrimaryScreen.WorkingArea; SizeF sizeF = g.MeasureString(text, label.Font, screen.Width - 20); size = sizeF.ToSize(); size.Width += 4; } if (size.Width < 200) size.Width = 200; Size clientSize = this.ClientSize; clientSize.Width += size.Width - label.Width; clientSize.Height += size.Height - label.Height; this.ClientSize = clientSize; label.Text = text; textBox.Text = defaultValue; this.DialogResult = DialogResult.Cancel; RightToLeftConverter.ConvertRecursive(this); }
public CustomDialog(string caption, string message, int acceptButton, int cancelButton, string[] buttonLabels) { this.SuspendLayout(); InitializeComponent(); this.Icon = null; this.acceptButton = acceptButton; this.cancelButton = cancelButton; this.Text = caption; using (Graphics g = this.CreateGraphics()) { Rectangle screen = Screen.PrimaryScreen.WorkingArea; ////SizeF size = g.MeasureString(message, label.Font, screen.Width - 20); ////Size clientSize = size.ToSize(); int maxWidth = screen.Width / 3; SizeF size = g.MeasureString(message, label.Font, maxWidth - 20); Size clientSize = size.ToSize(); if (clientSize.Height < 120) { clientSize.Height = 120; } Button[] buttons = new Button[buttonLabels.Length]; int[] positions = new int[buttonLabels.Length]; int pos = 0; for (int i = 0; i < buttons.Length; i++) { Button newButton = new Button(); newButton.FlatStyle = FlatStyle.System; newButton.Tag = i; string buttonLabel = buttonLabels[i]; newButton.Text = buttonLabel; newButton.Click += new EventHandler(ButtonClick); SizeF buttonSize = g.MeasureString(buttonLabel, newButton.Font); newButton.Width = Math.Max(newButton.Width, ((int)Math.Ceiling(buttonSize.Width / 8.0) + 1) * 8); positions[i] = pos; buttons[i] = newButton; pos += newButton.Width + 4; } if (acceptButton >= 0) { AcceptButton = buttons[acceptButton]; } if (cancelButton >= 0) { CancelButton = buttons[cancelButton]; } pos += 4; // add space before first button // (we don't start with pos=4 because this space doesn't belong to the button panel) if (pos > clientSize.Width) { clientSize.Width = pos; } clientSize.Height += panel.Height + 6; this.ClientSize = clientSize; int start = (clientSize.Width - pos) / 2; for (int i = 0; i < buttons.Length; i++) { buttons[i].Location = new Point(start + positions[i], 4); } panel.Controls.AddRange(buttons); } label.Text = message; RightToLeftConverter.ConvertRecursive(this); this.ResumeLayout(false); }