首页 > 解决方案 > C# 标签打印无

问题描述

目前正在尝试通过 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

但他们不是!想知道有没有人知道为什么

标签: c#winforms

解决方案


让我们提取方法

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();

编辑:替代没有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(...);
     });

推荐阅读