2

I want to create more than 1 TextBox in my application.

Only create 1 textbox

My code bellow only creates one TextBox because I create a global object.

My code to do this:

private Label ctrLabel = new Label();

public void btnAddCharacter_Click(object sender, EventArgs e)
{
    String LableName = "Lbl_";
    ctrLabel.Name = LableName;
    ctrLabel.Text = txtIDImg.Text;
    panel2.Controls.Add(ctrLabel);
}

If put Label ctrLabel = new Label(); inside the event btnAddCharacter_Click it will be create multiple object. But all control using ctrLabel will error because it doesn't know ctrLabel. The problem is: in other control of Winform will be using and manipulation with this object.

So, I don't know when user click text 1 or text 2, etc.. to apply a change to the corresponding with event like: cbxFontSize_SelectedIndexChanged, cbxFont_TextChanged, etc .....

My code like this:

private void cbxFontSize_SelectedIndexChanged(object sender, EventArgs e)
{
    ctrLabel.Font = new Font(ctrLabel.Font.FontFamily, Convert.ToInt32(cbxFontSize.SelectedItem),
        ctrLabel.Font.Style);
}

private void cbxFont_TextChanged(object sender, EventArgs e)
{
    ctrLabel.Font = new Font(cbxFont.Text, ctrLabel.Font.Size, ctrLabel.Font.Style);
}

and much other control will change a property of an object(TextBox).

0

2 Answers 2

2

It's hard to tell exactly what you are trying to do here, but if you want to manage multiple Labels that are created dynamically, you should maintain them using a list. I.E. like below (not tested code)

List<Label> activeLabels = new List<Label>();

private void CreateLabel() {
    Label ctrLabel = new Label();
    String LableName = "Lbl_" + activeLabels.Count();
    ctrLabel.Name = LableName;
    ctrLabel.Text = txtIDImg.Text;
    panel2.Controls.Add(ctrLabel);
    activeLabels.Add(ctrLabel);
}

private Label GetLabel(int index) {
    if (index > 0 && index < activeLabels.Count())
        return activeLabels[index];

    return null;
}
Sign up to request clarification or add additional context in comments.

Comments

1
private Label SelectedLabel; //don't initialize here (also note the name change)

public void btnAddCharacter_Click(object sender, EventArgs e)
{
    //do it here:
    var ctrLabel = new Label();
    ctrLabel.Name = "Lbl_";
    ctrLabel.Text = txtIDImg.Text;
    ctrLabel.BackColor = Color.Transparent;

    // In each of these events, add a line at the very top that looks like this:
    //     var ctrLabel = sender as Label;
    ctrLabel.MouseEnter += new EventHandler(control_MouseEnter);
    ctrLabel.MouseLeave += new EventHandler(control_MouseLeave);
    ctrLabel.MouseDown += new MouseEventHandler(control_MouseDown);
    ctrLabel.MouseMove += new MouseEventHandler(control_MouseMove);
    ctrLabel.MouseUp += new MouseEventHandler(control_MouseUp);

    panel2.Controls.Add(ctrLabel);
}

// Somewhere (perhaps in the events above) you have code that decides
// which control is selected. Now, instead of `ctrLabel`, you track this
// assigning to the `SelectedLabel` variable. Then the two methods below
// can look like this:
private void cbxFontSize_SelectedIndexChanged(object sender, EventArgs e)
{
    if (SelectedLabel == null) return;
    SelectedLabel.Font = new Font(ctrLabel.Font.FontFamily, Convert.ToInt32(cbxFontSize.SelectedItem),
        SelectedLabel.Font.Style);
}
private void cbxFont_TextChanged(object sender, EventArgs e)
{
    if (SelectedLabel == null) return;
    SelectedLabel.Font = new Font(cbxFont.Text, SelectedLabel.Font.Size, SelectedLabel.Font.Style);
}

1 Comment

Thanks. I fixed all. Your answer is useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.