目前正在尝试通过c# & winforms动态地制作一些标签。基本上想要添加一个零件号,如果它存在的话,给一个新的标签加很多号码。
我编写了这段代码,它的工作原理与预期一样:
string[] item_list = { "PN=12345678", "PN=1234-5678-0001", "PN=1234-4321-0001;LOT=xyz" };
string PN_EQ = "";
string LOT_EQ = "";
string final_form = "";
foreach (string item in item_list)
{
LOT_EQ = ""; // Clear BK_EQ after every iteration
string[] split = item.Split(';');
PN_EQ = split[0].Substring(3, split[0].Length - 3); // Removing PN=, will always exists
final_form = string.Format("PN_EQ:{0}", PN_EQ);
if(split.Length == 2)
{
LOT_EQ = split[1].Substring(4, split[1].Length - 4); // Removing LOT= if it exists
final_form = final_form + string.Format(" LOT_EQ: {0}", LOT_EQ);
}
Console.WriteLine(final_form);
} 产出,如预期:
PN_EQ: 12345678
PN_EQ: 1234-5678-0001
PN_EQ: 1234-4321-0001 LOT_EQ: xyz所以我把这个逻辑带到标签世界
string[] item_list = { "PN=12345678", "PN=1234-5678-0001", "PN=1234-4321-0001;LOT=xyz" };
string PN_EQ = "";
string LOT_EQ = "";
List<Label> labels = new List<Label>();
foreach (string item in item_list)
{
Label lbl = new Label(); // create new label for each thing
LOT_EQ = ""; // Clear BK_EQ after every iteration
string[] split = item.Split(';');
PN_EQ = split[0].Substring(3, split[0].Length - 3); // Removing PN=, will always exists
lbl.Name = PN_EQ // Unique Names for each label
lbl.Text = string.Format("PN_EQ:{0}", PN_EQ);
if(split.Length == 2)
{
LOT_EQ = split[1].Substring(4, split[1].Length - 4); // Removing LOT= if it exists
lbl.Text = lbl.Text + string.Format(" LOT_EQ: {0}", LOT_EQ);
}
labels.Add(lbl);
this.Controls.Add(lbl);
}然后在出现提示时输出:
PN_EQ: 12345678
PN_EQ: // Should be output
PN_EQ: // Should be output但他们不是!想知道是否有人知道为什么
发布于 2020-02-17 13:17:37
让我们来提取方法:
private static string FormatMe(string value) {
if (string.IsNullOrEmpty(value))
return value;
else if (!value.StartsWith("PN="))
return value;
int lot = value.IndexOf(";LOT=", 3);
if (lot >= 0)
return $"PN_EQ: {value.Substring(3, lot - 3)} LOT_EQ: {value.Substring(lot + 5)}";
else
return $"PN_EQ: {value.Substring(3)}";
}让我们看看:
string[] item_list = {
"PN=12345678",
"PN=1234-5678-0001",
"PN=1234-4321-0001;LOT=xyz"
};
string report = string.Join(Environment.NewLine, item_list
.Select(item => FormatMe(item)));
Console.Write(report);结果:
PN_EQ: 12345678
PN_EQ: 1234-5678-0001
PN_EQ: 1234-4321-0001 LOT_EQ: xyz创建Labels的时间(假设为WinForms):
using System.Linq;
...
string[] item_list = {
"PN=12345678",
"PN=1234-5678-0001",
"PN=1234-4321-0001;LOT=xyz"
};
// let's query item_list:
// for each item within item_list
// we create a Label
// all the labels we materialize in a List
List<Label> labels = item_list
.Select((item, index) => new Label() {
Text = FormatMe(item),
Location = new Point(10, 50 + 40 * index), //TODO: put the right locations here
AutoSize = true,
Parent = this, // Instead of this.Controls.Add(...);
})
.ToList();编辑: Alternative no Linq但循环解决方案:
List<Label> labels = new List<Label>(item_list.Length);
for (int index = 0; index < item_list.Length; ++index)
labels.Add(new Label() {
Text = FormatMe(item_list[index]),
Location = new Point(10, 50 + 40 * index), //TODO: put the right locations here
AutoSize = true,
Parent = this, // Instead of this.Controls.Add(...);
});https://stackoverflow.com/questions/60263032
复制相似问题