如何防止 Visual Studio 的 Windows 窗体设计器删除控件? [英] How do I keep Visual Studio's Windows Forms Designer from deleting controls?

查看:37
本文介绍了如何防止 Visual Studio 的 Windows 窗体设计器删除控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的几种表单,我偶尔会遇到以下问题:我使用设计器(Visual Studio 2008、Windows Forms、.NET 2.0、VB.NET)编辑表单以添加组件,后来才发现进行了一些小的调整(例如,表单的大小突然改变了几个像素),并且控件被删除.这会悄悄发生 — 事件处理方法也会自动删除它们的 Handles 后缀,因此它们永远不会被调用,并且没有编译器错误.我只是很晚才注意到或根本没有注意到,因为我正在处理表单中的不同区域.

With several forms of mine, I occasionally run into the following issue: I edit the form using the designer (Visual Studio 2008, Windows Forms, .NET 2.0, VB.NET) to add components, only to find out later that some minor adjustments were made (e.g. the form's size is suddenly changed by a few pixels), and controls get deleted. This happens silently — event-handling methods automatically have their Handles suffix removed, too, so they never get called, and there's no compiler error. I only notice much later or not at all, because I'm working on a different area in the form.

例如,我有一个表单,其中 SplitContainer 左侧包含 Infragistics UltraListView,右侧包含一个 UltraTabControl.我添加了一个新选项卡和其中的控件,它们运行良好.后来我发现列表视图的滚动条突然不可见,因为它的大小被关闭,并且至少有一个控件从我没有处理过的另一个选项卡中删除.

As an example, I have a form with a SplitContainer containing an Infragistics UltraListView to the left, and an UltraTabControl to the right. I added a new tab, and controls within, and they worked fine. I later on found out that the list view's scrollbar was suddenly invisible, due to its size being off, and at least one control was removed from a different tab that I hadn't been working on.

这是 WinForms Designer 或 Infragistics 的已知问题吗?当然,我使用版本控制,因此我可以比较更改并将已删除的代码合并回,但这是一个不必要的乏味过程.有没有办法避免这种情况?发生这种情况是否有充分的理由?

Is this a known issue with the WinForms Designer, or with Infragistics? I use version control, of course, so I can compare the changes and merge the deleted code back in, but it's a tedious process that shouldn't be necessary. Are there ways to avoid this? Is there a good reason for this to occur?

一个线索是,被移除的控件可能具有期望在运行时而不是设计时运行的代码(例如 Load 事件处理程序),并且可能会引发异常.这会导致 Visual Studio 删除控件吗?

One clue is that the control that was removed may have code (such as a Load event handler) that expects to be run in run time, not design time, and may be throwing an exception. Could this cause Visual Studio to remove the control?

推荐答案

这不是您问题的答案,但请记住,您应该在加载中使用DesignMode".这可以避免在设计时出现奇怪的意外行为.

This is not an answer to your question, but remember that you should use "DesignMode" in loads. This avoids weird unexpected behaviors during design time.

private void EureFormAbm_Load(object sender, System.EventArgs e)
{
    if (!DesignMode)
    {
        // Your code
    } /* if */
}

private void EureFormAbm_Load(object sender, System.EventArgs e)
{
    if (DesignMode)
        return;
    // Your code
}

这篇关于如何防止 Visual Studio 的 Windows 窗体设计器删除控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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