GUI Designer and Code view - c#

I do not understand why code view displays the code of Form instead of Form.designer file, which is the actual file containing the GUI code. How could they say then that you can see GUI in designer or code view?

The point is that the code behind for the GUI itself should not be altered manually, because any changes inside the designer can then overwrite or remove all your manual changes.
The code file you actually get to see is the same class as the other code behind file, but the class is split into two by the use of the partial keyword. This keyword was actually made for this purpose, so that auto generated code, and manually written code can be put into two different files.

'Code view' means: the code of your class (or module), not necessarily the GUI code for that class.

Related

C# Class Diagram Dynamic Updating

I have a class diagram in C# and to test it out I'd like to use it to generate code and then go back and edit it. So I need to be able to go back and forth, generate code, update class diagram, rinse repeat.
However, for some reason I was only able to generate code once. If I try to generate code again it says "another project might exist in the same solution". How can I get around this?
Also when I update the generated code I'd like it to update the class diagram. Is that possible?
Thank you.
The difference is a .cd vs a .classdiagram. The .classdiagram does not update dynamically. The .cd or "logical class designer" does.

How do I fix these C# errors?

I found some code to help me in a project and when I first ran the code I received an error message indicating: "Visual Studio cannot start debugging because the debug target c:\path\'dirInfo.exe' is missing. Please build the project and retry, or set the OutPath and AssemblyName properties appropriately to point at the correct location for the target assembly."
Then I select OK and receive an error message indicating that partial is missing. I add partial to the code and receive 3 more error messages.
The type 'RecursiveSearchCS.Form1' already contains a definition for 'components'
does this mean I should delete this from the Form1.cs file?
Type 'RecursiveSearchCS.Form1' already defines a member called 'Dispose' with the same parameter types.
Type 'RecursiveSearchCS.Form1' already defines a member called 'InitializeComponent' with the same parameter types.
(I notice, when I comment out the InitializeComponent line and/or Dispose line, many more error messages populate in ERRORS)
By they way you can find the original code # MicrosoftSite.
Any help would be greatly appreciated.
Thank You
Just gut instinct, if you were following along and copy pasting remember one key thing:
The designer creates two files when you create a form: A "code" file, and a "designer" file. However, when microsoft (and others) release "templates", they like to merge these two files.
Just create a new .cs file and paste the code and all should be good. It's the code basically saying "in the designer, we already have this stuff". (a good way to note this is the "partial" keyword located before your Form1 declaration)
More info:
The Code file will house all your own implementations. That is click events, methods you personally override, events you bind to, etc. This is the default file when you select "View Code" from either your solution explorer or the dialog itself. Within this file is a construct that calls a "hidden" method, (InitializeComponent) that if you right click and "Go to Definition" will bring you to the next file:
The Designer file is the IDE's generated file. This takes everything you do in the designer and stores it for you. That includes new controls, location and properties of the controls, and the IDisposable implementation. The idea is to keep the "meat an potatoes" out of the way while you worry only about implementation.
Yes it sounds like you've copied the entire code which includes many things already contained within your Form in a partial class. Either remove these or remove the partial class and partial class declaration from your Form to get rid of these errors
I went to the Microsoft site to see what you did. The site shows code for an entire "one file" solution. We've all agreed that Visual Studio creates multi-file solutions, so you're duplicating code.
I don't know if the current answers/comments have helped you get this sample code working, so I thought I'd add my share. I was able to get this sample working by doing the following:
First, where the sample code at the Microsoft site shows declarations for button, textbox, labels, and combobox, rather than attempting to copy that portion, I simply used the toolbox and dragged a button, the labels, the textbox, and the combobox from the toolbox to my form.
You'll probably want to arrange these to your liking.
This process created my form correctly with the appropriate objects on it. All I had to do was use the properties window for each object and rename them according to what they were named in the sample. For example, my new button was originally button1, but I renamed it to btnSearch just as it is named in the Microsoft sample.
I noticed that the Microsoft sample has an established event handler setup for the Form1_load() event. I created this same event in my form by clicking the form in the designer, clicking properties, clicking on the Events button in that properties, and double-clicking the "Load" event. This automatically generated the appropriate code.
In a similar way, I had to create the btnSearch_Click() event. I did this by simply double-clicking the button in the designer.
After that, all I had to do was manually copy and paste from the specific sections of the sample to my code -- fill in the Form1_Load() event with what was in the sample. Copy the DirSearch() method over. Fill in the btnSearch_Click() event. That was it.
I hope this helps solve the overall issue and gives you more insight into how you can avoid these problems in the future.
You have duplicated functionality in the classes, you have a file that was automatically generated with that functionality already in it.

Making a duplicate of a form in Visual Studio 2008 (C#)

I have a form in my existing project.
My current task is to make a duplicate of an existing form and change few things on the new form. Making a copy of the form cs files would not do since the existing contents themselves refer to file information.
Simply put, I am trying to crate a form name MyNewForm, which will be a direct duplicate of MyCurrentForm without causing any naming conflict that may arise in mere copy pasting of code content.
What is the fastest way I can achieve this?
Copy the form in visual studio solution explorer. Rename it. And change the class name manually both in .cs and .Designer.cs files. Do not use VS refactoring feature as it blows away references to the original class.
To duplicate a form (in the same project):
Right click on the source form --> Copy
Right click on the destination folder/project --> Paste
Right click on the new form --> Rename
Change manually the class name in .cs
Change manually the constructor name in .cs
Change manually the class name in .Designer.cs
Enjoy!
Why do you need to make a duplication of the form? Try to find some refactoring that can help you, e.g. create some base form and extract common logic there.
Every time you make a duplication kitten dies!
You can just add a new blank form and then select all items on the original aform and paste them onto the new form. This will not copy the code behind though. But that can also be solved with copy paste.
This will not cause any renaming conflicts.

How do I convert a windows forms app from C# to VB?

I've been converting all kinds of other things, classes, interfaces, ASP.NET MVC apps, and WPF applications, but I am stumped as to how to convert a windows forms application, as the whole structure seems to be different. Does anyone have any advice on how to tackle this problem?
I see essentially that you have two files, Form1.cs and Form1.Designer.cs. Literally translating them from C# to VB doesn't work because of some intricacies or other of how Visual Basic works. I think you /might/ be able to convert Form1.cs in this way, as it's just a constructor plus event handlers, but the designer contains the references to the controls etc. and I am not sure how to go about doing that.
I am using Visual Studio 2010 Beta 2.
My approach would be to create a new form in VB do add some forms and add events and look at the VB generated from there you will be able to see how each element [control/event/propertyset] is done in both C# and VB. From there it should be trival to conver from c# to VB.net. This would give me more confidance the designer could cope with the converted class as well as the runtime.
What I did is to manually create a new form in VB with the same name as the form in C#, then opened the C# form in design view, made sure that I selected all controls on the form, copied and pasted them onto the empty new form in VB. Then just translated the code-behind with the telerik free converter at:converter.telerik.com. I'm sure it becomes cumbersome if you have a huge solution that you need to convert, but in my case it was a small solution and the results were great.
Redgate.NET Reflector does a great job.
The two files each contain a part of the Form1 class, in the form of partial classes. As partial classes are also supported in VB.Net, I don't see a problem here.
The designer file contains all of the code generated by the windows forms designer, the Form1.cs file is the place where all custom code goes (event handling, etc).
So after converting both files you should check if all references made to controls in the Forms1.cs file are available in the designer.cs file.

Calling inline (ascx) methods from codebehind file using partial classes

I'm using .net 2.0. I'd like to place one of my C# methods in an inline server script block inside my ascx file because I need to update it a lot while designing. This way I don't have to compile my large project everytime.
I would like to call the method from inside my code behind file. I use to do this by inserting a virtual stub method in the codebehind file and overriding it in the ascx file. Now, since I've started using partial classes, I was expecting to be able to just call the method directly. Is this not possible?
Thanks
Unfortunately, I don't think this is possible. At design time, the partial class (*.designer.cs) mainly contains just your controls. The class that actually contains code from the script block is also generated by ASP.NET but it inherits from your main class.
See http://msdn.microsoft.com/en-us/library/ms178138.aspx for more info.
I don't understand your motive... Isn't it just as easy to modify a page's codebehind as it is the .ascx file? Either way ASP.NET is going to either dynamically compile them when the file gets accessed or force your to rebuild, depending on the type of project.

Categories

Resources