C#表单应用程序动态添加的元素定位

时间:2019-01-14 10:20:10

标签: c# winforms

首先,我对可能的语言不好感到抱歉。我是C#的新手,正在尝试构建一个简单的表单应用程序。当点击按钮时,新的文本框和标签将被放置在窗口中。这是问题所在。当我按右键单击按钮时,新添加的元素位于其他元素的右边。您能帮我解决这个问题吗?

Normal Added Elements

When Scrolled To Right

Last Positioning

static int formNo = 1;

private void btnEkle_Click(object sender, EventArgs e)
{
    TextBox isimTb = new TextBox();
    TextBox fiyatTb = new TextBox();
    Label urunLbl = new Label();

    int positionNo = formNo;

    isimTb.Name = "isimBox" + formNo.ToString();
    isimTb.Location = new Point(125, ((positionNo - 1) * 25));
    isimTb.Width = 200;
    isimTb.Text = "Ürün İsmini Giriniz";

    fiyatTb.Name = "fiyatBox" + formNo.ToString();
    fiyatTb.Left = 350;
    fiyatTb.Top = (positionNo - 1) * 25;
    fiyatTb.Width = 200;
    fiyatTb.Text = "Ürün Fiyatını Giriniz";

    urunLbl.Name = "label" + formNo.ToString();
    urunLbl.Text = formNo.ToString() + ". Ürün";
    urunLbl.Left = 10;
    urunLbl.Top = (positionNo - 1) * 25;
    urunLbl.Width = 100;


    this.Controls.Add(urunLbl);
    this.Controls.Add(isimTb);
    this.Controls.Add(fiyatTb);

    btnEkle.Top = (positionNo - 1) * 25 + 50;
    btnKaydet.Top = (positionNo - 1) * 25 + 50;

    formNo++;
}

2 个答案:

答案 0 :(得分:1)

您没有布局容器,例如TableLayoutPanel。 这些布局容器将帮助您放置动态添加的控件。 我正在粘贴为动态添加的控件所做的一些示例代码:

internal void AddControl(Models.CdConfig selectedCd)
    {
        SelectedCds.Add(selectedCd);
        if (selectedCd.DataType == CdType.Combo || selectedCd.DataType == CdType.Choice)
        {
            subItemHeight = 23;
        }
        else
        {
            subItemHeight = 30;
        }
        int currItemRowCount = getItemRowCount(selectedCd);
        Panel controlPanel = new Panel() // the panel that is visible
        {
            BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle,
            Location = new Point(3,3),
            Size = new Size(this.Size.Width - margin * 2 * 2 - scrollbarbuff, itemHeight + subItemHeight * currItemRowCount)
        };

        TableLayoutPanel t1 = new TableLayoutPanel() // main tlp
        {
            ColumnCount = 1,
            RowCount = 2,
            Size = new Size(this.Size.Width - margin * 2 * 2 - scrollbarbuff, itemHeight + subItemHeight * currItemRowCount)
        };
        t1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
        t1.RowStyles.Add(new RowStyle(SizeType.Absolute, itemHeight));
        t1.RowStyles.Add(new RowStyle(SizeType.AutoSize));

        TableLayoutPanel tHeader = new TableLayoutPanel() // label and delete button
        {
            ColumnCount = 2,
            RowCount = 1,
            Size = new Size(this.Size.Width - margin * 2 * 2 - scrollbarbuff, itemHeight)
        };
        tHeader.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80F));
        tHeader.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
        tHeader.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
        t1.Controls.Add(tHeader, 0, 0);

        // Add the label
        Label lbl = new Label()
        {
            Text = selectedCd.DisplayName,
            Anchor = AnchorStyles.Left | AnchorStyles.Top,
            Margin = new Padding(0, 3 * 2, 0, 0)
        };
        tHeader.Controls.Add(lbl, 0, 0);

        Button deleteBtn = new Button()
        {
            Text = "Delete",
            Anchor = AnchorStyles.Right | AnchorStyles.Top,
            Margin = new Padding(0, 2, 3 * 2, 0)
        };
        deleteBtn.Tag = controlPanel;
        deleteBtn.Click += HandleDelete;
        tHeader.Controls.Add(deleteBtn, 1, 0);
        controlPanel.Controls.Add(t1);

        // For the control
        TableLayoutPanel tControl = CreateCdControl(selectedCd, currItemRowCount);

        t1.Controls.Add(tControl, 0, 1);

        this.Controls.Add(controlPanel);
        controlPanel.Tag = selectedCd; // for convenience
    }

答案 1 :(得分:0)

添加控件时,请考虑AutoScrollPosition属性的值。

/// <summary>
/// Make a point that gives a location at the given coordinates in the form
/// independent of the current scroll position
/// </summary>
private Point MakeAbsolutePosition(int x, int y)
{
    Point result = new Point(x + AutoScrollPosition.X, y + AutoScrollPosition.Y);

    return result;
}

private void btnEkle_Click(object sender, EventArgs e)
{
    TextBox isimTb = new TextBox();
    TextBox fiyatTb = new TextBox();
    Label urunLbl = new Label();

    int positionNo = formNo;

    isimTb.Name = "isimBox" + formNo.ToString();
    isimTb.Location = MakeAbsolutePosition(125, ((positionNo - 1) * 25));
    isimTb.Width = 200;
    isimTb.Text = "Ürün İsmini Giriniz";

    fiyatTb.Name = "fiyatBox" + formNo.ToString();
    fiyatTb.Location = MakeAbsolutePosition(350, (positionNo - 1) * 25);
    fiyatTb.Width = 200;
    fiyatTb.Text = "Ürün Fiyatını Giriniz";

    urunLbl.Name = "label" + formNo.ToString();
    urunLbl.Text = formNo.ToString() + ". Ürün";
    urunLbl.Location = MakeAbsolutePosition(10, (positionNo - 1) * 25);
    urunLbl.Width = 100;

    this.Controls.Add(urunLbl);
    this.Controls.Add(isimTb);
    this.Controls.Add(fiyatTb);

    // TODO: Use the correct x values here
    btnEkle.Location = MakeAbsolutePosition(100, (positionNo - 1) * 25 + 50);
    btnKaydet.Location =MakeAbsolutePosition(200,  (positionNo - 1) * 25 + 50);

    formNo++;
}

documentation for the AutoScrollPosition property says

  

以编程方式将控件添加到窗体时,请使用AutoScrollPosition属性将控件放置在当前可见滚动区域的内部或外部。

使用其他答案所示的使用LayoutPanel(例如TableLayoutPanelFlowLayoutPanel)的方法更好。如果可能的话,您应该使用那个。