将项目从一个单元格移动到另一个单元格 [英] Move Item from one cell to another

查看:51
本文介绍了将项目从一个单元格移动到另一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 16 个单元格的 tableLayoutPanel.15 个单元格有对照.我希望能够在运行时将控件从一个单元格移动到另一个单元格.

I have a tableLayoutPanel with 16 cells. 15 of the cells have controls. I want to be able to move the controls from one cell to another at runtime.

我用过

    private void button15_Click(object sender, EventArgs e)
    {
        tableLayoutPanel1.Controls.Remove(button15);

        tableLayoutPanel1.Controls.Add(button15, 3, 3);
    }

这很好用,但我想知道是否有更好的方法来做到这一点???

This works well but i want to know if there is any better way to do this???

推荐答案

Winforms 中,你可以只在其父级内部移动一个控件(当然也有一些例外到一些实际上没有任何父级的控件).所以这里的想法是,如果您想移动 TableLayoutPanel 的控件,则必须将其 Parent 设置为另一个 containerForm鼠标按住,移动时,控件的位置在新的父级,释放鼠标后,我们必须将控件的父级设置回TableLayoutPanel,当然我们必须找到下拉单元格位置并使用 SetCellPosition 方法将控件定位在 TableLayoutPanel 上,这是给你的演示代码(效果很好),我使用 2 Buttons 在这个演示中,你可以用你想要的任何控件替换它们:

In Winforms, you can only move a control inside its parent (of course there are some exceptions to some controls which in fact don't have any Parent). So the idea here is if you want to move a control of your TableLayoutPanel, you have to set its Parent to your Form of another container when mouse is held down, when moving, the position of the control is in the new parent, after mouse is released, we have to set the Parent of the control to the TableLayoutPanel back, of course we have to find the drop-down cell position and use SetCellPosition method to position the control on the TableLayoutPanel, here is the demo code for you (works great), I use 2 Buttons in this demo, you can replace them with any control you want:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        //This will prevent flicker
        typeof(TableLayoutPanel).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(tableLayoutPanel1, true, null);
    }
    Point downPoint;
    bool moved;
    //This is used to store the CellBounds together with the Cell position
    //so that we can find the Cell position later (after releasing mouse).
    Dictionary<TableLayoutPanelCellPosition, Rectangle> dict = new Dictionary<TableLayoutPanelCellPosition, Rectangle>();
    //MouseDown event handler for all your controls (on the tableLayoutPanel1)
    private void Buttons_MouseDown(object sender, MouseEventArgs e) {
        Control button = sender as Control;
        button.Parent = this;            
        button.BringToFront();            
        downPoint = e.Location;            
    }
    //MouseMove event handler for all your controls (on the tableLayoutPanel1)
    private void Buttons_MouseMove(object sender, MouseEventArgs e) {
        Control button = sender as Control;
        if (e.Button == MouseButtons.Left) {
            button.Left += e.X - downPoint.X;
            button.Top += e.Y - downPoint.Y;
            moved = true;
            tableLayoutPanel1.Invalidate();
        }
    }
    //MouseUp event handler for all your controls (on the tableLayoutPanel1)
    private void Buttons_MouseUp(object sender, MouseEventArgs e) {
        Control button = sender as Control;
        if (moved) {
            SetControl(button, e.Location);
            button.Parent = tableLayoutPanel1;
            moved = false;
        }
    }
    //This is used to set the control on the tableLayoutPanel after releasing mouse
    private void SetControl(Control c, Point position) {
        Point localPoint = tableLayoutPanel1.PointToClient(c.PointToScreen(position));
        var keyValue = dict.FirstOrDefault(e => e.Value.Contains(localPoint));
        if (!keyValue.Equals(default(KeyValuePair<TableLayoutPanelCellPosition, Rectangle>))) {
            tableLayoutPanel1.SetCellPosition(c, keyValue.Key);
        }
    }
    //CellPaint event handler for your tableLayoutPanel1
    private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
        dict[new TableLayoutPanelCellPosition(e.Column, e.Row)] = e.CellBounds;
        if (moved) {
            if (e.CellBounds.Contains(tableLayoutPanel1.PointToClient(MousePosition))) {
                e.Graphics.FillRectangle(Brushes.Yellow, e.CellBounds);
            }
        }
    }
}

这篇关于将项目从一个单元格移动到另一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆