簡體   English   中英

如何使用c#在winform中的按鈕單擊事件上將多個面板移動到不同位置?

[英]how to move several panels on to different locations on a button click event in winform using c#?

我在一個事件上生成了幾個面板,在這些面板中,我有一個按鈕將刪除該特定面板。 現在,我要在刪除沒有間隙的面板時移動其他面板。 我的面板生成功能是...。

public void generate_editpanel(string c)
    {
        int i, j, k, b;
        int m, n;


        //Bitmap bmp = drawled(16, 16, Color.Black, Color.White);
        //imglst.Images.Add(bmp);


        for (i = 0; i <= 20; i++)
        {
            epnl[i] = new Panel();
            epnl[i].Visible = false;
            epnl[i].Enabled = true;
            epnl[i].Name = "epnl" + c;
            epnl[i].Location = new System.Drawing.Point(x, 0);
            epnl[i].Width = 260;
            epnl[i].Height = 173;
            epnl[i].BorderStyle = BorderStyle.Fixed3D;
            EditPanel.Controls.Add(epnl[i]);


            grid[i] = new DataGridView();
            cellstl = new DataGridViewCellStyle();

            grid[i].Visible = true;
            grid[i].AllowUserToAddRows = false;
            grid[i].AllowUserToDeleteRows = false;
            grid[i].AllowUserToOrderColumns = false;
            grid[i].AllowUserToResizeRows = false;
            grid[i].AllowUserToResizeColumns = false;
            grid[i].ColumnHeadersVisible = false;
            grid[i].RowHeadersVisible = false;
            grid[i].Name = "grid" + c;
            grid[i].Location = new System.Drawing.Point(120,5);
            grid[i].Size = new System.Drawing.Size(129, 129);
            grid[i].BackgroundColor = Color.Black;
            grid[i].GridColor = Color.Green;
            grid[i].CellBorderStyle = DataGridViewCellBorderStyle.None;
            grid[i].ScrollBars = ScrollBars.None;
            fill(Color.Black, Color.White);
            for (j = 1; j <= 8; j++)
            {

                col = new DataGridViewImageColumn();
                col.Width = 16;
                col.Image = imglst.Images[0];
                col.ImageLayout = DataGridViewImageCellLayout.Normal;
                grid[i].Columns.Add(col);
                grid[i].Columns[j - 1].FillWeight = 1;
            }
            for (k = 1; k <= 8; k++)
            {
                grid[i].Rows.Add();
                grid[i].Rows[k - 1].Height = 16;
            }
            cellstl.Alignment = DataGridViewContentAlignment.MiddleCenter;
            cellstl.BackColor = Color.White;
            grid[i].CellMouseClick += new DataGridViewCellMouseEventHandler(this.gridmouseclick);
            grid[i].CellMouseMove += new DataGridViewCellMouseEventHandler(this.gridmousemove);
            grid[i].CellMouseDown += new DataGridViewCellMouseEventHandler(this.gridmousedown);
            grid[i].CellMouseUp += new DataGridViewCellMouseEventHandler(this.gridmouseup);

            //for (m = 1; m <= 8; m++)
            //{
            //    for (n = 1; n <= 8; n++)
            //    {
            //        griddata[m, n] = 0;
            //    }
            //}
            epnl[i].Controls.Add(grid[i]);




            cbtn[i] = new Button();
            cbtn[i].Visible = true;
            cbtn[i].Location = new System.Drawing.Point(1, 1);
            cbtn[i].Name = "ctbn" + c;
            cbtn[i].Width = 25;
            cbtn[i].Height = 25;
            cbtn[i].Click += new System.EventHandler(this.dropedit);
            cbtn[i].BackgroundImage = Video_Project.Properties.Resources.w2;
            cbtn[i].BackgroundImageLayout = ImageLayout.Stretch;
            epnl[i].Controls.Add(cbtn[i]);


            lbl[i] = new Label();
            lbl[i].Visible = true;
            lbl[i].Location = new System.Drawing.Point(3, 79);
            lbl[i].Name = "lblr" + c;
            lbl[i].Width = 45;
            lbl[i].Height = 13;
            lbl[i].Text = "Repeat:";
            epnl[i].Controls.Add(lbl[i]);


            lbl[i] = new Label();
            lbl[i].Visible = true;
            lbl[i].Location = new System.Drawing.Point(3, 106);
            lbl[i].Name = "lbls" + c;
            lbl[i].Width = 45;
            lbl[i].Height = 13;
            lbl[i].Text = "Speed:";
            epnl[i].Controls.Add(lbl[i]);


            lbl[i] = new Label();
            lbl[i].Visible = true;
            lbl[i].Location = new System.Drawing.Point(3, 145);
            lbl[i].Name = "lblt" + c;
            lbl[i].Width = 45;
            lbl[i].Height = 13;
            lbl[i].Text = "Text:";
            epnl[i].Controls.Add(lbl[i]);


            lbl[i] = new Label();
            lbl[i].Visible = true;
            lbl[i].Location = new System.Drawing.Point(3, 0);
            lbl[i].AutoSize = false;
            lbl[i].Width = 70;
            lbl[i].Height = 70;
            lbl[i].Text = c;
            lbl[i].TextAlign = ContentAlignment.MiddleCenter;
            epnl[i].Controls.Add(lbl[i]);
            b = Convert.ToInt32(c);
            b += 1;
            c = Convert.ToString(b);

            txt[i] = new TextBox();
            txt[i].Visible = true;
            txt[i].Location = new System.Drawing.Point(50, 72);
            txt[i].Name = "txtr" + c;
            txt[i].Width = 35;
            txt[i].Height = 20;
            txt[i].MaxLength = 2;
            epnl[i].Controls.Add(txt[i]);


            txt[i] = new TextBox();
            txt[i].Visible = true;
            txt[i].Location = new System.Drawing.Point(50, 103);
            txt[i].Name = "txts" + c;
            txt[i].Width = 35;
            txt[i].Height = 20;
            txt[i].MaxLength = 2;
            epnl[i].Controls.Add(txt[i]);


            txt[i] = new TextBox();
            txt[i].Visible = true;
            txt[i].Location = new System.Drawing.Point(50, 142);
            txt[i].Name = "txtt" + c;
            txt[i].Width = 200;
            txt[i].Height = 20;
            txt[i].MaxLength = 250;
            epnl[i].Controls.Add(txt[i]);

            x = x + 280;
        }

    } 

我正在其他事件上設置特定面板的可見模式

 private void viewscreen_MouseDown(object sender, MouseEventArgs e)
    {
        int i;
        if (e.Button == MouseButtons.Right)
        {
            for (i = 0; i <= 20; i++)
            {
                //string c = Convert.ToString(b);
                //generate_editpanel(c);
                if (b == i)
                {
                    epnl[i].Visible = true;
                }
            }
        }
    }

我正在刪除另一個事件上的特定面板...

public void dropedit(object sender, EventArgs e)
    {
        int i;
        for (i = 0; i <= 20; i++)
        {
            if (cbtn[i].Capture)
            {
                EditPanel.Controls.Remove(epnl[i]);
                epnl[i].Dispose();
            }
        }
    }

所以現在我在刪除面板時會出現間隙,我想將其余面板向左移動並消除該間隙。 任何人都可以幫助...如果您需要其他信息,請告訴我

您可以將FlowLayoutPanel用作面板的容器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM