当用户滚动到随机位置时,如何设置新字段的位置

时间:2015-03-27 16:24:34

标签: c# .net

我正在尝试在用户单击按钮时创建一个新的文本框行。但是,如果用户向下滚动然后单击按钮,则新文本框似乎低于我希望它们。我如何处理这个以便文本框对齐?

    private void button1_Click(object sender, EventArgs e)
    {
        TotalNumberAdded++;

        AddControls(TotalNumberAdded);
    }

    private void AddControls(int rowCounter)
    {

        // Convert 'rowCounter' to string
        string counter = rowCounter.ToString();
        // Create new TextBox
        var newTextbox = new TextBox();
        //panel1.Controls.Add(newTextbox);
        newTextbox.Name = "txt_" + counter;
        //newTextbox.Location = new Point(60, y);            
        newTextbox.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
        newTextbox.Top = y;
        newTextbox.Left = 60;
        // Add items to panel, then add panel to form
        this.panel1.Controls.Add(newLabel);
        this.panel1.Controls.Add(newTextbox);
        y = y + 40;
    }

1 个答案:

答案 0 :(得分:0)

我想出来了。在面板中创建锚标签,并使用当前相对位置newTextbox.Top = mAnchor.Location.Y + y;作为Y轴。