How to output information when debugging designer based code?
-
Friday, December 14, 2012 7:54 PM
Hi all,
If this isn't the correct forum, please let me know which one is.
I am working with Windows.Forms.Controls on forms and have some code that is there just for the designer (i.e. if (this.DesignMode)....)
Is there an easy way to debug this code? The debugger doesn't like it (at least I can't figure out how to get it active at Design time). And none of the messaging or output to console seems to work either.
So far, I have been simply filling in the contents of a textbox with information, but this is problematic.
So my questions are:
1) Is there any debugger option that allows me to step through code that is executed at design time, NOT run time.
2) Is there anyway I can have code that outputs information when in Design Time - to the Output or some other window?
3) Are there any good links to resources on the various options available when working on code that runs at design time?
4) What is the best way to have a routine that can be run at runtime and will modify my code (I assume I could use the macro option, but thought I would check on other options.)
Thanks!
me (and yes, I DO mark correct answers)
- Moved by Mark Liu-lxf Monday, December 17, 2012 6:02 AM (From:Visual Studio Class Designer)
All Replies
-
Monday, December 17, 2012 6:02 AM
Hi FletcherJ,
Thanks for you post.
Microsoft provides a special forum to deal with debug issue. For better support, I will move this thread to Visual Studio debugger forum. It will cost a little time to involve the members in this forum. I appreciate your patience.
Sorry for any inconvenience and have a nice day.
Mark Liu-lxf
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Monday, December 17, 2012 7:00 PM
Mark,
Thank you for moving it. But debugging the issues related to designer specific code vs. debugging issues related to the application code are quite different issues. What do you mean by "It will cost a little time to involve the members in this forum"?" Do I need to repost my question a number of times until I get a response?
I have to think that many people have already had to deal with this and there are some very good solutions. I just couldn't figure out what to search on to find them.
Thanks
me (and yes, I DO mark correct answers)
-
Monday, December 17, 2012 11:11 PM
>Is there an easy way to debug this code?<
Do you refer to debugging code in a block
if (this.DesignMode) { //... }for projects similar to ClockControlLibrary
Building Windows Forms Controls and Components with Rich Design-Time Features
http://msdn.microsoft.com/en-us/magazine/cc164159.aspx
Just tested on a XP3/VS2010 - instructions in section 'Debugging' of this article seem to work for me, after adjusting path to devenv.exe.With kind regards
- Proposed As Answer by Patrick Nelson - MSFTMicrosoft Employee Wednesday, December 19, 2012 2:48 AM
- Marked As Answer by Jack Zhai - MSFTMicrosoft Contingent Staff, Moderator Monday, December 24, 2012 2:59 AM
-
Tuesday, December 18, 2012 12:05 AM
Thank you, but this has nothing to do with the question.....
I have some code that runs AT DESIGN TIME. I need to know how to debug code that is execuated AT DESIGN TIME.
Using your example, I would need to know how to debug the code you show as:
///...
I can't use the debugger or use the debug.write/console.write windows (as that never shows up at design time, only run time.)
So my question is, how can I debug code that runs at design time? How can I check values, etc.?
Thank you
me (and yes, I DO mark correct answers)
-
Tuesday, December 18, 2012 1:55 AM
Sorry for misunderstanding, I thought you meant something like this:
With kind regards
(sample see link above)
-
Tuesday, December 18, 2012 2:30 AM
Hello,
That looks nice, but when I open the form in design mode, the debugger never fires even if I have a breakpoint either inside or outside the "If this.DesignMode code block.
My experience is that the breakpoint you have in the code above will NEVER break the debugger since it is inside a block that only runs at DesignTime when the debugger doesn't seem to be able to run.
Or is there some switch somewhere that I can turn on so that the breakpoint above will bring up the debugger? It certainly doesn't on my system.
Thanks
me (and yes, I DO mark correct answers)
-
Tuesday, December 18, 2012 7:30 AMModerator
Thanks for MaybeCompletelyW’s help.
Hi FletcherJ,
Sorry for my delay.
I can't use the debugger or use the debug.write/console.write windows (as that never shows up at design time, only run time.).
Not very sure whether I understand this issue correct, if you want to debug your code without use the VS debugger tool or the debug.write/console.write/debug.print, I’m afraid that it is hard.
With the VS debugger, you can break, or suspend, execution of your program to examine your code, evaluate and edit variables in your program and so on, so if possible, I suggest you try to debug your app with it.
I know it is not the solution you want to get, since you don’t want to use the VS debugger, so it is not the VS debugger issue. If possible, you could try to post this issue to the specific development forum, and maybe you could get some useful information. For example, if it is the WinForms app, you could post it here. But as my understanding, I'm afraid that the VS doesn’t support it now.
If possible, you could submit this feature request: http://visualstudio.uservoice.com/forums/121579-visual-studio. The Visual Studio product team is listening to user voice there. You can send your idea there and people can vote. If you submit this suggestion, I hope you could post that link here, I will vote it. Thanks for your understanding.
Have a nice day,
Jack Zhai [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Edited by Jack Zhai - MSFTMicrosoft Contingent Staff, Moderator Tuesday, December 18, 2012 7:31 AM
-
Tuesday, December 18, 2012 11:54 PM
Please, Please read the question.....
I LOVE the debugger. I use it ALL THE TIME. It is WONDERFULL.
I also use the design environment. It too is great. Except for one issue which should be really EASY to fix (I hope.)
I have some code that is run by the designer when the form is displayed. It does not run at run time, ONLY at design time. This is controlled by the use of this.DesignMode. If that is true, some special code runs. If it is false (i.e. we are actually running code) then something else happens.
This is NOT rocket science. It is very simple.
So here is the question.
How do I debug code that ONLY runs at design time, never at run time?
#1 The debugger only works at runtime (unless there is a setting I don't know about.)
#2 If I use console.out, debug.writeline, etc. they DO NOT output anything except during run time. At design time, I don't know of any way to output values so that I can see what is happening.
Unless you know a way around #1 and #2, it is very hard to debug the designer specific code. But I have to believe there is a way to do it or the poor folks at MS would have had a coniption fit trying to get things to work when they were developing it. But how do I get it?
Thanks
me (and yes, I DO mark correct answers)
-
Wednesday, December 19, 2012 2:43 AM
Looks like MaybeCompletelyW answered your question as I read it.
Here's my answers to your questions above:
"1) Is there any debugger option that allows me to step through code that is executed at design time, NOT run time."-> There is not an "option", but you can have one Visual Studio debug another Visual Studio to step through the code executed at design time
"2) Is there anyway I can have code that outputs information when in Design Time - to the Output or some other window?"
-> Yes, if you are debugging Visual Studio.
"3) Are there any good links to resources on the various options available when working on code that runs at design time?"
-> Yes, MaybeCompletelyW posted a link with many resources for working with design time code - including debugging. It is old, but is still relevant for the most part.
"4) What is the best way to have a routine that can be run at runtime and will modify my code (I assume I could use the macro option, but thought I would check on other options.)"
-> I don't understand this question. Aren't all routines "run at runtime"? Do you mean design mode/not design mode? You can write a routine that creates new code via the Reflection.Emit namespace, but this doesn't seem like a good idea to me. What are you trying to accomplish exactly?
Anyway, here's another way of debugging your design time code:
Start two instances of Visual Studio. In one instance of Visual Studio, Debug -> Attach to Process, select Managed Code from "Attach To", select "devenv" from "Available Processes". In this VS you should now be able to open your source file and set breakpoints in your designer code. In the first VS (the one being debugged), open your designer to exercise the code you want to debug. You should now be able to hit breakpoints and look at output strings in the other VS.
Patrick Nelson - Developer - Visual Studio Debugger Team
- Edited by Patrick Nelson - MSFTMicrosoft Employee Wednesday, December 19, 2012 3:05 AM
- Marked As Answer by Jack Zhai - MSFTMicrosoft Contingent Staff, Moderator Monday, December 24, 2012 3:00 AM
-
Thursday, December 20, 2012 7:44 PM
Patrick,
Cool, this is what I wanted. Seems weird at first, but makes sense when you think about it.
Yes, my item #4 was a typo - I want to be able to update the code from the designer when the standard search and replace can't manage it (although the regex search/replace does manage quite a lot.)
Thanks again!
me (and yes, I DO mark correct answers)
-
Thursday, December 20, 2012 8:30 PM
Patrick,
Ok, I am trying it but have some minor problems.
I have a solution that contains multiple projects. The controls I am trying to debug on in my windows UI project.
The directions are a little dated, but I think I have most of it figured out. But I am clearly missing something.
In VS 2010, I went to project properties and changed the Debug tab to start an external program and gave a complete path to the devenv.exe.
Then I added the command line argument (and this is where I think I messed up.) I entered the name of the UI project (fully pathed and quoted)
I then went to that project and specified a startup program (program.cs) and modified that to start a form that has the control in question.
When I try to debug, I get a file not found error: "FileNotFoundException crossed a native/managed boundary". I know it's not my reference to devenv.exe since the devenv comes up and then shows the error.
The problem I am trying to debug is what gets executed when the form opens and tries to render the controls.
Any idea what I need to use for the command line argument? The real startup solution/project or the UI project? And what startup do I use, the real one, the temp one I created in the project that calls my test form, or the control itself?
Of course, the other thing I don't know (since I haven't gotten that far) is how I then open the form to get the designmode code to run.
Thanks!
me (and yes, I DO mark correct answers)
-
Monday, December 24, 2012 2:59 AMModerator
Hi FletcherJ,
Glad to see that Patrick and MaybeCompletelyW’s suggestions could help.
As Patrick’s suggestion, it seems that we could use the managed debugging properties for it like the following documents.
http://msdn.microsoft.com/en-us/library/vstudio/68c8335t(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/vstudio/1ktzfy9w(v=vs.100).aspx
In this thread, we are mainly discussing about < how to debug the designer based code >. About this new issue, it would be better if you open up a new thread for the new question. In this way, our discussion here will not deviate too much from the original issue. This will make answer searching in the forum easier and be beneficial to other community members as well.
Thank you for your understanding.
Best Regards,
Jack Zhai [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Wednesday, January 30, 2013 5:38 AM
Ok, I finally gave up on trying to run VS in design mode using another instance of VS. I am sure this is a great way to do it, but since my goal is not to write code that executes at design time, but to get around a bug that MS has known about since 2003, I finally went with a way that worked for me.
I added a method that gets a string. It appends that to a member var with a separator.
In the controls load method, the last thing I do is check another boolean to see if it is true. If it is, I replace the TEXT with the contents of the member containing the messages.
When the screen is redrawn after a build, the text property now holds a long string of all the messages. I simply take that and break it into individual chunks (if the seperator is a couple of CR LF, then it's already broken into pieces.)
Anyway, by adding a call to this method in various places, I was able to figure out what code was running in which order and, more importantly, the location and size values at various points. Given that info, I was able to add code to fix the problem....
Thanks for all the suggestions. Even if I didn't need it now, I will probably need it at some point down the road.
me (and yes, I DO mark correct answers)
- Marked As Answer by FletcherJ Wednesday, January 30, 2013 5:38 AM

