如何使用标签在面板框内书写?

时间:2018-05-07 11:37:56

标签: c# winforms

以下是在文本框中使用G表示形式编写字母*的代码。现在我想使用标签在面板中写这个。我该怎么办?

我在面板内绘制了标签框,想要在标签内写。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication31
{
  public partial class Form1 : Form {

    public Form1() {
      InitializeComponent();
    }

    private void A(object sender, EventArgs e) {
      int row, column;

      for (row = 0; row <= 6; row++) {
        for (column = 0; column <= 6; column++) {
          if ((column == 1 && row != 0 && row != 6) || 
              ((row == 0 || row == 6) && column > 1 && column < 5) || 
               (row == 3 && column > 2 && column < 6) || 
               (column == 5 && row != 0 && row != 2 && row != 6))
            textBox1.AppendText("*");
          else
            textBox1.AppendText(" ");
        }

        textBox1.AppendText(Environment.NewLine);
      }
      textBox1.AppendText(Environment.NewLine);
    }
  }
}

2 个答案:

答案 0 :(得分:1)

对于TextBox:

textBox1.AppendText(text);

对于标签:

label1.Text += text;

使用StringBuilder单独构建文本。

StringBuilder builder = new StringBuilder();

int row, column;

for (row = 0; row <= 6; row++)
{
    for (column = 0; column <= 6; column++)
    {
        if ((column == 1 && row != 0 && row != 6) ||
            ((row == 0 || row == 6) && column > 1 && column < 5) ||
                (row == 3 && column > 2 && column < 6) ||
                (column == 5 && row != 0 && row != 2 && row != 6))
            builder.Append("*");
        else
            builder.Append(" ");
    }

    builder.Append(Environment.NewLine);
}
builder.Append(Environment.NewLine);

string text = builder.ToString();

答案 1 :(得分:0)

假设您可以使用文本框来满足您的需求,

但是,标签控件默认不是多行的。文本框可以像使用多行属性那样制作(因此你需要检查兼容性)

将您的代码重构为

extension UIView {

    public func addSubviewScreenCenter() {
        if let keyWindow = UIApplication.shared.keyWindow {
            keyWindow.addSubview(self)
            self.center()
        }
    }

    public func removeFromWindowView() {
        if self.superview != nil {
            self.removeFromSuperview()
        }
    }
}