Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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 :
C#
//
// 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:
C#
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 :
C#
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 :
C#
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):
C#
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
Posted
Updated 25-Oct-13 11:49am
v5
Comments
sjelen 25-Oct-13 10:22am    
Have you modified Designer.cs file manually? That's never a good idea.
Do you have any compiler errors when you build app?
Are you able to run app? Is menustrip visible when you run it?
Sergey Alexandrovich Kryukov 25-Oct-13 10:37am    
There is nothing wrong in modifying auto-generated files manually, because, in fact, this is the only Source of Truth. In certain cases (refactoring, cleaning after some wrong move), it can be a very useful technique.

What, have you been effected by Microsoft propaganda? But for them, its natural. :-)

And for OP, this is just a way to ask a question...

—SA
sjelen 25-Oct-13 11:35am    
OK, 'never' is maybe too strong.
But you never know when will Designer decide to re-gen the file and overwrite manual changes.
Sergey Alexandrovich Kryukov 25-Oct-13 11:58am    
You are perfectly right if you meant that a user should not do whole development on the auto-generated code. But you, see, I had only one developer who did something similar, but that person was extremely stupid and acted in a stupid way under my pressure, unfortunately. This is a very good idea to move a lot of development out of auto-generated files, but this is opposite to using those files. I just say that re-touching auto-generated files can often be very efficient and robust. Please see my answer where I explain some techniques.
Majid Yaghoubi 25-Oct-13 16:43pm    
No , There is no error when I build App and My App is run Properly . But Menu strip is not visible . even menu strip is not under my form in Form.cs[Design] windows.

It is probably missing a
C#
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).
 
Share this answer
 
v2
Comments
BillWoodruff 25-Oct-13 9:01am    
In Visual Studio 2012, in a WinForms Project, a MenuStrip added to a Form is added to the ControlCollection of the Form in the Designer.cs Class file. Only when you add Components to a WinForms project is the System.ComponentModel.IContainer components instance set to anything but 'null in the Designer.cs Class.

The general idea you are suggesting: to examine the Designer.cs file for structural problems compared to a "known good" Designer.cs file is absolutely on-target.
Ron Beyer 25-Oct-13 10:28am    
Thanks, I couldn't remember and I'm not on my dev machine to whip up a quick test. I was confused because adding a menu (maybe the old menu) put an item in the component tray.
Majid Yaghoubi 25-Oct-13 9:21am    
thx , but where should i write this ?
I add it in Designer.cs but i get this exception :
Object reference not set to an instance of an object.
Ron Beyer 25-Oct-13 11:15am    
It should go at the bottom of the initialization, you should see a bunch of other similar looking ones.
Sergey Alexandrovich Kryukov 25-Oct-13 11:04am    
This is one of the typical mistakes, 5ed.
For general approach, please see the advice by Bill and myself (inspired by Bill's post). I found a number of little techniques to fix designer-generated problems quickly and prevent them.
—SA
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 !
 
Share this answer
 
Comments
Majid Yaghoubi 25-Oct-13 10:47am    
no, unfortunately its not work !
Sergey Alexandrovich Kryukov 25-Oct-13 11:01am    
What doesn't work? A good advice? Then you screwed up something. :-)
—SA
Sergey Alexandrovich Kryukov 25-Oct-13 11:02am    
My 5. Please see, I added some more.
(Already sorry about that: OP is apparently too focused on the present situation, does not want to listed to good advice :-( )
—SA
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:
    C#
    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
 
Share this answer
 
v2
thx everybody. I added these lines to Form.Designer.cs file and my problem fixed.

C#
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
 this.menuStrip1.BackColor = System.Drawing.Color.Transparent;
 this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] 
  { this.Main,this.ReportsTSM });
 this.menuStrip1.Location = new System.Drawing.Point(0, 0);
 this.menuStrip1.Name = "menuStrip1";
 this.menuStrip1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
 this.menuStrip1.Size = new System.Drawing.Size(589, 24);
 this.menuStrip1.TabIndex = 10;
 this.menuStrip1.Text = "menuStrip1";
 this.menuStrip1.ResumeLayout(false);
 this.menuStrip1.PerformLayout();

 this.Controls.Add(this.menuStrip1);

 private System.Windows.Forms.MenuStrip menuStrip1;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900