How do I stop Auto Outline Expansion in Visual Studio 2010? - c#

How to do I stop Visual Studio 2010 from expanding outlines/regions automatically?
Right now whenever I create certain syntax errors (usually while editing, sometimes just from starting to type new code), it will auto expand every outline/region below that line.
I've been through the formating settings, but I can't find anything. Maybe I don't know the proper terminology that applies to the setting.
Example code that causes this behavior(one of many):
public string myMethod1(string myStr)
{
try //<-SELECT AND DELETE ME
{ //<-AND ME TOO, AT THE SAME TIME
return myStr + "success";
}
catch
{
return myStr + "failed";
}
}
//ALL OF THESE BELOW WERE COLLAPSED,
//BUT WILL EXPAND WHEN "TRY {" IS DELETED
#region HIDING
public string myMethod2(string myStr) { /*...*/ }
public string myMethod3(string myStr) { /*...*/ }
public string myMethod4(string myStr) { /*...*/ }
public string myMethod5(string myStr) { /*...*/ }
#endregion HIDING
Thanks for any help!
EDIT:
I can force to happen by removing any starting bracket {, anywhere in my code.

This is not normal behavior. It is possible that an add-on you have installed is incorrectly doing this. You can reset your environment to the default settings by going to
Tools -> Import and Export Settings ...
choose Reset all settings
click next
save your current settings if you want to
then choose the default setting (I prefer General Development Settings because I work on lots of different things)
Click finish
Saving your current settings allows you to keep your old stuff in case you do not like what defaults you have chosen. So there is really no chance for loss of your environment setup when you do this.

Related

Application changes not taking effect

I have made a change to a method used in a Functiod in a mapping file but it seems the new code is not taking effect, ever.
I have deployed properly, started the application, restarted related host instance (actually all host instances I could find) and still, the old code seems to execute.
Here's the, simple, method:
public string RemoveNonNumericChars(string stIn, int maxLength)
{
string strOut;
try
{
strOut = Regex.Replace(stIn, "[^0-9]", "");
System.Diagnostics.EventLog.WriteEntry("BizTalk Server 2009", strOut);
return strOut.Substring(0, maxLength);
}
catch
{
return string.Empty;
}
}
I added the writing to EventLog line to see that this code is indeed being executed, but I don't get anything in "Application" event logs.
I do NOT get an empty string being returned, so it really does seem like the old code that's being executed prior to me fixing the method.
What am I missing exactly ?
Thank you.
For some reason, the script is not able to correctly retrieve the Build Config selected in Visual Studio, it's taken from Debug when I'm actually trying to build it for a Test environment. I should have known, thanks anyways.

Standard way to keep debug lines in the program

I would like to add some debug lines to my program. Once after executing statements it will record the current status to a file.
I have done that in following way.
public int? DoWork(int x, int y)
{
Log.Write("Received inputs. X an Y values are:"+x+","+y);
bool result = ChekData(x);
if (!result)
{
Log.Write("First input is not valid");
return null;
}
result = ChekData(y);
if (!result)
{
Log.Write("Second input is not valid");
return null;
}
Log.Write("Valid input found");
....
....
}
I feel this is not the standard wa to do this. Keeping text like this in the code. After searching I found using Resource file I can save these messages like name value pair.
But I have no idea about the standard of that. Please advise me.
Basicaly for the loging I am using Log4Net
This is pretty normal way of doing logging.
Using resource files for logging generally does not make sense because:
it moves descriptive message away from the place it most useful - inline code
logs most commonly used by original developers, so getting logs in Japanese (if log resource strings are properly localized) is rarely useful for English speaking developers and vise versa.
avoiding localization of some strings (one that are used for logging) may be inconvenient, localizing them is not free...
If it is only for debug purpose i would do the following:
Set appropriate debuglevels. The debug version should then be build using a level to show all messages. The release build normally don't need debug outputs. Therefore disable the message level for release output.
For distinction if you are in release build or debug build you can use the following 2 things:
#if DEBUG
// enable all tracing
#endif
or if you also want that your realease build brings messages if a Debugger is Attached
if(System.Diagnostics.Debugger.IsAttached)
{
// Someone has attached a debugger, so give more output
}
You can also wrap the logcalls if you want with a method which justs checks for debug/attached debugger..

Highlighting code window text from Visual studio tool window using VSPackage

I'm kind off new to the Visual Studio Extensions. Is there a way to interact to code window from Visual Studio 2010 tool window.
I have a Datagrid hosted on the VisualStudio tool window. DataGrid contains ClassName, MethodName e.tc. On the click of className/MethodName need to acheive the following
Open the particular .cs file containing className/MethodName
HighLight the particular className/MethodName.
I know this acheivable using "IWpfTextView" class, but not sure how. Did a lot of googling but in vain.Even the link below remains to be un-answered link.
Any help on the above will be greatly appreciated.
I actually did almost the same thing. You can see complete source code on Visual Localizer.
Basically you need to do two things:
Obtain IVsTextView instance for the file
call its SetSelection() method which takes range (start line, end line, start column, end column) as parameters. You may also want to call the EnsureSpanVisible() to scroll down.
Obtaining IVsTextView is quite easy as well. In my project (Visual Localizer) there's a class called DocumentViewsManager, located in VLlib/components that has fairly straightforward method called GetTextViewForFile(), which takes only file path as an argument. It does the following:
use VsShellUtilities.IsDocumentOpen method to obtain IVsWindowFrame for given file path
pass this to the VsShellUtilities.GetTextView method, which returns IVsTextView
Hope it helps.
Thanks cre8or.
I found an alternative to do the same.
You need to use "TextSelection" class to highlight the above code line.
foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements)// search for the function to be opened
{
// get the namespace elements
if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
{
foreach (CodeElement namespaceElement in codeElement.Children)
{
// get the class elements
if (namespaceElement.Kind == vsCMElement.vsCMElementClass || namespaceElement.Kind == vsCMElement.vsCMElementInterface)
{
foreach (CodeElement classElement in namespaceElement.Children)
{
try
{
// get the function elements to highlight methods in code window
if (classElement.Kind == vsCMElement.vsCMElementFunction)
{
if (!string.IsNullOrEmpty(highlightString))
{
if (classElement.Name.Equals(highlightString, StringComparison.Ordinal))
{
classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
// get the text of the document
EnvDTE.TextSelection textSelection = window.Document.Selection as EnvDTE.TextSelection;
// now set the cursor to the beginning of the function
textSelection.MoveToPoint(classElement.StartPoint);
textSelection.SelectLine();
}
}
}
}
catch
{
}
}
}
}
}
}

Reset settings to SpecialFolder

I store the directory path of a folder in Properties.Settings.Default.Temporary and I allow the user to change this value and other settings using a PropertyGrid.
When the user decides to reset the Settings, I would like to change Properties.Settings.Default.Temporary to the value of System.IO.Path.GetTempPath() by using Properties.Settings.Default.Reset()
I know about System.Configuration.DefaultSettingValueAttribute. Something like this:
[global::System.Configuration.DefaultSettingValueAttribute(System.IO.Path.GetTempPath())]
does not work.
I also read Storing default value in Application settings (C#), which described a related problem, but I wonder if there is a way to solve my problem in the way described above.
The DefaultSettingValueAttribute.Value property is a string, therefore you cannot pass a function call to be called when the value is used. In fact there is no ability to pass code to an attribute: only literals are possible.
Instead in your applications code where you reset the settings, follow this by setting and settings that you want to have values that are not literals at compile time (eg. dependent on the execution environment).
I just had an idea for a workaround myself:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute(null)]
public string TemporaryDirectory
{
get
{
if (this["TemporaryDirectory"] == null)
{
return System.IO.Path.GetTempPath();
}
return ((string)this["TemporaryDirectory"]);
}
set
{
if (System.IO.Directory.Exists(value) == false)
{
throw new System.IO.DirectoryNotFoundException("Directory does not exist.");
}
this["TemporaryDirectory"] = value;
}
}
I don't know, if this has any side effects, but so far it seems to work. I am sorry that I had this idea shortly after posting. I should've thought about the problem a bit longer.

Reading default application settings in C#

I have a number of application settings (in user scope) for my custom grid control. Most of them are color settings. I have a form where the user can customize these colors and I want to add a button for reverting to default color settings. How can I read the default settings?
For example:
I have a user setting named CellBackgroundColor in Properties.Settings.
At design time I set the value of CellBackgroundColor to Color.White using the IDE.
User sets CellBackgroundColor to Color.Black in my program.
I save the settings with Properties.Settings.Default.Save().
User clicks on the Restore Default Colors button.
Now, Properties.Settings.Default.CellBackgroundColor returns Color.Black. How do I go back to Color.White?
#ozgur,
Settings.Default.Properties["property"].DefaultValue // initial value from config file
Example:
string foo = Settings.Default.Foo; // Foo = "Foo" by default
Settings.Default.Foo = "Boo";
Settings.Default.Save();
string modifiedValue = Settings.Default.Foo; // modifiedValue = "Boo"
string originalValue = Settings.Default.Properties["Foo"].DefaultValue as string; // originalValue = "Foo"
Reading "Windows 2.0 Forms Programming", I stumbled upon these 2 useful methods that may be of help in this context:
ApplicationSettingsBase.Reload
ApplicationSettingsBase.Reset
From MSDN:
Reload contrasts with Reset in that
the former will load the last set of
saved application settings values,
whereas the latter will load the saved
default values.
So the usage would be:
Properties.Settings.Default.Reset()
Properties.Settings.Default.Reload()
Im not really sure this is necessary, there must be a neater way, otherwise hope someone finds this useful;
public static class SettingsPropertyCollectionExtensions
{
public static T GetDefault<T>(this SettingsPropertyCollection me, string property)
{
string val_string = (string)Settings.Default.Properties[property].DefaultValue;
return (T)Convert.ChangeType(val_string, typeof(T));
}
}
usage;
var setting = Settings.Default.Properties.GetDefault<double>("MySetting");
Properties.Settings.Default.Reset() will reset all settings to their original value.
I've got round this problem by having 2 sets of settings. I use the one that Visual Studio adds by default for the current settings, i.e. Properties.Settings.Default. But I also add another settings file to my project "Project -> Add New Item -> General -> Settings File" and store the actual default values in there, i.e. Properties.DefaultSettings.Default.
I then make sure that I never write to the Properties.DefaultSettings.Default settings, just read from it. Changing everything back to the default values is then just a case of setting the current values back to the default values.
How do I go back to Color.White?
Two ways you can do:
Save a copy of the settings before the user changes it.
Cache the user modified settings and save it to Properties.Settings before the application closes.
I found that calling ApplicationSettingsBase.Reset would have the effect of resetting the settings to their default values, but also saving them at the same time.
The behaviour I wanted was to reset them to default values but not to save them (so that if the user did not like the defaults, until they were saved they could revert them back).
I wrote an extension method that was suitable for my purposes:
using System;
using System.Configuration;
namespace YourApplication.Extensions
{
public static class ExtensionsApplicationSettingsBase
{
public static void LoadDefaults(this ApplicationSettingsBase that)
{
foreach (SettingsProperty settingsProperty in that.Properties)
{
that[settingsProperty.Name] =
Convert.ChangeType(settingsProperty.DefaultValue,
settingsProperty.PropertyType);
}
}
}
}

Categories

Resources