C#高级:Winform中的自定义窗体输入
目录
一、多样式输入(无封装)
1.代码
2.效果
二、单输入框封装
1.使用
2.封装
3.效果
三、组合框批量输入封装
1.使用
2.封装
3.效果
一、多样式输入(无封装)
1.代码
private async void button1_Click(object sender, EventArgs e)
{
// 在方法中直接创建一个新的窗体
Form inputForm = new Form
{
Text = "输入对话框",
Width = 300,
Height = 300,
MaximizeBox = false,
StartPosition = FormStartPosition.CenterScreen
};
// 创建控件
Label textlabel = new Label { Text = "文本输入:", Width =80,Location = new System.Drawing.Point(20, 15) };
TextBox textBox = new TextBox { Location = new System.Drawing.Point(110, 10), Width = 150 };
CheckBox checkBox = new CheckBox { Text = "是否选中", Location = new System.Drawing.Point(20, 60) };
RadioButton radioButton1 = new RadioButton { Text = "选项 1", Location = new System.Drawing.Point(20, 100), Checked = true };
RadioButton radioButton2 = new RadioButton { Text = "选项 2", Location = new System.Drawing.Point(130, 100) };
ComboBox comboBox = new ComboBox
{
Location = new System.Drawing.Point(20, 140),
Width = 200,
Items = { "选项 A", "选项 B", "选项 C" }
};
comboBox.SelectedIndex = 0; // 默认选中第一项
NumericUpDown numericUpDown = new NumericUpDown
{
Location = new System.Drawing.Point(20, 180),
Width = 200,
Minimum = 0,
Maximum = 100,
Value = 10
};
// 创建确认按钮
Button btnOK = new Button
{
Text = "确定",
Height=30,
Location = new System.Drawing.Point(20, 220)
};
btnOK.Click += (s, args) =>
{
// 获取控件的值
string textBoxInput = textBox.Text;
bool checkBoxChecked = checkBox.Checked;
string radioButtonSelection = radioButton1.Checked ? "选项 1" : "选项 2";
string comboBoxSelection = comboBox.Text;
decimal numericValue = numericUpDown.Value;
// 显示结果
MessageBox.Show($"你输入的内容:\n" +
$"文本框: {textBoxInput}\n" +
$"复选框: {checkBoxChecked}\n" +
$"单选框: {radioButtonSelection}\n" +
$"下拉框: {comboBoxSelection}\n" +
$"数字框: {numericValue}");
// 关闭窗体
inputForm.Close();
};
// 创建取消按钮
Button btnCancel = new Button
{
Text = "取消",
Height = 30,
Location = new System.Drawing.Point(120, 220)
};
btnCancel.Click += (s, args) =>
{
// 取消操作,关闭窗体
inputForm.Close();
};
// 将控件添加到窗体
inputForm.Controls.Add(textlabel);
inputForm.Controls.Add(textBox);
inputForm.Controls.Add(checkBox);
inputForm.Controls.Add(radioButton1);
inputForm.Controls.Add(radioButton2);
inputForm.Controls.Add(comboBox);
inputForm.Controls.Add(numericUpDown);
inputForm.Controls.Add(btnOK);
inputForm.Controls.Add(btnCancel);
// 显示窗体
inputForm.ShowDialog();
}
2.效果
二、单输入框封装
1.使用
异步方法返回结果需要通过.Result获取
//点击触发自定义弹窗输入
private async void button1_Click(object sender, EventArgs e)
{
var result = GetTextInput("请输入您的尊姓大名").Result;
}
2.封装
//新建自定义窗体,输入提示语,是否采用文本框(false=采用数字框)
private async Task<string> GetTextInput(string laebl,bool isUseTextBox=true)
{
//多少个输入框:
var InputLength = laebl.Length;
int labelwidth = InputLength * 20 + 5;
// 在方法中直接创建一个新的窗体
Form inputForm = new Form
{
Text = "请输入",
Width = labelwidth+300,
Height = 180,
MaximizeBox = false,
StartPosition = FormStartPosition.CenterScreen
};
// 创建控件
Label label = new Label { Text = laebl + ":", Width = labelwidth, Location = new System.Drawing.Point(20, 23), BackColor = Color.Transparent };
TextBox textBox = new TextBox();
NumericUpDown numericUpDown = new NumericUpDown();
inputForm.Controls.Add(label);
int input_x = labelwidth + 20;
if (isUseTextBox)
{
textBox = new TextBox { Location = new System.Drawing.Point(input_x, 20), Width = 200 };
inputForm.Controls.Add(textBox);
}
else
{
numericUpDown = new NumericUpDown
{
Location = new System.Drawing.Point(input_x, 20),
Width = 200,
Minimum = 0,
Maximum = 100000000000,
Value = 1
};
inputForm.Controls.Add(numericUpDown);
}
// 创建确认按钮
Button btnOK = new Button
{
Text = "确定",
Height = 30,
Location = new System.Drawing.Point(input_x, 70)
};
string result = "";
btnOK.Click += (s, args) =>
{
//结果赋值
result = isUseTextBox ? textBox.Text : numericUpDown.Value.ToString();
// 关闭窗体
inputForm.Close();
};
// 创建取消按钮
Button btnCancel = new Button
{
Text = "取消",
Height = 30,
Location = new System.Drawing.Point(input_x+100, 70)
};
btnCancel.Click += (s, args) =>
{
// 取消操作,关闭窗体
inputForm.Close();
};
// 将btn控件添加到窗体
inputForm.Controls.Add(btnOK);
inputForm.Controls.Add(btnCancel);
// 显示窗体
inputForm.ShowDialog();
return result;
}
3.效果
三、组合框批量输入封装
1.使用
异步方法返回结果需要通过.Result获取
//点击触发自定义弹窗输入
private async void button1_Click(object sender, EventArgs e)
{
List<Dictionary<string, List<string>>> list = new List<Dictionary<string, List<string>>>
{
new Dictionary<string, List<string>> { { "选择一", new List<string> { "1", "2", "3" } } },
new Dictionary<string, List<string>> { { "选择二", new List<string> { "4", "5", "6" } } },
new Dictionary<string, List<string>> { { "选择三", new List<string> { "7", "8", "9" } } }
};
var result = GetComboboxInput(list).Result;
}
2.封装
//新建自定义窗体,提示语:输入框选项
private async Task<List<string>> GetComboboxInput(List<Dictionary<string, List<string>>> list)
{
//多少个输入框:
var InputCount = list.Count;
var InputLabels = list.SelectMany(x => x.Keys).ToList();
var InputValues = list.SelectMany(x => x.Values).ToList();
int addheight = 50;
// 在方法中直接创建一个新的窗体
Form inputForm = new Form
{
Text = "请输入",
Width = 350,
Height = InputCount * addheight + 130,
MaximizeBox = false,
StartPosition = FormStartPosition.CenterScreen
};
// 创建控件
List<Label> LaeblList = new List<Label>();
List<ComboBox> ComboBoxList = new List<ComboBox>();
int y = 20;
for (int i = 0; i < InputCount; i++)
{
Label en_label = new Label { Text = InputLabels[i] + ":", Width = 80, Location = new System.Drawing.Point(20, y+3),BackColor=Color.Transparent };
ComboBox en_com = new ComboBox { DataSource = InputValues[i], Width = 200, Location = new System.Drawing.Point(100, y) };
LaeblList.Add(en_label);
ComboBoxList.Add(en_com);
inputForm.Controls.Add(en_label);
inputForm.Controls.Add(en_com);
y += addheight;
}
// 创建确认按钮
int btn_y = InputCount * addheight + 20;
Button btnOK = new Button
{
Text = "确定",
Height = 30,
Location = new System.Drawing.Point(80, btn_y)
};
List<string> result = new List<string>();
btnOK.Click += (s, args) =>
{
//结果赋值
result = ComboBoxList.Select(x => x.Text).ToList();
// 关闭窗体
inputForm.Close();
};
// 创建取消按钮
Button btnCancel = new Button
{
Text = "取消",
Height = 30,
Location = new System.Drawing.Point(180, btn_y)
};
btnCancel.Click += (s, args) =>
{
// 取消操作,关闭窗体
inputForm.Close();
};
// 将btn控件添加到窗体
inputForm.Controls.Add(btnOK);
inputForm.Controls.Add(btnCancel);
// 显示窗体
inputForm.ShowDialog();
return result;
}
如果输入的label太长,请手动调节窗体宽度、combobox的x坐标