简体   繁体   中英

C# count of labels or textboxs in win form

How Can i retrieve number of labels in winform dynamicly? Because in my form i have about 12 labels and if i press button i want to change every label to textbox. 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. So we have TextBox in 2 different modes Label Mode (no editing) and TextBox Mode . To switch between them (assuming 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 ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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