简体   繁体   English

赢形式的 C# 标签或文本框计数

[英]C# count of labels or textboxs in win form

How Can i retrieve number of labels in winform dynamicly?如何动态检索 winform 中的标签数量? Because in my form i have about 12 labels and if i press button i want to change every label to textbox.因为在我的表单中我有大约 12 个标签,如果我按下按钮,我想将每个标签更改为文本框。 So in sum I want to make every label editable and after editing and save change it back to label.所以总而言之,我想让每个标签都可以编辑,并在编辑和保存后将其更改回标签。 In summary it will be like editable label.总之,它就像可编辑的标签。 But it take lot of time and rows in code to write change for every label, so if it is possible to make it dynamic i will be perfect.但是为每个标签编写更改需要大量时间和代码行,因此如果可以使其动态化,我将是完美的。 Thanks.谢谢。 这是一个例子

I suggest have TextBox es, not Label s to contain any text which can be edited.我建议使用TextBox es,而不是Label s来包含任何可以编辑的文本。 So we have TextBox in 2 different modes Label Mode (no editing) and TextBox Mode .所以我们有两种不同模式的TextBox标签模式(无编辑)和TextBox 模式 To switch between them (assuming WinForms ):在它们之间切换(假设WinForms ):

// Make TextBox to look like a label: readonly, color, border etc.
private static void ToLabelMode(TextBox box) {
  if (null == box)
    return;

  box.HideSelection = true;
  box.BackColor = SystemColors.Control;
  box.ReadOnly = true;
  box.BorderStyle = BorderStyle.None;
}

private static void ToTextBoxMode(TextBox box) {
  if (null == box)
    return;

  box.HideSelection = false;
  box.BackColor = SystemColors.Window;
  box.ReadOnly = false;
  box.BorderStyle = BorderStyle.Fixed3D;
}

Then you can use them:然后你可以使用它们:

TextBox[] m_TextBoxes;

private void MyForm_Load(object sender, EventArgs e) {
  m_TextBoxes = new TextBox[] {
    textBoxFirstName, 
    textBoxLastName, 
    //TODO: Put the relevant ones
  };

  // Let all TextBox be in Label mode
  EnableEdit(false);
}

private void EnableEdit(bool enabled) {
  foreach (var box in m_TextBoxes)
    if (enabled)
      ToTextBoxMode(box);
    else 
      ToLabelMode(box); 
}

Edit: If you insist on having Label and TextBox I suggest having both of them and use Visible to show the right control (either Label or corresponding TextBox ):编辑:如果您坚持使用LabelTextBox我建议同时使用它们并使用Visible来显示正确的控件( Label或相应的TextBox ):

Dictionary<Label, TextBox> m_TextBoxesPairs;

private void MyForm_Load(object sender, EventArgs e) {
  m_TextBoxesPairs = new Label[] {
    labelFirstName,
    labelSecondName,
    //TODO: put all the relevant labels here
  }
  .ToDictionary(lbl => lbl,
                lbl => new TextBox() {
                  Parent   = lbl.Parent,
                  Text     = lbl.Text,
                  Location = lbl.Location,
                  Size     = lbl.Size,
                  Visible  = false
                });

  // If you want to modify Label.Text on TextBox.Text changing
  foreach (var pair in m_TextBoxesPairs)
    pair.Value.TextChanged += (o, ee) => {pair.Key.Text = pair.Value.Text;} 

  EnableEdit(false);  
}

private void EnableEdit(bool enabled) {
  foreach (var pair in m_TextBoxesPairs) {
      pair.Key.Visible = !enabled;
      pair.Key.Visible =  enabled;
    } 
}

Try following :尝试以下:

            Control[] labels = this.Controls.Cast<Control>().Where(x => x.GetType() == typeof(Label)).ToArray() ;
            for(int i = labels.Length - 1; i >= 0; i--)
            {
                Label label = (Label)labels[i];
                TextBox newBox = new TextBox();
                newBox.Left = label.Left;
                newBox.Top = label.Top;
                newBox.Width = label.Width;
                newBox.Height = label.Height + 10;
                newBox.Text = label.Text;
                label.Parent.Controls.Add(newBox);
                label.Parent.Controls.Remove(label);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM