-1

I am creating new controls and putting there names in a list box, how do i use the name selected in the list box to change the controls properties.

//creating the label
 LabelNumber++;
 label label=new Label();
 label.BackColor=Color.Transparent;
 label.Location=new System.Drawing.Point(1,
1);
 label.Name="Label" + LabelNumber;
 label.Text=LabelNumber.ToString();
 label.Size=new System.Drawing.Size(20,
20);
 label.TextAlign=ContentAlignment.MiddleCenter;
 ProjectPanel.Controls.Add(label);
 ControlBox1.Items.Add(label.Name);
Sam Hinchy
  • 11
  • 5
  • Depending on which bit you are having trouble with, [this may also be of use](http://stackoverflow.com/questions/15003095/getting-value-of-selected-item-in-list-box-as-string) – musefan Oct 27 '14 at 10:56

3 Answers3

0

Yo can use the Label Name under ControlBox1_SelectedIndexChanged event and get the value of selected indexed Label Name.

Bhavesh Harsora
  • 655
  • 5
  • 14
  • -1 Not only does this not go in to detail about how to get the name of the label, but it doesn't even attempt to address the requirement of obtaining a reference to the control (with the matching name) and then changing the properties of that control – musefan Oct 27 '14 at 11:12
0

You can create a simple 'listboxitem' structure and use it like this:

struct lbo
{
    // make the structure immutable
    public readonly Control ctl;
    // a simple constructor
    public lbo(Control ctl_) { ctl = ctl_; }
    // make it show the Name in the ListBox
    public override string ToString() { return ctl.Name; }
}

private void button1_Click(object sender, EventArgs e)
{
    // add a control:
    listBox1.Items.Add(new lbo(button1));
}

private void button2_Click(object sender, EventArgs e)
{
    // to just change the _Name (or Text or other properties present in all Controls)
    ((lbo)listBox1.SelectedItem).ctl.Text = button2.Text;

    // to use it as a certain Control you need to cast it to the correct control type!!
    ((Button)((lbo)listBox1.SelectedItem).ctl).FlatStyle = yourStyle;

    // to make the cast safe you can use as
    Button btn = ((lbo)listBox1.SelectedItem).ctl as Button;
    if (btn != null) btn.FlatStyle = FlatStyle.Flat;
}

No checks here for the correct type or that you have selected an item..but you get the idea: put something more useful than a naked object or a mere string into the ListBox!

You could instead loop over all controls and compare the names but that's less efficient and actually not safe as the Name property is not guaranteed to be unique..

TaW
  • 53,122
  • 8
  • 69
  • 111
0

The question suggests that the OP is using a ListBox, so my code makes that assumption.

Essentially what you need to do is as follows: Get the selected text from the ListBox, find the control that has the same Name (we will assume this is always unique), then change the properties of that control.

The following code will meet those requirements:

// Get the selected text from the ListBox.
string name = ControlBox1.GetItemText(ControlBox1.SelectedItem);

// Find the control that matches that Name. Assumes there is only ever 1 single match.
Control control = ProjectPanel.Controls.Find(name, true).FirstOrDefault();

// Set properties of the Control.
control.Name = "new name";

// If you know it's a Label, you can cast to Label and use Label specific properties.
Label label = control as Label;
label.Text = "some new text";
musefan
  • 47,875
  • 21
  • 135
  • 185