CadParamPluging/UI/TextInputForm.cs

85 lines
2.5 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace CadParamPluging.UI
{
public class TextInputForm : Form
{
private readonly TextBox _txt;
public string Value => _txt.Text?.Trim();
public TextInputForm(string title, string label, string initialValue)
{
Text = title;
Size = new Size(360, 150);
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
var layout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
Padding = new Padding(10),
RowCount = 3,
ColumnCount = 1
};
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
var lbl = new Label
{
Text = label,
AutoSize = true,
Dock = DockStyle.Fill
};
_txt = new TextBox
{
Dock = DockStyle.Fill,
Text = initialValue ?? string.Empty
};
var buttons = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
FlowDirection = FlowDirection.RightToLeft,
AutoSize = true
};
var btnCancel = new Button { Text = "取消", DialogResult = DialogResult.Cancel };
var btnOk = new Button { Text = "确定", DialogResult = DialogResult.OK };
buttons.Controls.Add(btnCancel);
buttons.Controls.Add(btnOk);
layout.Controls.Add(lbl, 0, 0);
layout.Controls.Add(_txt, 0, 1);
layout.Controls.Add(buttons, 0, 2);
Controls.Add(layout);
AcceptButton = btnOk;
CancelButton = btnCancel;
}
public static bool TryGetValue(IWin32Window owner, string title, string label, string initialValue, out string value)
{
using (var form = new TextInputForm(title, label, initialValue))
{
var result = form.ShowDialog(owner);
if (result == DialogResult.OK)
{
value = form.Value;
return true;
}
}
value = null;
return false;
}
}
}