How to remove a property from a custom user control - c#

A property was added to a custom user control. Since then, Visual Studio has inserted generated code to every form that uses that user control. Now we want to remove that property because it is not going to be used. But obviously doing so causes compile errors. Is there a way to tell Visual Studio to remove the property from all that generated code? It seems to me that putting something like [Obsolete()] should be enough to tell Visual Studio to remove it from the generated code. Is there some other way?

Look at DesignerSerializationVisibilityAttribute, especially the DesignerSerializationVisibility.Hidden option.
Caveat: Unfortunately you will have to open every form/control in design mode so the code can be regenerated (if it does not do so already).

Regex to the rescue! What I usually do is open Replace in Files dialog (Ctrl+Shift+H), check Use Regular Expressions checkbox and replace instances of .+\.SomeProperty.+ with empty string.
Be sure to commit your changes to repository before this, so you can revert to working state if anything goes wrong.

You can try to use some refactoring tool, like Resharper.
http://www.jetbrains.com/resharper/
Adding the "Obsolete" atribute will only generate a compiler warning, but wont remove any code.

Just leave it and mark it as Obsolete.

Related

Visual Studio Refactoring changes to many things

Example: When i rename the following function
public void Foo(){}
to
public void Bar(){}
Then visual studio changes all constant strings, that contain the word "Foo" and replaces "Foo" with "Bar". Same goes for comments.
I NEVER want to replace values in strings with ctrl+r. And i only want to replace the references in the comments, if they are explicitly referenced with cref attribute.
It would be much appreciated if anyone could help me find the related setting in visual studio.
Thanks and kind regards.
I tried:
CTRL+R to refactor stuff.
...Comments changed
...Strings changed
...I expect only cref values and C# references to be updated.
I searched for "refactor" in Tools/Options and couldnt find the setting... But i might have overlooked something, because there are tons of options for that term...
I looked for other stackoverflows with that problem. Might have overlooked something there too.
There are checkboxes in the rename dialog, accessed by placing the caret on a variable and pressing Ctrl+R, Ctrl+R, that allow you to exclude comments and strings from automatic renaming operations.

Can VS Intellisense be configured to run "to-cursor"?

I use both Delphi and Visual Studio (C#), and I've noticed a difference in how code-completion works which I find really annoying. In Delphi, if you edit existing code so that you change the variable you're using, code completion will give hints based on the code before your cursor. For instance, inserting an "I" into myString to become myIString would give a code hint to show any available variables starting with "myI". Selecting one will then overwrite myString with the new one.
In Visual Studio doing the same thing brings up a code-completion menu with all available variables, and none selected so you see the top of the list. You have to completely delete the variable you're using and start again before you get meaningful code completion.
I seem to make changes like this quite often (maybe I should just get it right in the first place!) so it gets a bit annoying having to remove code, and can be trickier on longer lines with lots of parts to them.
Here's some screenshots which (hopefully) illustrate the difference. I was struggling how best to word this!
Code completion in Delphi
Code completion in Visual Studio
I've tried playing with the Intellisense settings in Visual Studio and there's nothing there which seems to change the behaviour. Is there any way of replicating it in C#?
I've tried playing with the Intellisense settings in Visual Studio and
there's nothing there which seems to change the behaviour. Is there
any way of replicating it in C#?
For now, VS doesn't support for this behavior. The auto completion won't work when we insert a character into existing variable. It will take effect when we type a character in a empty place.
Type character in a new place, the intellisense works.
Insert character,the intellisense not works.
In my opinion your suggestions is really meaningful. I think it could be better if we have a new option to support for auto-completion when insert character, so I suggest you can Go=>Help=>Seed Feedback=>Provide a suggestion to post your idea there.
We who interested in it will vote for you if you share the link here.
As alternative ways:
You can go Tools=>Options=>Text Editor=>C#=>Intellisense=>Enable Show Completion list after a character is deleted option.
Then you can insert 'i', the statement could be myiSring1.GetType(); And delete any or all characters in the String1,it would show the intellisense like below:(e.g:I delete 'r'.) Apparently, this is what you really want, but as I mentioned above, VS itself doesn't support this behavior for now, hope you can use 'delete option' saves some time. Sorry for the inconvenience.
Also, you can check if ReSharper can meet your needs,since it's a third-party tool which seems to charge after xx-days trial, I have no further details for it.
Hope all above helps :)

Visual Studio auto opening collapsed properties

Here is my problem. I have a C# class file loaded in Visual Studio that I am working on. I made a class and created some properties inside of said class. I collapsed the properties like so:
I collapse the properties with the little +/- button on the side of each. I want to collapse each of them since I have a lot of other classes with properties and I don't want to see a lot of repetitive code (get, set).
Closing them works fine but after I work on some other classes in the same file the properties re-open so I can see the code:
This happens relatively often and is quite annoying. I have a lot of lines in the file and I sometimes have to work with a small screen. Is this a glitch/bug or something else? (I am using Visual Studio 2010 C# Express)
Thanks for your help.
There is no way to solve this so far, but this does the trick. Press CTRL-M and then O and it will auto collapse everything in the current file you are on.
An alternate tip to #DJKRAZE's excellent answer is Resharper. I use Resharper and it takes care of this automatically for me. For example if I write this code:
The 4 line property procedure causes a Expand/Collapse that is causing you grief.
Resharper makes it one line. When I delete the last curly brace (namespace) and enter it again, the code is automatically formatted (Edit > Advanced > Format Document doesn't do this):
You can use this:
#region some_name
//your variables or code whatever
#endregion
And then you can collapse the particular code, and the good thing it does not open like the way it happens in your case.
See if that worked.

Is it good practise to remove Id tags from Controls that I don't reference in Code Behind

I'm wondering about a feature in Visual Studio. Personally it bugs me when I open a solution someone else has been working on and it's full of Controls with Ids like "label27" "textbox3" etc.
One of the first things I do is either rename them or remove them if they are never referenced in code behind (as is usually the case with the labels).
But I was just wondering why Visual Studio defaults them such meaningless values, then I started thinking if maybe it is better practise to have something in the Id field rather than blank it.
Of course the best situation is to give them all sensible names, but if I come across a project full of those auto generated Ids, am I doing the author a favour by removing them to "clean up" the solution?
I used to think so, but wouldn't the default for dragging and dropping or copying and pasting controls in be without an Id?
I think there's no reason to keep IDs like that. More ids => more html => more size. I know, this may be not significant, but why to plague your source code with something you don't want and don't really need?
If you don't like VS's default behavior, you can disable "auto id-ing" via settings:
Tools -> Options -> (Show all settings) -> Text editor -> HTML -> Miscellaneous
VS will assign ID automatically to the controls you haven't provide them to. But if you are pasting some code with controls without IDs, VS will add autogenerated names to them.
So, Microsoft thinks that all the controls should have uique IDs. And you should provide such IDs. But if you really don't need them, so simply remove ID to adjust readability to your code.
The main point is this should be for all controls accross the project, and it should be easy to read "clean" code.

Remove the automatic #region/#endregion tags when implementing an interface in Visual Studio 2005/2008

When user the "Implement Inteface X" context menu feature, the inserted code gets surrounded by a
#region [interfacename] Members
#endregion
pair. I always end up deleting this, is there a way I can permanently turn it off? I had a quick search through the snippets directory, but wasn't sure if this was the right place. There's pp_region.snippet that I guess I could modify, but I got the feeling that would turn off the #region/#endregion completely. I thought I'd ask here before I go doing things that will have me re-installing VS...
You can turn it off via Tools / Options
Then, in the option-window, you select 'Text Editor', then the language of your choice (C# for ex).
Then, you select 'Advanced', and then, you have a checkbox which says:
'Surround generated code with #region'
Also, if you're using Resharper you can fully configure the layout of your classes so they'll look exactly the way you want. One of the options is to completely remove the region tags whenever it finds any. It's awesome to open any c# project and have it reformatted just by doing [CTRL+E, C]

Categories