Everytime I move to Designer View my whole designer.cs code is messed up :
VS Designer reorganizes code blocks and puts an irritant verbose prefixes like "this.whatever"
and fully qualifies objects using "System.Windows.Forms.whatever"
I Know that "Designer.cs" is not intended to be edited but I need to do some GUI code customization from time to time and would like these to stay as I changed them.
How to avoid that ? (Guess this is too funky for VS to handle)
(Actually I am just avoiding the use of the designer and do it all by hand, the good old way)
Update : I am surprised to see the herd-like reaction towards this question. Sorry if it is disturbing, but it is interesting to see that, before hitting me on the hand saying "DON'T DO that, it's Bad", NO ONE asked WHY I wanted to do it. IMHO the question is relevant and that's why : Many of the "auto-generated" code is rubbish and of absolutely no use, it does need some enhancement. One example (among soooo many others) : Why generate a Size/Location property when the control's Dock Mode is set to Fill ? I wanted to take advantage of the benefits whithout the drawdowns. Anyway, I'll keep the short answer : You take it ALL (with the rubbish) or leave it ALL.
It's simple: don't edit the designer code. Those warnings are there for a reason, and for Visual Studio to work correctly it needs to own that file.
This is a partial class: everything you need to do, you can do in the matching non-designer file for that class; this includes all your control declarations and other things. Since you're trying to avoid the designer entirely, let visual studio have that file and just put everything in your normal .cs file.
Update:
Based on the comment, I want to add the following —
Either use the designer or don't use the designer. Don't be wishy-washy about it. If you're using and relying on the designer for some things, you MUST leave the designer's file alone.
If you're avoiding the designer, then really avoid the designer. Everything it does you can do in your own code (except of course for the visual queues, but even that can be done better via prototyping). You can even create your own additional file for the partial class to keep designer-like code in.
If you're only using the designer as an occasional code generator to help reduce some boring typing, do that in a separate project or on a throw-away form in your existing project and just copy/paste the code over.
The designer should only be writing code in FormName.Designer.cs which you should not touch. Visual Stuiod declares a partial class implemented in two files - one for the designer, and one for your code. I'd be surprised to hear that the designer was rewriting the non-designer file.
If this doesn't help. please give more details of which version of VS you're using, which files are being rewritten, and which bits of those files.
You can always extend the designer class by putting your custom code in another cs file. The designer can't touch that and the compiler will compile your additions to that class. If you're doing something like adding properties or methods its super to simple to do:
// MyWinformExtension.cs
partial class MyWinformName
{
public string Foo { get; set; }
public void ProcessData()
{
// do some processing..
}
}
Voila! That's how you add your own code. You can't alter the designer code because it will be re-written ALL DAY LONG by the IDE. That's why there are warnings in the source and why everyone else is telling you the same thing.
Related
I have been using vs2013 for my Unity game with VS Unity plug-in.
Intellisense is working just fine except for my partial classes.
For some reasons that I cannot fathom, Intellisense's complete word function does not work.
If I write down the whole method/variable, it does not show any errors BUT when I'm half done with my typing, it shows up on very rare occasions.
I am currently wondering what may be the cause of this problem but not even close to finding one reason.
Could be that my partial class (split into 2) is large since both added up contains more than 20000 lines (which is the reason I split them).
But then, its merely 20,000 lines of code and is just a medium sized project compared to what other projects may contain.
Any help would be appreciated.
At the moment I have to write C# code in VS2012, (Before was: Java (Eclipse, IDEA)).
May be I am strange guy, but I like when class name == class file name.
Why in VS2012 no option to rename class file same as I renaming class name? (Or I could not find it)
I know that C# compiler do not care about matching class name and class file name, but I want to do it. I can`t find class by its file name if they are not same, it is annoying me very much aaaargh.
Help me please, I'm tired finishing work after VS
If you rename the file first, Visual Studio will usually offer to rename the class to match.
I'm not sure what the exact circumstances are, but if you're sticking to a single class per file it'll work most of the time.
You could use ReSharper. This plugin has option to rename file to match the class name.
It is a feature that VS sadly does not have. You can supply with a few refactoring plugin, like Resharper or CodeRush or Refactor! Pro...
They all come at a price and with their own fan base. CodeRush as far as I remember as a free version with very limited features, but maybe the "Rename File" is among them, you would need to try.
Another option would be to find an extension (or develop one) that does that for you, just head to the Visual Studio Gallery to find one. =)
You just point to the object you want to change, right click and use Refactor -> Rename. A popup refactoring window will appear. Choose your options and click "ok".
To address this line from Rawling's answer (since I don't have enough rep to just make a comment):
I'm not sure what the exact circumstances are, but if you're sticking to a single class per file it'll work most of the time
To clarify, Visual Studio will offer to rename and refactor your class when you change the file name as long as the class is defined within a namespace. If you've chosen not to use namespaces, you won't have access to this IDE nicety. This is true up to at least VS 2017 Pro (the newest as of this answer).
Please excuse the newbie question - my background is Unix and I am very green when it comes to WPF...
//Context//
I need to design a single page WPF-based UI, using Visual Studio, or Blend.
Additionally, I need this to be portable outside the microsoft generated solution... this is because I need to be able to define the business logic within a different c# environment, that is exposed by software called 'NinjaTrader' (for those who may use it).
I am restricted to .NET 3.5
//Additional info//
Right now, I am trying to get to proof-of-concept as quickly as possible. I have successfully implemented the UI using winforms. by simply designing the form within VS, and then copying the auto-generated designer code into my own abstract class definition (say 'myForm') within NinjaTrader. I then have to inherit myForm into a new class definition within a 'standard' NinjaTrader.Indicator class (this is necessary to be able to instantiate the myForm2 class), and by passing the outer class to the constructor of myForm2, and overriding the methods I need for the business logic, I can display a UI upon 'OnStartUp' of the NinjaTrader.Indicator class, and implement the behaviour that I need.
//My problem//
WPF is proving more difficult so far. I have tried copying the class definitions from 'WPFWindow.xaml.cs', and also what is necessary from 'WPFWindow.g.cs' into a single .cs file (due to limitations within NinjaTrader, I must use a single file for this), and then instantiating this class from another NinjaTrader.Indicator class.
Everything is in the same namespace, though I am repeatedly stumped when it comes to the relative Uri and it seems I get the following message, no matter what I do:
Cannot locate resource 'wpfwindow/wpfwindow.xaml'
Now - I am aware that the relative Uri will not be the same, when running from within a different application, and I have even tried copying the WPFWindow.xaml file to a new folder (within what seems to be the current working directory, and without any spaces) and calculating the relative :
string cpath = Directory.GetCurrentDirectory();
string installPath = #"C:\Program Files (x86)\NinjaTrader 7\bin64\WPFWindow\WPFWindow.xaml";
Uri cwUri = new Uri(#cpath);
Uri instUri = new Uri(#installPath);
string relPath = cwUri.MakeRelativeUri(instUri).ToString();
System.Uri resourceLocater = new System.Uri(relPath, System.UriKind.Relative);
System.Windows.Application.LoadComponent(this, resourceLocater);
However, I am still getting the same error.
I would be extremely grateful if anyone has any advice / suggestions as to how I might try to proceed.
Is there another way that I can load an XAML file (that is external to the current application / project), without using a relative Uri in order to display a single UI window? Any advice on what the bare minimum (less is definitely more reliable in this case) I would need to implement in order to do this and define the business logic, would be extremely helpful...
Thanks in advance for any advice
J
EDIT: (& Reponse to Brian S):
Thanks for the response, and also for the reccomendation on material. It is entirely possible that I have misunderstood the necessary architecture involved with WPF...
Therefore, please let me know it that is the case... My understanding is as follows:
The graphical composition, content, layout, etc is all specified within the XAML file. Behaviour, interaction, event handling etc is specified in the code-behind.
Once the XAML markup has been written, I had made the assumption that, in order to instantiate the window I had designed, all I needed to do was to load / parse it into a tree of objects within - lets say - a class that inherits System.Windows.Window, and is named as the same class as in x:class="myClass", within the XAML file.
Upon re-reading what I wrote previously, I realise that I was not all that clear about the situation. I can use multiple files for the solution, without a problem, the issue arises as follows:
With classes that are partial, across 2 files - lets say file1.cs and file2.cs, class methods will only be recognized within the same file that they are defined (unless I actually instantiate the class - after which I can call it's methods). So - for example - I could not specify InitializeComponent() in file1.cs, and then define a constructor in file2 that refers to InitializeComponent()... I hope that makes sense...
What I can do, which seems to work, is to avoid defining methods outside of the file that I need to reference them in. And thus, avoiding the use of partial classes as far as possible.
I can define a class in one file (say class1.cs), then instantiate it from another file (say class2.cs) no problem, by using:
private class1 mynewclass1 = new class1(args)
I can also use:
'System.Windows.Application.LoadComponent(args);'
Though I seem to be running into issues with the relative addressing, as no matter what I try it does not seem to find the .xaml file..
Is there a way that I can try to load the xaml file without using relative addressing? I have been looking at XamlReader, though I'm not sure it can do what I need....
Thanks again,
J
If I follow, you're trying to treat the WpfWindow.xaml file as a loose resource file located in the same directory. This is not how WPF works - the WpfWindow.xaml file is a partial class with the codebehind. This is possible with XAML Resource Dictionaries, but I don't believe it is possible with the WpfWindow.xaml file because they need to be compiled together.
If you want a single-file solution, it is a lot more work, but everything you do in XAML, you can do through code. So you can create a window, add controls, position them and connect behaviors all through the codebehind. Obviously, this is not the ideal approach, but it sounds like you've got some pretty significant limitations (I'm not familiar with NinjaTrader).
The MSDN Documentation will provide help on the code necessary to manually construct and layout a WPF window, or if you are looking for another resource, Applications = Code + Markup, by Charles Petzold goes through doing anything you need to do with WPF from codebehind.
Background:
I have a C# Windows Forms application that contains a Windows service and an interface used to configure system settings as well as communicate with the service.
Desired Outcome:
I would like to build two versions of the solution - a client version with all the Windows service related code and form elements and a server version that contains everything.
The form contains a tabbed control, where one tab contains elements used to interface with the Windows service using sockets. All I'm really trying to achieve is that for a full build the tab containing service related elements is compiled, while for a conditional build the same tab is excluded.
Problem:
At this stage I've used #if directives around the Windows service related code. For example:
#if SERVERBUILD
//Code relating to Windows service that I do not want to compile
//for a client version.
#endif
In the above example, 'SERVERBUILD' corresponds to a build configuration that I can select via the Configuration Manager (as opposed to the standard 'Release' build option).
The issue I'm having is that some of the code I've had to wrap this #if directive around lies in the WinForm.Designer.cs file in the region titled 'Windows Form Designer generated code'.
What seems to be occurring is that when I make a change to some of the form properties, this entire region of code seems to be deleted and re-generated, thereby removing the #if sections I had added.
Am I going about this the right way?
Is there a way to avoid the situation where I am losing the changes I've made in the WinForm.Designer.cs code?
I would really appreciate any advice from anyone with experience with conditional compilation and this sort of stuff.
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
Nuff said. Solve your problem by putting the code in the form constructor:
public Form1() {
InitializeComponent();
#if !SERVERBUILD
panel1.Visible = false;
#endif
}
Note that using a panel is an easy way to make all the controls that are on it invisible.
An issue with the #if approach is that you can rapidly start falling into a situtation where you have multiple #if statements for different compiles. It very quickly becomes difficult to tell which chunk of code goes with which project.
A better solution would be to identify all common code and keep them in a separate project folder, such as common/ . Then you have the specific service code under server/ for example and all forms code under client/ .
All common code will still exist in one location and you server and clients become much more readable. You no longer have to worry about client only changes affecting the server and the opposite. Plus with proper organization of folders you can keep the what of your project (abstract concepts such as client/server) apart from the how (concrete implementations such as forms).
There isn't much you can do. the Form designer is going to generate that code, and do it in a brute force way, with no care at all about your needs or changes you have made. You shouldn't muck with autogenerated code because of this.
You will need to take a new approach and figure out how to get what you need accomplished without altering the autogen code, as that's definitely a path of pure frustration.
I've added a small function in the Resources.Designer.cs and it working great, the problem is that when I add or remove something from the Resources.resx this function always get removed, is there any indicator I could put or anyway to bypass this?
Thank you!
It is a really simple method used as an indexer.
internal static string Keys(string key)
{
return ResourceManager.GetString(key, resourceCulture);
}
I've done that because you can't do Properties.Resources and then concat a value. An extension could work, however I tried and it gets fancy because it's all static method and you dont want to instantiate.
Ideas?
The code portion of a resx file is autogenerated by a tool called PublicResXFileCodeGenerator (or InternalResXFileCodeGenerator, depending on what you've set the resource file visibility to). It says right at the top of the file:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
If you really need to get around this, you'll need to subclass the PublicResXFileCodeGenerator (the tool that VS runs to generate the resx code), and implement your own tool that Visual Studio can run every time it wants to refresh a resource file.
Overriding the tool would allow you to generate partial classes, and then you could store your customized code in another .cs file somewhere as another partial class to the resource file class.
Honestly though, there's probably easier approaches. What's the custom code that you're implementing? Can you just turn it into extension methods? A bit more detail might help us point you in a better direction.
i can't verify at the moment but if the autogenerated class is a partial class, create a matching partial class in another .cs file and add any ammendments to that
edit
confirmed the designer class for resx is not partial (seem it should be as a generated file?) so an extension method is probably the best.