即使代码存在,菜单条也不会在表单设计器中显示 [英] Menu strip is not shown in form designer, even though its code exists

查看:65
本文介绍了即使代码存在,菜单条也不会在表单设计器中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚,我正在通过visual studio 2012在C#中开展我的项目。突然间,我遇到了一些来自visual studio的错误,然后菜单条进入隐藏状态。现在我的表单中没有菜单条,我丢失了所有可视选项,但是我的`formdesigner.cs`文件中包含了所有代码。我不能再做所有选项,因为它很难且耗时,我必须用新名称创建菜单条。并用新名称创建所有子项。如何恢复丢失的菜单条形成?

这是我的设计师代码的一部分:

  this  .Main =  new  System.Windows.Forms.ToolStripMenuItem(); 
this .userOptionTtm = new System.Windows.Forms.ToolStripMenuItem();




我拥有菜单的所有子项的属性,我之前创建(或定义)了。例如:

  //    
// Main
//
this .Main.DropDownItems.AddRange( new System.Windows.Forms.ToolStripItem [] {
this .userOptionTtm,
this .CalenderOption,
this .ExitTsm});
this .Main.Font = new System.Drawing.Font( Segoe UI,9F);
this .Main.Name = Main ;
this .Main.Size = new System.Drawing.Size( 62 20 );
this .Main.Text = تنظیمات ;





,在我的表单代码中,我有这个菜单的所有代码。例如:

  private   void  FeutureJobsTSM_Click ( object  sender,EventArgs e)
{
FeutureReportForm.isJobs = true ;
FeutureReportForm fr = new FeutureReportForm();
fr.ShowDialog( this );
}



请注意:

没有相同的代码:

< pre lang =c#> .component。 add this .Main);



当我添加此行时,发生此错误:

对象引用未设置为实例一个对象。

看看这张图 />
当我构建应用程序时发生此异常。

如果我添加此行:

  this  .controls。 add  this  .Main); 



我在列表中看到2个错误错误:

错误1 :'System.Windows.Forms的最佳重载方法匹配.Control.ControlCollection.Add(System.Wi 'nms.Forms.Control)'有一些无效的参数

错误2:参数1:无法从'System.Windows.Forms.ToolStripMenuItem'转换为'System.Windows。 Forms.Control'

这是关于菜单条(主要)的设计师代码:

  .Main =  new  System.Windows.Forms.ToolStripMenuItem(); 



以及定义子菜单的一些代码。我很惊讶,为什么Form.cs [Design]窗口中我的表单下不存在菜单条图标。

看看这张图片




这个

解决方案

它可能缺少

  this  .Controls.Add( this  .Main); 

某处。确保将菜单添加到控件或表单上的组件集合我很确定它在组件集合中)。编辑:这是Controls集合(感谢Bill)。


有时WinForms应用程序被破坏,您无法获得Form或UserControl的设计时视图正确的,但正如你在这里展示的那样,你看起来像是一个有效的Designer.cs文件,以及有效的代码隐藏页面。



我发现这个策略有用:



1.立即制作整个申请表的备份副本。



2关闭项目/解决方案,并尝试重新打开它:如果这不起作用,退出Visual Studio,重新打开它,然后重新加载项目/解决方案:如果这不起作用,那么:



3.创建一个新的VS解决方案,并且......在这种情况下......在其上放一个MenuStrip,然后复制Designer.cs文件,关闭解决方案,然后粘贴Designer.cs文件副本在文本编辑器中:仔细比较它的结构与您知道不适合您的Designer.cs文件的结构(没有给出正确的des点燃时间观点)。如果你很幸运,你可能会发现一些关键的区别,可以修复的差异。但是,如果失败了:



4.您可以尝试逐步修改备份副本,直到您有一个可用的应用程序,然后尝试重新仅创建应用程序中拒绝为您提供所期望的设计时视图的那些部分。或者:如果不起作用:



5.创建一个新项目/解决方案:重新创建原始项目中的表单,用户控件等/解决方案:在Forms或UserControls上删除相同的控件。确保NameSpaces,表单名称等与您尝试抢救的项目/解决方案完全相同。



打开Forms的Designer.cs文件或UserControls在新项目中,并粘贴保存的代码。打开UserControls和Forms的代码隐藏页面,并粘贴保存的代码。



虽然#5是一个极端的措施,它对我来说很少见我必须这样做。



希望如果你有类文件(所有代码,没有GUI),那么这些文件可以保持不变并轻松恢复。


祝你好运!


Bill的建议(解决方案2)的一些补充。



  • 而不是备份,它应该是一些修订控制系统的使用,以小步骤提交,以便能够通过任意数量的步骤追溯。这些天,不使用修订控制系统是对你自己的犯罪。



    请参阅:

    修改控制系统,可供选择? [ ^ ],

    需求wisedom设置和/或使用服务器的一些话 [ ^ ]
  • 不要过度使用设计师。有些人手动添加了几十个文本框。这很乏味且容易出错。所有定期工作都应该在代码中完成。例如,如果您需要和文本数组装箱,请首先使用数组,而不是单个控件。然后你就可以使用循环。
  • 永远不要使用自动生成的名称。他们违反(好)微软命名,这甚至是好的:它告诉你你还没有使用什么控件。当你使用时,总是给它们所有语义敏感的名字。您如何看待,重构引擎是什么?
  • 添加更多具有相同表单类(或用户控件)的部分声明的文件。但是有一个问题:点击后,愚蠢的设计师会打开一个不同的不存在的表单。它不会伤害太多,但会令人困惑。我自己的诀窍是:为名称添加.designer。例如,您有表单文件MyForm.cs。在名为MyForm.Setup.designer.cs,MyForm.Actions.designer.cs,MyForm.Threds.designer.cs的文件中添加新的部分声明。它的工作原理非常方便。
  • 如果你迷路了,只是一点点,不要害怕去自动生成的文件(与问题评论中的sjelen建议相反) )。例如,这是删除一些难以找到并在设计器下单击的错误放置控件的最简单方法。
  • 使用 if(DesignMode){DoSomethingComplex(); } 。它将保护您的设计师不会显示表单。在开发过程中,您可能会犯一些错误,并且会使您的表单无法显示。在 DesignMode 的检查下进行:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx [ ^ ]。
  • 并且,最后,我采用的方式是不要过度使用设计师。请注意,有些成员批评我的方法,所以自己决定。



    我的规则是:永远不要在设计师中添加任何事件处理程序。原因如下:如果使用匿名语法,在代码中编写处理程序更易于维护(在单独的部分声明部分,单独的文件中):

     myControl。 MouseDown + =(sender,eventArgs)= >  {
    DoSomething(eventArgs.X,eventArgs.Y);
    };

    // 或者,适用于C#v.2:

    myControl.MouseDown + = 委托对象发​​件人,MouseEventArgs eventArgs){
    DoSomething(eventArgs.X,eventArgs.Y);
    };





    除了其他好处之外,它还将委托与一些语义处理程序方法分离,减少了未使用参数的传递并且把所有代码都放在你的控制之下。

  • 祝你好运,

    -SA


Last night, I was working on my project in C# by visual studio 2012. Suddenly I encountered a few errors from visual studio and then menu strip went into hiding. Now I haven't menu strip in my form and I lost all it visual option, but I have all it code in my `formdesigner.cs` file. I can't make all option again because it is hard and Time-consuming and I must create a menu strip by new names.and create all sub items by new names.How I can resume my lost menu strip to form?
This is a part of my designer code:

this.Main = new System.Windows.Forms.ToolStripMenuItem();
this.userOptionTtm = new System.Windows.Forms.ToolStripMenuItem();
.
.
.


and i have properties for all sub items of menu , that i was created (or defined ) previously. for example :

//
// Main
//
this.Main.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.userOptionTtm,
this.CalenderOption,
this.ExitTsm});
this.Main.Font = new System.Drawing.Font("Segoe UI", 9F);
this.Main.Name = "Main";
this.Main.Size = new System.Drawing.Size(62, 20);
this.Main.Text = "تنظیمات";
.
.
.


and in my form code, I have all code of this menu. for example:

private void FeutureJobsTSM_Click(object sender, EventArgs e)
{
    FeutureReportForm.isJobs = true;
    FeutureReportForm fr = new FeutureReportForm();
    fr.ShowDialog(this);
}


please attention :
There is no code same :

this.component.add(this.Main);


When i add this line , This error occurred :
Object reference not set to an instance of an object.
Look at this picture
this exception occurred when I build App.
If i add this line :

this.controls.add(this.Main);


I see 2 Errors in list error :
Error 1 : The best overloaded method match for 'System.Windows.Forms.Control.ControlCollection.Add(System.Windows.Forms.Control)' has some invalid arguments
Error 2 : Argument 1: cannot convert from 'System.Windows.Forms.ToolStripMenuItem' to 'System.Windows.Forms.Control'
This is my Designer code about Menu strip (Main):

this.Main = new System.Windows.Forms.ToolStripMenuItem();


And some code for define sub Menus. I'm surprised that Why don't exist menu strip icon under my form in Form.cs[Design] windows.
Look at this picture


And this

解决方案

It is probably missing a

this.Controls.Add(this.Main);

somewhere. Make sure that the menu is being added to the controls or components collection on the form (I'm pretty sure it goes in the Components collection). Edit: It is the Controls collection (thanks Bill).


Sometimes a WinForms app gets corrupted, and you can't get the design-time view of a Form, or UserControl, to appear correctly, but you do, as you show here, have what looks like a valid Designer.cs file, and valid code-behind pages.

I've found this strategy useful:

1. immediately make a back-up copy of the whole Application.

2. close the Project/Solution, and try re-opening it: if that doesn't work, quit Visual Studio, re-open it, and reload the Project/Solution: if that doesn't work then:

3. create a new VS Solution, and ... in this case ... drop a MenuStrip on it, then copy the Designer.cs file, and close the Solution, and paste the Designer.cs file copy in a text editor: compare its structure carefully with the structure of the Designer.cs file that you know is not working for you (not giving a proper design-time view). If you're lucky, you just might find some key difference, a difference that can be fixed. But, if that fails:

4. you could try modifying your back-up copy step-by-step until you have a working app, and then try re-creating only those parts of your app that refuse to give you the design-time view you expect. or: if that doesn't work:

5. create a new Project/Solution: re-create the Forms, UserControls, etc. you had in the original Project/Solution: drop the same Controls on the Forms or UserControls. Make sure NameSpaces, Form Names, etc. are all the same as the Project/Solution you are trying to salvage.

Open the Designer.cs files of the Forms or UserControls in the new Project, and paste in the saved code. open the code-behind pages for UserControls, and Forms, and paste in the saved code.

While #5 is an extreme measure, it has worked for me the few times I've had to do it.

Hopefully, if you have Class files (all code, no GUI), those can be left untouched and restored easily.

good luck !


Some addition to the advice by Bill (Solution 2).

  • Instead of back-up, it should be a use of some Revision Control System, committing in small steps, to be able to trace back back by any number of steps. These days, not using Revision Control System is a crime against yourself.

    Please see:
    Revision control systems, which to choose from?[^],
    Needs some words of wisedom to set up and/or use a server[^]
  • Don't overuse the designer. Some add tens of text boxes manually. This is tedious and error-prone. All periodic work should be done in code. For example, if you need and array of text boxed, use array in first place, not individual controls. Then you will be able to use loops.
  • Never use auto-generated names. They violate (good) Microsoft naming, which is even good: it tells you what controls you did not put to use yet. When you put then to use, always give them all semantically sensitive names. How do you think, what is the refactoring engine for?
  • Add more files with partial declarations of the same form class (or user control, for that matter). There is a problem, though: on click, stupid designer will open a different non-existing "form". It won't hurt much but is confusing. My own trick is: add ".designer" for the name. For example, you have the form file "MyForm.cs". Add a new partial declaration in a file named like "MyForm.Setup.designer.cs", "MyForm.Actions.designer.cs", "MyForm.Threds.designer.cs". It works and is very convenient.
  • If you are lost, but just a bit, don't be afraid to go to auto-generated file (do the opposite to what sjelen advices in a comment to the question). For example, this is the easiest way to remove some incorrectly placed control which is hard to find and click in it under the designer.
  • Use if (DesignMode) { DoSomethingComplex(); }. It will protect your designer from not showing a form. During development, you can make some mistake, and it will render your form not showing. Do it under the check of DesignMode:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].
  • And, finally, my very own approach in the style of "don't overuse the designer". Note that some members criticized my approach, so decide by yourself.

    My rule is: "never add any event handlers in the designer". Here is why: writing the handler in code is way more maintainable (in a separate partial declaration part, a separate file), if you use anonymous syntax:

    myControl.MouseDown += (sender, eventArgs) => {
        DoSomething(eventArgs.X, eventArgs.Y);
    };
    
    // or, good for C# v.2:
    
    myControl.MouseDown += delegate(object sender, MouseEventArgs eventArgs) {
        DoSomething(eventArgs.X, eventArgs.Y);
    };



    Among other benefits, it decouples delegate from some "semantic" handler methods, reducing passing of the unused arguments and putting all code under your control.

  • Good luck,
    —SA


这篇关于即使代码存在,菜单条也不会在表单设计器中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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