VS2010 designer window is executing my form - c#

Am I crazy or is this normal operation. Visual Studio 2010 SP1.
I have a .NET 3.5 (C#) base form with a few inherited classes. Whenever I open the inherited form in designer view it actually starts running my code... I'm not even executing or debugging.
Example:
I need my form to start a named pipe server which is created in the FormLoad event. Whenever I open the form in designer view, it actually starts up the pipe server.
Maybe I just don't fully understand how designer view works?
Edit: I know it's actually running because I'm trying to debug my pipe code and every time I open the designer window, it starts throwing pipe exceptions at me. I can also see through process explorer that devenv.exe is starting up my pipe instances when I'm not debugging.

Yes, the designer is actually "running" your form.
If you want to avoid this, just put your critical code inside an if block like so:
if(!this.DesignMode) //this is the Form
{
....
}
Cheers

Related

Visual Studio - End debugger only when all forms have been closed

I'm working in Visual Studio 2013 Premium on a Windows forms project. When I'm debugging my windows forms project and I close my start up form, all other forms stop too. I googled this problem and saw that in project --> properties there is a drop down list where you're able to change shutdown mode, which would solve my problem.
The shutdown mode option however is not present at my application properties. I can't find it anywhere else and searching the web didn't resolve this problem for me either, so what could it be?
That only applies to Visual Basic projects.
Here's your options:
For Visual Basic projects, twiddle the option you found on Google
For C# WPF projects, you can set the Application.ShutdownMode property
For C# WinForms projects, you must either:
Accept that it will close when the main form closes
Prevent the main form from closing until all other forms have closed
The Application.OpenForms Property should be helpful to you:
Definition:
Gets a collection of open forms owned by the application.
Example:
In the closing event of the main application you can put following code that will check if there's more than 1 form opened.
var openForms = Application.OpenForms;
if(openForms.Count > 1)
{
//More than 1 form is opened: cancel the close-event
}
else
{
}

Debug a WPF window or Windows Form without running your application?

I'm new to WPF and have a question which quite same with the article: "Walkthrough: Debug a WPF window or Windows Form without running your application".
The link: http://blogs.msdn.com/b/habibh/archive/2009/07/17/walkthrough-debug-a-wpf-window-or-windows-form-without-running-your-application-video.aspx
I will take the picture as an example:
http://blogs.msdn.com/blogfiles/habibh/WindowsLiveWriter/DebugaWPFwindoworWindowsFormwithoutrunni_FD38/image_3.png
From the picture, the project have 4 WPF windows such as:
App.xaml
Create...so on.xaml
MainWindows.xaml
ViewChart... so on.xaml
When every time in VS 2010, I click on the starting debugging (F5) or start without debugging(Ctrl + F5) will always run the MainWindows.xaml. What the reason coz this? Is this because of in the App.xaml that we declare StartupUri="MainWindow.xaml".
Can I run the particular WPF windows such number 4. ViewChart... so on.xaml instead of the whole application(*Such as the MainWindows.xaml)?
The article you linked is out of date and the video that demonstrates his solution is no longer hosted. However, the basic steps that were outlined in the video are still possible, though the process can be more trouble than it's worth.
The process is as follows:
Open the Immediate Debugger window (Debug -> Windows -> Immediate Window)
In the Immediate mode, create an instance of the window you are wishing to test.
call the ShowDialog() method on your Immediate instance.
The main trouble with this method of debugging is that if your window has a complex constructor or has dependencies on external objects, you may end up spending more time setting up the dependencies than just navigating to the page would have taken.

Recover code deleted by Visual C# Form Designer?

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.

c# form losing focus

When i run my application with visual studio IDE, the application runs fine. When i run directly the executable it is not running fine.
My app creates two forms. First form losts focus when the second form is about to create. The first form UI frezzes when i run directly executable.
Please give any suggestions how to debug the problem.
Regards
Raju
Setting the First form property Topmost to true solved the prolem.

VS 2010 - Error when opening User Control / Form with 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.

Categories