首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#标签打印无

C#标签打印无
EN

Stack Overflow用户
提问于 2020-02-17 13:01:09
回答 1查看 110关注 0票数 0

目前正在尝试通过c# & winforms动态地制作一些标签。基本上想要添加一个零件号,如果它存在的话,给一个新的标签加很多号码。

我编写了这段代码,它的工作原理与预期一样:

代码语言:javascript
复制
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);
} 

产出,如预期:

代码语言:javascript
复制
PN_EQ: 12345678
PN_EQ: 1234-5678-0001
PN_EQ: 1234-4321-0001 LOT_EQ: xyz

所以我把这个逻辑带到标签世界

代码语言:javascript
复制
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);
}

然后在出现提示时输出:

代码语言:javascript
复制
PN_EQ: 12345678
PN_EQ: // Should be output
PN_EQ: // Should be output

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-17 13:17:37

让我们来提取方法:

代码语言:javascript
复制
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)}";
}

让我们看看:

代码语言:javascript
复制
  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);

结果:

代码语言:javascript
复制
PN_EQ: 12345678
PN_EQ: 1234-5678-0001
PN_EQ: 1234-4321-0001 LOT_EQ: xyz

创建Labels的时间(假设为WinForms):

代码语言:javascript
复制
 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但循环解决方案:

代码语言:javascript
复制
    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(...);
     });
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60263032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档