有这样一个需求,类似从一个Excel表格筛选符合条件的行,如果列字段被标记为手动输入,则需要弹出窗口让工人输入,关键是手动输入的列字段不是确定数量的,可能是一个,也有可能是十个,也有可能一百个,不可能每种情况都创建一个对应输入窗口,只能依据参数个数,动态添加控件。
1. 在当前窗口动态添加2个文本控件
效果如下:
实现代码:
//添加第一个文本框
TextBox tb1 = new TextBox();
tb1.Name = "myTextBox1";
tb1.Text = "第一个文本框";
tb1.Width = 150;
tb1.Height = 50;
tb1.HorizontalAlignment = HorizontalAlignment.Left;
tb1.VerticalAlignment = VerticalAlignment.Top;
tb1.Margin = new Thickness(10, 10, 0, 0);
gdTest.Children.Add(tb1);
//添加第二个文本框
TextBox tb2 = new TextBox();
tb2.Name = "myTextBox2";
tb2.Text = "第二个文本框";
tb2.Width = 150;
tb2.Height = 50;
tb2.HorizontalAlignment = HorizontalAlignment.Left;
tb2.VerticalAlignment = VerticalAlignment.Top;
tb2.Margin = new Thickness(10, 100, 0, 0);
gdTest.Children.Add(tb2);
2. 访问动态添加的文本框的内容
//访问添加的全部文本框
foreach (var c in gdTest.Children)
{
if (c is TextBox)
{
TextBox tb = (TextBox)c;
if (tb.Name == “myTextBox1”)
{
MessageBox.Show(KaTeX parse error: Expected 'EOF', got '}' at position 30: …xt}"); }̲ else i…“第二个文本框:{tb.Text}”);
}
}
}
输出:
3. 获取指定文本框的内容
假如需要第2个文本框的内容,只需要在上步骤中限定文本框名称即可。
foreach (var c in gdTest.Children)
{
if (c is TextBox)
{
TextBox tb = (TextBox)c;
if (tb.Name == "myTextBox2")
{
MessageBox.Show(tb.Text);
}
}
}
效果:
4. 创建一个输入窗口,动态创建参数名和参数值
关闭窗口回传输入的值:
参数输入窗口:
public partial class SubWindow : Window
{
public SubWindow()
{
InitializeComponent();
}
private List<string> parName = new List<string>();
public SubWindow(int parameCount, List<string> parameName)
{
InitializeComponent();
parName = parameName;
for (int i = 0; i < parameCount; i++)
{
Label lbl = new Label();
lbl.Content = parameName[i];
lbl.FontSize = 14;
lbl.HorizontalAlignment = HorizontalAlignment.Left;
lbl.VerticalContentAlignment = VerticalAlignment.Top;
lbl.Margin = new Thickness(20, 8 + i * 30, 0, 0);
gd.Children.Add(lbl);
TextBox tb1 = new TextBox();
tb1.Name = parameName[i];
tb1.Text = $"第{i}个文本框";
tb1.Width = 300;
tb1.Height = 25;
tb1.KeyDown += Tb1_KeyDown;
tb1.HorizontalAlignment = HorizontalAlignment.Left;
tb1.VerticalAlignment = VerticalAlignment.Top;
tb1.Margin = new Thickness(130, 10 + i * 30, 0, 0);
gd.Children.Add(tb1);
tb1.Focus();
}
}
private void Tb1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
string next = "";
TextBox tb = sender as TextBox;
int index = parName.IndexOf(tb.Name);
if (index < parName.Count - 1)
{
next = parName[index + 1];
}
else
{
this.Close();
}
foreach (var t in gd.Children)
{
if (t is TextBox)
{
TextBox textbox = (TextBox)t;
if (textbox.Name == next)
{
textbox.Focus();
}
}
}
}
}
}
使用方式:
List<string> names = new List<string> { "SAP", "OrderNr", "Test", "COUNT" };
SubWindow wnd = new SubWindow(names.Count, names);
wnd.ShowDialog();
Dictionary<string, string> parameValueDict = new Dictionary<string, string>();
foreach (var item in wnd.gd.Children)
{
if (item is TextBox)
{
TextBox tb = (TextBox)item;
parameValueDict[tb.Name] = tb.Text;
}
}
foreach (var item in parameValueDict)
{
txtMessage.AppendText($"{item.Key} --{item.Value}\n");
}
5. 动态创建20个参数和演示值
private void btnDmeoAdd20_Click(object sender, RoutedEventArgs e)
{
List<string> Names = new List<string>();
for (int i = 0; i < 20; i++)
{
Names.Add($"MyVarName{i}");
}
SubWindow wnd = new SubWindow(Names.Count, Names);
wnd.ShowDialog();
Dictionary<string, string> parameValueDict = new Dictionary<string, string>();
foreach (var item in wnd.gd.Children)
{
if (item is TextBox)
{
TextBox tb = (TextBox)item;
parameValueDict[tb.Name] = tb.Text;
}
}
foreach (var item in parameValueDict)
{
txtMessage.AppendText($"{item.Key} --{item.Value}\n");
}
}
效果如下:
关闭窗口后回传输入值: