当我在面板小部件中动态添加新小部件时,自动滚动会影响坐标,WinForms

bfrts1fy  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(140)

我在WinForms项目的主面板中添加一个新的小部件时遇到了一个意想不到的问题。(Bottom + 2)的其他.这个地方我在我的静态变量.一切正常,正如我所料.当我点击底部+,我的面板出现在第一个(图片1). Everything is OK但是,如果我点击我的按钮+,这样一个滚动线可以出现,我把它向下移动,并再次点击我的按钮+.下一个面板放置不只是(底部+ 2),但它增加了一些像素取决于多远是滚动按钮从它的中性点。图片(2)This space between my Panels is getting bigger than far than I move the scrollButton
我的类代码

internal class AddPanel
    {
        const int coordX = 3;
        static int coordY = 4;
        static int num;

        TextBox TBnum, TBquest, TBansw; // for each textbox from our panel
        Panel newPanel; // the panel proper

        public AddPanel(Panel MainPanel, params Button[] buttons)
        {
            newPanel = new Panel();
            TBnum = new TextBox();
            TBquest = new TextBox();
            TBansw = new TextBox();

            newPanel.Location = new Point(coordX, coordY); //TODO: Change coords
            newPanel.Size = new Size(350, 88);
            newPanel.Name = "MyPanel" + Convert.ToString(num);

            TBnum.Location = new Point(3, 3);
            TBnum.Name = "MyTBNum" + Convert.ToString(num);
            TBnum.Size = new Size(27, 23);

            TBquest.Location = new Point(50, 3);
            TBquest.Name = "MyTBquest" + Convert.ToString(num);
            TBquest.Size = new Size(287, 23);

            TBansw.Location = new Point(50, 32);
            TBansw.Name = "MyTBansw" + Convert.ToString(num);
            TBansw.Multiline = true;
            TBansw.Size = new Size(287, 47);

            if (num == 0)
            {
                TBnum.PlaceholderText = "1";
                TBquest.PlaceholderText = "Вопрос. Напр: Адрес приемной комиссии?";
                TBansw.PlaceholderText = "Ответ на вопрос";

            }
            else TBnum.Text = Convert.ToString(num + 1);

            //foreach (Button btn in buttons)
                //btn.Location = new Point(btn.Location.X, btn.Location.Y + 100);

            newPanel.Controls.Add(TBnum);
            newPanel.Controls.Add(TBquest);
            newPanel.Controls.Add(TBansw);
            newPanel.BorderStyle = BorderStyle.FixedSingle;
            MainPanel.Controls.Add(newPanel);

            coordY = newPanel.Bottom + 2 ;
            num++;

        }

        static AddPanel()
        {
            num = 0;
        }
    }

按钮代码:

new AddPanel(MainScrollPanel, UpButton, DownButton);

我怎么能处理这个问题呢?我试着谷歌了一下,但什么也没找到。

kuhbmx9i

kuhbmx9i1#

我做了一个决定,哈哈。

MainScrollPanel.AutoScrollPosition = new Point(0, 0);

我们可以在创建新的Panel之前将ScrollLine移动到中间位置。

相关问题