So, I made a program in Visual C# 2010 Express. Finished enough to work, and pretty darned good for my first attempt at the language. But then I foolishly decided, "Hey, this program needs a close confirmation dialog before I send it out!" So I write the function, and then I go into the auto-generated code to bind said function appropriately.
KABOOM!
Now, the form designer shows a blank form, and all those shiny components have all been erased from both the designer and the auto-generated code. Which, in such a simple program, is about 80% of the work I put into it.
Now, in NetBeans, I would right-click the file's tab, and just go back to a previous version, maybe losing about 15 minutes of work. This is a pretty obvious concept, I'm just not sure where to find file history in Visual Studio. So, where is the equivalent so I can get my work back?
(I've also managed to get my code back by just undoing a whole bunch - but the designer doesn't see it, and neither does the compiler, so it still builds to a blank form.)
Sadly, you've just learned a couple of things.
One is to never, ever modify designer-generated code. In fact, C# has a concept called partial classes which allow you to modify a generated class without having to modify the generated file.
Second, that Visual Studio in and of itself has no built in source code control -- you need to choose the one you like and be diligent in using it. Some of the most popular are git, subversion, mercurial, and Team Foundation Server. I personally use svn, but the others are good too. Avoid something called Visual SourceSafe (VSS).
From the sound of it, I'd say you tried ctrl-z until some point in your code, but not in the designer?
Performing multiple "undo's" in the code-behind file where you put your code is one thing, performing "undo's" in the designer is another.
Hopefully, you have not closed visual studio yet, and viewing your now blank form in design view, and trying a bunch of ctrl-z presses there, might do the trick.
Actually the controls are not lost from Design code or Initialize
Component() ,make sure that whether check
Initialize Component() { this.controls.add("Where the control was ")
}
Example:
1-this.Controls.Add(this.panel1.label1); 2-this.Controls.Add(this.panel1.label2);
either in form you can write like below
should write in the Initialize Component()
this.Controls.Add(this.label1);
I had a similar effect: the code wasn't lost because the project compiled perfectly but I only got a blank form in designer.
This was because the form was inherited from another form, and I added in the Load event of the base form the line: this.MdiParent = Application.OpenForms("BaseForm");
After deleting this line (and recompiling) I could access the design of the descendig forms again.
One thing you could do, it won't help you this time, but may be a good idea for the future - if you copy your code alot (like I do), get one of those clipboard savers - like ClipTrap (which is lightweight, simple, and great).
Then, if you realize you can't undo what you did, then you get a second chance by running through the "trapped" text to see if what you want might just be there.
Another good option is to try something like AutoVer, which will save a copy of any changed file (or files in a folder) every so often as you like. This could provide you with a backup of your codefile, or even the entire project - every five minutes, or every minute as long as something has been changed. Awesome program. This is a certain fashion of source control, or at least source backup.
(And no I'm not the authour of either application)
However, for your particular problem, if the code is still there in your designer.cs page - maybe you just erased a reference or a namespace. Make sure the namespace matches, and make sure your in your form's code file, that the InitializeComponent(); function is being called (that's what places all the designer's controls onto the form, you know).
In the code behind file click 'undo', not in the designer window. Accept the warning, and when the code comes back, the form should be back to normal, Visual Studio 2015.
Related
I am learning to write in C# on Visual Studios and everything has been going well until today when i was trying to run my app
(using Ctrl + F5). After I tried to run my app it would not run and prompted me to save. Unfortunately I did not pay to much attention and tried to save it but an error message came up saying something like, "There already exists a file named form1.designer. Would you like to replace this file?" I was worried to replace the file so I saved it in a new location.Now when I
look at my solution explorer my original form1.designer.cs is not usable and a new form1.designer has been added.
I am no longer able to go to design view where i can edit the GUI(when i use form1.designer.cs), when i do it displays a blank form. When i try to open my design view from the lower form1.designer, it takes me to the code where the Components are initialize. The program still works perfectly when i run it, i just can't edit the GUI.
I figured it out, it was a linking problem between the form and the design GUI. I had to right click on the solution, click add, existing item, and then i had to select the item i wanted to link. In this case it was the ".cs" file.
When opening my project in Visual Studio the code- and designer files for the main form are marked as unsaved (marked with a star after the name of the files):
If I close VS again right away (without making any changes) VS asks if I want to save the changes.
If I first save all items and then close VS the same happen again on next start of VS.
So my questions are:
1: Why are some kind of changes made automatically to the two mentioned files?
2: Which changes could it be?
3: How to get rid of this behaviour, so when I start my project in VS all files appear as saved, as I have not made any changes myself upon that time?
Thanks!
You dont have posted the designer code so I can only guess that you have created the frmMain on a different machine with different graphics options (especially with diffren DPI).
If you could post your code, may be we can guide you better. Otherwise you may have a look at VS 2010 updates Designer.cs when no changes are made which explains a little bit more about this feature.
I'm trying to learn C# from some YouTube videos, but I have a few concerns. I started with JavaScript, wanted to learn a software language, so I learned some C++, but then I found out that C# is better for GUI programming, so now I'm going with that.
The problem is that I feel like VS is doing a lot of things I should know how to do but don't... When I add a button or create a new class or something it's all done with VS. I want to know what it's doing, though, so I can do it myself if I need to.
I'm on the video explaining classes, and I was wondering where the header file was. I couldn't find it, so I searched online and, come to find out, unlike C++, C# doesn't have header files. Are the class files automatically linked?
Is it practical to learn how to do everything manually, or is C# done solely in VS anyway? Is there a way to keep track of what files and code snippets are added and created when a button is pressed in VS?
I am bias, but the best way to understand and learn what is going on in a program is to debug it.
Creating a new C# app in Visual Studio will give you the fastest path to this. You shouldn't have to worry too much what Visual Studio is generating for you, because if you want to understand it, simply step through the code in the debugger. You can see exactly what calls are being made, how each of them changes state, etc, etc.
If you make a basic GUI app, then step through the InitializeComponents function, that gives you a really good sense of how VS sets things up for you.
C# can be used outside of VS, but having all the components so you can just hit F10 and starting stepping away in the IDE makes it the best choice.
Well, best way would be to not use the VS Designer, if you want to learn what happens behind the curtains. You may of course code every single line by hand and it still will work. Eg. to add a button:
Form form = new Form(); // create my form
Button button = new Button(); // create the button
button.Text = "Click me!"; // add some text
form.Controls.Add(button); // add the button to the form
Application.Run(form); // start the applications main thread and display the button
There are dozens of tutorials outside, just find the one that suits you best.
The thing is that visual studio is giving you all the preregistered stuff in built. So you just need to build your own class templates the definition will be presented to you default.
So if you create a class call employee it will create basic syntax for you and you need to write the logic directly.
Thanks,
Jigar
C# as a language is used to develop several kinds of applications such as console-based applications, Windows desktop applications and Web applications.
For the former, you will code everything mostly by hand, even if you are using Visual Studio.
For the latter two, since they are GUI based, it's recommended to use Visual Studio so that you can use the drag-and-drop approach as well as have Visual Studio take care of the routine tasks so that you can focus on the logic and actual development.
IMO, you shouldn't try to learn everythin, but anyway - core concepts will broaden your understanding and will surely help you in further dev-life.
If you want what exactly VS do with your code - put some "VCS guard" at top of project. Use Git or Mercurial and you could see exact changes that was applied to each file (commit everything before you going perform interesting action and check changes after). It could help you to track anything that will happens with your project. But for most cases its not very productive to do everything by hands. DevTools were made to remove very bored routine.
Its very useful to know how you can do something without VS installed, but all such tasks is usually not directly related to programming, mainly its some kind of supporting stuff.
Ex. how build a project with msbuild or csc.exe (C# compiler) to setup homebrew continuous integration system.
If you want really deep grasp into .NET - read CLR via C# by Jeffrey Richter. IMO it's far more important to know system guts than to know how to place button on the form without VS-designer.
C#, VS2010, WinForm application:
Sometimes I do have the problem that I get an error message when opening some of my controls / forms. All code compiles and the application runs properly. Opening the control in the designer gives me:
The designer loader did not provide a root component but has not indicated why.
From my experience I can tell, it is usually something in my code which does not get properly initialized, e.g. a property is not set which is somehow available at runtime, but not when opened with the designer. The only issue is, that the root cause is hard to find.
Q: Is there a chance to somehow use the debugger when the designer opens my component in Visual Studio 2010? That would help a lot and the problem tackling is most likely a matter of minutes then.
Remark: Just to make this clear, I know how to use the debugger ;-), I only have no idea how I could tell VS2010's designer to open my control in Debug mode.
As of 2nd SEP 2010 added:
Thanks for your help. Basically it is the MSDN Library article describing how to do it.
I have managed to set it up and run the second instance
(there was not much to understand how to do it).
It only fails in my case, because the 2nd VS2010 instance (debugging Design Time) fails to find the
symbols for my custom control. I have added the symbols manually under Debugging / symbols. No
result, still "Breakpoint won't be hit because of missing symbols"
BTW, using this approach it is helpful to exclude some stuff from the symbol loading (via modules window), because this will safe a lot of time.
Now if have to figure out how to get the symbols resolved and then I can tackle the issue.....
The vast majority of design time problems with custom controls are caused by code in the event handlers or method overrides in your control running at design time as well as run time. That's normally desirable, you get instant feedback when you change a property in the Properties window for example.
But not desirable when the code depends on something that's available at runtime but not design time. Like a dbase connection or a file that's stored in the build folder. That can generate exceptions and Visual Studio isn't very robust against handling exceptions at design time. Worst case, you can crash VS to the desktop without any diagnostic. But anything is possible.
Review the code in your control and make sure that the bits of code that should only run at runtime are wrapped like this:
if (!DesignMode) {
// etc..
}
Hard cases can be diagnosed with the debugging tips in this MSDN Library article.
To debug your control in design mode, you need two instances of Visual Studio. In the first instance, open the project which contains this control source code. In the Project Properties, Debugger, set command line which calls another Visual Studio instance (msdev? don't know exactly for VS2010 - take it from the shortcut), Then execute "Start Debugging" command. Another Visual Studio instance starts. In this instance open client project which uses your control on the form.
Please humor me as I might be crazy.
Does viewing xaml in the Visual Studio UI designer execute the code-behind file?
For the past while whenever I try to view the main xaml file of my WPF application it throws an exception, but because I've been busy with other things I never really looked into it until today. Now I notice that in the exception's call stack it's calling many of the code-behind's methods, including methods like Window_Loaded (!). It's like it's trying to run my application while I'm designing it and obviously failing.
So I'm also wondering why it does this.
I'm also wondering how exactly I'm supposed to debug this as it's doing all this behind my back.
The actual exception comes from the fact that my application uses an unmanaged dll that it tries to use but somehow can't find it for some reason.
[Edit]
I've tried moving my unmanaged DLL to Windows/System32, but I still get the same exception.
Thanks!
Yes, there are things (like constructors) that get evaluated at design time. If your code is throwing an exception in the designer you can add this:
if (DesignerProperties.GetIsInDesignMode(this))
{
return;
}