控件仅在面板内部移动

问题描述:

亲爱的朋友,

我正在开发一个小项目,在我的项目中,我有一个面板,该面板内部有一些控件,如图片框,文本框等.当鼠标移动时,我只想在此面板内部移动这些控件.我完成了控件的移动,但它也移到了面板的外部.

我想在移动鼠标时仅在面板内部移动控件.

请回复[已删除紧急情况]

[edit]已删除呼喊声-OriginalGriff [/edit]

Dear Friends,

I am developing one small project, in my project i have one panel, inside of this panel have some controls like picture box, text boxes etc.... I want to move this controls only inside of this panel when the mouse move. I finished to move controls but it is moving outside of my panel also.

I want to move controls only inside of panel when i move mouse.

Please reply [Removed Urgency]

[edit]SHOUTING removed - OriginalGriff[/edit]

要解决此问题,请考虑代码段的面板左侧和顶部值,

可能是以下解决方案为您充分利用了!

To resolve this issue, please consider the panel left and top values for your piece of code,

May be the below solution is use full for you!

if(panel1.Left < e.X)
            Control.Left = e.X;
            if(panel1.Top <e.Y)
            Control.Top = e.Y;


在运行时获取面板底部和面板右侧的值,然后调整边界.

希望你能得到我!

祝您编程愉快:)
Get your panel botton and panel right values in the run time and then adjust the boundaries.

Hope you got me!

Happy coding:)


我们无法设置right和bottom属性值!这些值是只读的,因此我们总是只需要修改Top和Left属性.

希望以下方法对您有用!

取2个面板并将它们放在面板的底部和右侧.
根据您的要求设置topBoundary和rightBoundary.我尝试过这种方法效果很好.

We can''t set right and bottom property values! These values are readonly so we always need to modify Top and Left properties only.

Hope the below approach is useful for you!

Take 2 panels and place them in bottom and right side of your panel.
set topBoundary and rightBoundary as per your requirement. I tried this approach is working fine.

int topBoundary;
        int rightBoundary;
        private void Form1_Load(object sender, EventArgs e)
        {
            topBoundary = panel1.Bottom - 60;
            rightBoundary = panel1.Right - 100;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            checkBox1.Top = e.Y;
            checkBox1.Left = e.X;
        }

        private void panel2_MouseEnter(object sender, EventArgs e)
        {
            checkBox1.Top = topBoundary;
        }

        private void panel3_MouseEnter(object sender, EventArgs e)
        {
            checkBox1.Left = rightBoundary;
        }