C#WinForm的锁控制 [英] C# Lock WinForm Controls

查看:226
本文介绍了C#WinForm的锁控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我写用户可以添加控件到窗体和移动他们,并且设置一个伪设计模式中的某些特性的程序。我希望能够给所有这些控件锁定在一个位置,当他们按下一个按钮,切换到数据模式。我怎样才能做到这一点?我想要做的是能遍历所有的控件和使用锁定属性,但我注意到,它并没有在智能感知出现。

In a program I have written users can add controls to the form and move them around and set some properties in a pseudo design mode. I want to be able to lock all these controls into one location when they press a button to switch to "data mode". How can I do this? I wanted to do be able to loop through all the controls and use the Lock Property but I noticed it didn't show up in intellisense.

谢谢!

推荐答案

我对伪设计假设模式你的意思是,你的应用程序在运行时的状态,以及最终用户正在经历一个虚拟设计模式:请纠正我,如果我错了。

I am assuming by "pseudo-design mode" you do mean that your application is in a run-time state, and the end-user is experiencing a "virtual design mode" : please correct me if I am wrong.

不过,我假设你指的是设计时的控件锁定属性,您希望效仿这在运行时......是否正确?

But, I am assuming you are referring to the design-time 'Locked property of controls, and that you wish to "emulate" this at run-time ... correct ?

我也假设你正在连接鼠标上/下/移动处理器的控制你就可以四处走动,可能是通过所有循环,或者,控件在窗体上的一个子集(或集合你维护的允许移动)控制。

I'm also assuming you are attaching mouse up/down/move handlers to the controls you do allow to move around, probably by looping through all, or a subset of, the controls on the form (or a collection you are maintaining of controls allowed to be moved).

如果我的假设是正确的,我会去删除事件处理程序,使移动时,你需要禁用控制运动,然后还原这些事件处理程序,当你需要允许控制再次被感动。

If my assumptions are correct, I would go for removing the event handlers that enable moving when you need to disable control movement, then restoring those event handlers when you need to allow controls to be moved again.

一个主要的原因是它是,恕我直言,最佳实践来控制事件 - 严格处理(就地可处置的对象干扰离开事件处理......尽管这可能在没办法,适用于在这里您的方案)。

One main reason being that it is, imho, "best practice" to control event-handling rigorously (leaving event handlers "in-place" can interfere with object disposal ... although that may, in no way, apply to your scenario here).

一更多的想法:你有一个看不见的面板停靠填写表单:此面板上是可以移动的所有控件:这可能让你更轻松地将目标锁定关于控制你花这一额外的代码上。在使用这种方法的缺点是通常:

One more idea : you have an "invisible" Panel docked 'fill to the Form : on this panel are all controls that can be moved : this may allow you to more easily "narrow your focus" on which controls you "spend" this extra code on. The drawbacks in using this approach are usually :


  1. 如果您使用hostingForm.ActiveControl确定哪些控制得到了鼠标按下(并且,因此,然后可以移动):你会发现一些控件,比如标签和pictureboxes,点击时不会成为形式的ACTIVECONTROL,但大多数人

  1. if you use hostingForm.ActiveControl to determine which control got the mousedown (and, thus, can then be moved) : you'll find some controls, like labels, and pictureboxes, do not become the activecontrol of the form when clicked, but most do.

你有一个Z顺序的事情要考虑,因为控制不是在你的面板封装你希望的控件允许伪透明面板将被隐藏背后来移动发送。

you have a "z-order" thing to think about since a control not in your panel encapsulating the controls you wish to allow to move sent behind the pseudo-transparent panel will be hidden.

由于这些原因,恕我直言,我认为禁用和重新启用的事件处理程序的附件是最好,最简单,因为这是可以做到的时候控制是向下转换,以他们的控制身份:

For these reasons, imho, I think disabling and re-enabling event handler attachments is best, most simple, and since it can be done when the controls are "down-cast" to their control "identity" :

private void enableControlsMove()
{
    foreach (Control theControl in panel1.Controls)
    {
        Console.WriteLine(theControl.Name);

        theControl.MouseDown += new MouseEventHandler(theControl_MouseDown);
        theControl.MouseUp += new MouseEventHandler(theControl_MouseUp);
        theControl.MouseMove += new MouseEventHandler(theControl_MouseMove);
    }
}

private void disableControlsMove()
{
    foreach (Control theControl in panel1.Controls)
    {
        Console.WriteLine(theControl.Name);

        theControl.MouseDown -= theControl_MouseDown;
        theControl.MouseUp -= theControl_MouseUp;
        theControl.MouseMove -= theControl_MouseMove;
    }
}



我用这种方式。

I use it this way.

最好的,比尔

这篇关于C#WinForm的锁控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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