如何在C#中创建多个对象?

时间:2016-05-07 03:37:49

标签: c# winforms

我想在我的应用程序中创建多个TextBox

Only create 1 textbox

我的代码只会创建一个TextBox,因为我创建了一个全局对象。

我的代码:

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

如果将Label ctrLabel = new Label();放入事件btnAddCharacter_Click中,则会创建多个对象。 但是使用ctrLabel的所有控件都会出错,因为它不知道ctrLabel。 问题是:在Winform的其他控件中将使用和操纵此对象。

因此,我不知道用户何时点击text 1text 2等,以便对相应的事件应用更改,例如:cbxFontSize_SelectedIndexChangedcbxFont_TextChanged等等......

我的代码是这样的:

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

和许多其他控件将更改对象的属性(TextBox)。

2 个答案:

答案 0 :(得分:2)

很难确切地告诉您这里要做什么,但如果您想要管理动态创建的多个Labels,则应使用列表维护它们。 I.E.如下(未经测试的代码)

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

答案 1 :(得分: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);
}