简体   繁体   English

如何使Visual Studio的Windows窗体设计器不会删除控件?

[英]How do I keep Visual Studio's Windows Forms Designer from deleting controls?

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 (eg the form's size is suddenly changed by a few pixels), and controls get deleted. 有了我的几种形式,我偶尔会遇到以下问题:我使用设计器编辑表单(Visual Studio 2008,Windows Forms,.NET 2.0,VB.NET)来添加组件,但后来才发现一些小的调整制作(例如,表格的大小突然改变几个像素),控件被删除。 This happens silently — event-handling methods automatically have their Handles suffix removed, too, so they never get called, and there's no compiler error. 这是静默发生的 - 事件处理方法也会自动删除其Handles后缀,因此它们永远不会被调用,并且没有编译器错误。 I only notice much later or not at all, because I'm working on a different area in the form. 我之后才会注意到或者根本没有注意到,因为我正在处理表单中的其他区域。

As an example, I have a form with a SplitContainer containing an Infragistics UltraListView to the left, and an UltraTabControl to the right. 作为一个例子,我有一个包含SplitContainer的表单,左边是Infragistics UltraListView ,右边是UltraTabControl 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. 我后来发现列表视图的滚动条突然不可见,因为它的大小已经关闭,并且至少有一个控件从我没有处理的不同选项卡中删除。

Is this a known issue with the WinForms Designer, or with Infragistics? 这是WinForms Designer或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? 有没有充分的理由发生这种情况?

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. 一个线索是,被删除的控件可能具有期望在运行时而不是设计时运行的代码(例如Load事件处理程序),并且可能抛出异常。 Could this cause Visual Studio to remove the control? 这会导致Visual Studio删除控件吗?

This is not an answer to your question, but remember that you should use "DesignMode" in loads. 这不是您的问题的答案,但请记住,您应该在加载中使用“DesignMode”。 This avoids weird unexpected behaviors during design time. 这避免了在设计时出现奇怪的意外行为。

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

or 要么

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

If you can reproduce the problem, then you can determine whether or not the cause is an exception. 如果您可以重现问题,那么您可以确定原因是否是异常。 Start a second instance of Visual Studio and use "Tools->Attach to Process" to attach to the first one. 启动Visual Studio的第二个实例,并使用“工具 - >附加到进程”附加到第一个实例。 Then proceed to reproduce the problem, and the code should break when the unhandled exception is thrown. 然后继续重现问题,并且在抛出未处理的异常时代码应该中断。 Information about other exceptions (possibly handled) will be visible in the Output window. 有关其他异常(可能已处理)的信息将在“输出”窗口中显示。

It is not a real answer for you, but here are some more pointers I've found helpful.. 这不是一个真正的答案,但这里有一些我发现有帮助的指针..

A) See @Daniel Dolz's answer. A)参见@Daniel Dolz的回答。 I wholeheartedly recommend you ALWAYS do what he's recommending except when you avoid putting non-UI code (or any code) in Form_Load altogether. 我全心全意地建议你总是做他推荐的事情,除非你完全避免在Form_Load放入非UI代码(或任何代码)。 This actually solves a lot of problems, but especially those exceptions popping up in designers and slow designer performance. 这实际上解决了很多问题,但尤其是那些在设计师中出现的异常和设计师的性能降低。

B) As much as humanly possible: B)尽可能多的人:

Never edit the designer.cs in Windows Forms . 切勿编辑Windows窗体中的designer.cs

OK, sometimes you have to, but it's not someplace to do regular development. 好吧,有时候你必须这样做,但这不是定期开发的地方。 Third-party controls will have bugs that require you to do so, but it should be an exceedingly rare day when you're making a direct change in the designer file. 第三方控件会出现需要您执行此操作的错误,但是当您在设计器文件中进行直接更改时,这应该是非常罕见的一天。 One way to help manage this is that if you don't want the designer automatically adjusting something, copy it over to your regular .cs file. 帮助管理这一点的一种方法是,如果您不希望设计器自动调整某些内容,请将其复制到常规.cs文件中。 That doesn't cover everything, but keep it in mind. 这并不涵盖所有内容,但要记住它。

C) Also, do you know you can Lock the layout via the IDE to prevent stray mouse clicks from goofing everything up on you? C)另外,你知道你可以通过IDE 锁定布局,以防止鼠标点击鼠标乱搞你的一切吗? This will preempt some of these "automatic updates" you're running into. 这将抢占您遇到的一些“自动更新”。

Parting Shot I worked on a large Windows Forms project for more than two years immediately prior to my current project, and all I can say is until we put an end to people editing the designers except for very minor edits and required them to put in the DesignMode checks, we had nothing but problems. Parting Shot在我目前的项目之前,我在一个大型的Windows Forms项目上工作了两年多,我只能说,直到我们结束编辑设计师的人,除了非常小的编辑并要求他们放入DesignMode检查,我们只有问题。 Coming off of web projects, we have a hard time with this idea because we're so used to editing markup, but the designer.cs isn't the same as markup. 关于Web项目,我们很难有这个想法,因为我们习惯于编辑标记,但designer.cs与标记不同。

I've found the DesignMode property sometimes produces an unexpected result (Daniel's answer). 我发现DesignMode属性有时会产生意想不到的结果(Daniel的回答)。 Below is a property I have on my base control class (most controls I created inherit from this), the comments should indicate the 'reason' for what looks like hacky code, but essentially this is much more reliable than DesignMode on its own. 下面是我在我的基本控件类上的属性(我创建的大多数控件继承自此),注释应该指示看起来像hacky代码的“原因”,但实际上这比自己的DesignMode更可靠。

    /// <summary>
    /// Indicates if the current view is being utilized in the VS.NET IDE or not.
    /// </summary>
    /// <remarks>The DesignMode property for a UserControl object will show that it is in DesignMode
    /// only if the immediate parent is viewed in the IDE; if it is a grand child of the object that is being viewed in the IDE,
    /// then the DesignMode property will not be true.
    /// This is a workaround</remarks>
    public bool InDesignMode
    {
        get
        {
            // Site.Design mode sometimes produces a better result.
            if (DesignMode || Site != null && Site.DesignMode)
                return true;
            Control parent = Parent;
            while (parent != null)
            {
                if (parent.Site != null && parent.Site.DesignMode)
                    return true;
                parent = parent.Parent;
            }

            // Note: I am not 100% sure about this one; I need to double check
            // if in design mode then entryAssembly will be null. This check is 
            // needed because DesignMode property is only true for the control
            // that is actively being designed, so child controls will be false...
            // We do check the Parent heirarchy in InDesignMode but in some 
            // cases Parent will not be set before the check is required.
            var entryAssembly = Assembly.GetEntryAssembly();
            if (entryAssembly == null)
                return true;

            return false;
        }
    }

I've seen this happen with Visual Basic 6.0 and early .NET when the OS was having problems. 我已经看到,当操作系统出现问题时, Visual Basic 6.0和早期的.NET会发生这种情况。 We had to do a reboot and repair. 我们不得不重新启动并修复。

Another piece of good practice, do not delete default form constructors. 另一个好习惯,不要删除默认的表单构造函数。 IDE uses them, even when your logic do not. IDE使用它们,即使您的逻辑没有。 Locking is also useful. 锁定也很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Visual Studio Designer将Table窗体控件置于TableLayoutPanel中 - Centering Windows Forms Controls inside TableLayoutPanel with Visual Studio Designer Windows Forms 中控件的位置和大小在 Visual Studio Designer 和编程实例化之间有所不同 - Positions and sizes of controls in Windows Forms differ between Visual Studio Designer and programmatic instantiation 创建使您可以从Visual Studio Forms Designer中选择当前表单中其他控件的下拉菜单 - Create dropdowns that let you select other controls in the current form from the Visual Studio Forms Designer 如何阻止Visual Studio重命名我的控件? - How do I prevent Visual Studio from renaming my controls? 如何调试Windows窗体设计器崩溃? - How do I debug Windows Forms Designer crashing? Windows Forms Designer-如何防止删除子控件 - Windows Forms designer - How to prevent deletion of child controls 如何防止 Visual Studio 将文件视为组件或 forms - How to keep Visual Studio from treating files as components or forms 如何在WPF Visual Studio设计器中锁定控件以避免意外更改? - How to lock controls in WPF Visual Studio designer to avoid accidental changes? 如何在Visual Studio 2010中向Windows窗体应用程序添加用户控件? - How do I add a user control to a Windows Forms application in Visual Studio 2010? 如何在 Visual Studio 2019 中的 Windows Forms 应用程序中打印变量? - How do I print a variable in a Windows Forms Application in Visual Studio 2019?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM