Alternative to macros in Visual Studio 2012 - visual-studio-2012

I use macros extensively for ViewModel properties in XAML development. I use them even more in WCF to generate Message and DataContract properties.
To my disappointment, the macros I've built aren't going to be usable in Visual Studio 2012.
An example of what I'm talking about, for a VM, I would enter something like this.
int id;
string name;
Select both lines, run a macro and end up with
private int _id;
private string _name;
public int Id
{
get {return _id;}
set
{
if(_id != value)
{
_id = value;
RaisePropertyChanged("Id");
}
}
public string Name
{
if(_name != value)
{
_name = value;
RaisePropertyChanged("Name");
}
}
I'm looking for ideas of other solutions deal with losing macros.

The simplest alternative to macros is creating add-ins. I know, I know, I wasn't excited about it either, but it's actually surprisingly easy. There are three simple parts to it:
Create the macro project, stepping through a wizard UI.
Write your code.
Copy the macro's .addin and .dll files to your Visual Studio Addins directory.
Let's take a simple macro I wrote to show the Start Page after closing a solution and turn it into an add-in.
Create the macro project
Run VS 2012 and create a new project.
Go to Templates > Other Project Types > Extensibility and select Visual Studio Add-in.
Give it a name, such as ShowStartPage.
Click OK. This brings up the Add-in Wizard.
Step through the wizard, choosing:
Programming language: we'll use C#
Application host: VS 2012 should be selected
Name and description for your add-in
On the add-in options page, checkmark only the second option ("I would like my Add-in to load when the host application starts")
Skip past the About Box stuff for now, and click Finish.
Now you have an add-in project. Here's what you do with it:
Write the code
Open the Connect.cs file. (It might already be open. Some of the "DTE" stuff should look familiar.)
Add this code at class level:
SolutionEvents solutionEvents;
Add this code to the OnConnection method, right after the _addInInstance = (AddIn)addInInst; line:
solutionEvents = _applicationObject.Events.SolutionEvents;
solutionEvents.AfterClosing += () =>
{
_applicationObject.ExecuteCommand("View.StartPage");
};
Hit the "Run" button to test your code. A new instance of Visual Studio 2012 starts up, with your add-in loaded. Now test the add-in and make sure it works. (Open a solution, then close it; the Start Page should return when you do.)
Deploy it
Once the add-in works, to use it regularly with Visual Studio 2012, you only need to deploy two files:
ShowStartPage.AddIn (from your main project directory)
ShowStartPage.dll (from your project's build directory; e.g. bin\Debug or bin\Release)
Put those two files in your VS 2012 add-ins directory, probably located here:
C:\Users\[your user name]\Documents\Visual Studio 2012\Addins
Then exit and restart Visual Studio, and you should see your add-in working. You should also see it listed when you go to Tools > Add-in Manager.
While this is a bit more of a nuisance than just opening the macro editor and sticking your macro code in there, it does have the advantage that you can use any language you want, instead of being stuck with the somewhat flaky VB-like editor in past versions of Visual Studio.

The Visual Commander extension (developed by me) is an alternative to macros in Visual Studio 2012/2013/2015. You can even reuse your existing Visual Studio macros code in new VB commands.

I'll stick to cutting the text into Notepad++ and using macros there, then pasting back. It is a shame the feature isn't in Visual Studio 2012 any more...

There is an add-in, VSScript, for Visual Studio which replaces missing macros functionality. Although it does not use Visual Basic, but the Lua scripting language, you might want to try it out.
There is a recorder, macro code editor window with IntelliSense, and a simple debugger. The add-in also supports earlier versions of Visual Studio, so if you prefer the Lua language rather than Visual Basic, you can use it instead original Visual Studio macros.

I was very sad to see Macros go too. You can get close with substitutions using the regular expression search and replace inside of Visual Studio 2012. In your case:
Find:
(.*) (.*);
Replace with:
private $1 _$2;\npublic $1 $2\n{\n get {return _$2;}\n set\n {\n if(_$2 = value;\n RaisePropertyChanged("$2");\n }\n}\n
That will get you everything except capitalization of property names which Macros would be better for.
But one advantage of the regular expression approach is when the input isn't as simple (e.g. database table DDL statements).
Here are a couple of useful links from MSDN:
Substitutions in Regular Expressions
Using Regular Expressions in Visual Studio

I use Notepad++ with regular expressions like this:
Find:
public (.\*) (.)(.*) \\{ get; set; \\}
Replace:
private \1 \l(\2)\3; \r\n public \1 \2\3 \\{ get \\{ return \l(\2)\3; \\} \r\n set \\{ \l(\2)\3 = value; OnPropertyChanged\(para => this\.\2\3\); \\}\\}

Check out http://devexpress.com/coderush
The templates feature does pretty much what you want.
There is a free "Express" version too.

Visual Studio 2012's lack of macros was getting me down, as I have a few that I use literally all the time to insert standard little bits of text with a single keypress. So I wrote a very simple scripts extensibility package, VSScripts, which allows manipulation of the current selection from a command-line program.
This doesn't claim to be some all-encompassing full replacement for the old macro system, and it doesn't provide keyboard macros, but it does make it possible to recreate many types of text manipulation macro.

Here's what I did to keep my macro functionality...
Download and install the Visual Studio 2012 SDK here (it contains the "Visual Studio Package" template)
New project -> Installed.Templates.Visual C#.Extensibility.Visual Studio Package
Wizard page 1 of 7
language = C#
gen new key is fine, or use another if you have one
wizard page 3 of 7
check "menu command"
Wizard page 7 of 7
uncheck both integration and unit test project options
Click finish
In the .cs file:
using EnvDTE;
using EnvDTE80;
...
private void MenuItemCallback(object sender, EventArgs e)
{
MenuCommand cmd = sender as MenuCommand;
// This should start to look like familiar macro code...
EnvDTE80.DTE2 dte2 = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2;
TextSelection selection = (TextSelection)dte2.ActiveDocument.Selection;
dte2.UndoContext.Open("macro command replacement");
selection.Text = "inserted from macro replacement";
selection.NewLine(1);
dte2.UndoContext.Close();
...
Run the project. A new Visual Studio instance will start with the package loaded.
Find your command as the first entry at the top of the Tools menu. Click it to see if it works.
To install for real, go to your bin\debug(/release) directory and double-click on the .vsix file
It should be installed for the next time you run
Go to menu Tools -> Options... -> environment.keyboard and map a keystroke to your tool
Mapping theme : Visual C# 2005
Command : Tool.yourFunctionName (functions defined in the .vsct file)
If you want more than one command, you will need to add menu id's in the PkgCmdID.cs, Guids in the Guids.cs, layouts in the .vsct and a function in the *package.cs (and button(MenuCommand) in the Initialize function) for each one. It can all be done in the same project.
I used this project to create several new 'tools' with my old macro code, then mapped my keys to them. It's a lot more work (and headaches) up front, but doesn't have the lag time that macros had.
There is probably a way to do this without having it take up tool menus. I started looking at this last night and finally got it to work, so I'm done with it for now (at least until Microsoft decides to drop this too).

Personally I like this one - the Visual Commander extension lets you automate repetitive tasks in Visual Studio.

Related

Custom Pane In Excel

I am trying to create a custom static pane on the right side. so it shall be independent of zoom and scroll function in excel. please see the image for more. I tried the following. but no success.
I not sure whether the split-screen function in excel can help to achieve this.
Please note: I want to do it in page layout mode. as I was to set cell sizes in inch.
I tried the following.
ActiveSheet.Range("H1").Select
ActiveWindow.FreezePanes = True
ActiveSheet.Range("H1").zoom = False
I have no idea to make it work, as I am new to VBA. I need a user interface something like in the picture below. so I can scroll on the left but static part on right. in the static part, I would be able to add custom controls like Combobox, command button, TextBox, etc. Thanks
You have 2 solutions:
VSTO Taskpane add-in:
Custom task panes give you a way to create your own task pane and provide users with a familiar interface to access your solution's features. Custom task panes let you integrate your features into a familiar user interface. You can create a custom task pane quickly by using Visual Studio tools.
Document: https://learn.microsoft.com/en-us/visualstudio/vsto/custom-task-panes?redirectedfrom=MSDN&view=vs-2019
Office JS Taskpane add-in:
You can create an Office Add-in by using the Yeoman generator for Office Add-ins or Visual Studio. The Yeoman generator creates a Node.js project that can be managed with Visual Studio Code or any other editor, whereas Visual Studio creates a Visual Studio solution. Select the tab for the one you'd like to use and then follow the instructions to create your add-in and test it locally.
Document: https://learn.microsoft.com/en-us/office/dev/add-ins/quickstarts/excel-quickstart-jquery?tabs=yeomangenerator

Maddening auto-format behavior with Visual Studio 2019 / Resharper that I cannot disable

This started happening for me in Visual Studio 2019 but does not happen in 2017. I cannot figure out how to stop it.
Something I do all the time is make use of Resharper's reformatting. Suppose I have little block of code like this:
Scan = scan;
Target = target;
IsDone = true;
ShouldAlign = false;
I want to align it up to look all nice and neat. So I select my block of code and I either use the menu or a keyboard shortcut to invoke Resharper's autoformat to line up the equals signs. Voila! Looks great, Here is the reformatted code, exactly as it appears, right after I've reformatted. Note the code is still selected:
But then all I have to do is I click anywhere else in the whitespace of that document, and Boom! Everything gets undone
It is as if VS2019 runs its own "Format Selection" command when I click away But I never did that. (but if I manually do, it has the same effect)
However if instead of just clicking away on the whitespace I, click the menu area of Visual Studio and then click the document whitespace, no problem. No reformat.
Just now I've just finished comparing Visual Studio 2017 and 2019 editing settings side-by-side next to each other. I've compared every single edit/formatting/spacing setting I can possibly find. Both Visual Studio's and Resharper's. They are identical except in those few cases where VS 2019 has some new setting that 2017 did not.
This still happens even when all of VS2019's "automatically format" settings are off
I'm asking the same question of the Resharper folks but I think it has to do with the interaction of the two.
Is anyone else seeing anything like this? It's driving me nuts.
After several hours of this, I have finally discovered that that problem was neither Visual Studio nor Resharper. It was actually a THIRD extension I use (also very common) for C++ files called Visual Assist".
VA has its own "format after paste" option. I am guessing that it must detect the reformat/click away as a "paste" operation and it reformats it according to Visual Studio's settings.
Once I disabled that in single, Visual Assist option VS2019 (as I apparently already had in 2017) everything worked well.

Shortcut to run a project in VS 2012

I'm working on a large Silverlight solution with 75+ projects. Instead of right clicking on the start up project and then selecting Debug | Start New Instance... I'd like to use a keyboard shortcut to run this specific project. Hitting F5 is a non-starter, even if I select the option to only build the start up project and it's dependencies. Takes too long.
Accomplishing this was easy with the macro functionality in VS 2010. I haven't been able to find a good way to replace this capability in VS2102. Note, I have tried the Visual Commander extension but it doesn't seem to be able to automate this kind of command sequence.
While you can't record it automatically, you can just paste this code as a VB command in Visual Commander with your project name:
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate()
DTE.ActiveWindow.Object.GetItem("MySolution\MyProject").Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance")

How to choose which file to debug in Visual Studio 2012 prof

Kinda stupid question, but I've only been using this IDE for two days now. I havent found anything in google yet and I figured that once in a while you're entitled to a stupid question:
I have a project (or solution as Microsoft chose to call it) with two .cs-files. When I click on debug, it's alwys the same file that opens. How can I get VS to open the other .cs-file?
It sounds like you have multiple static Main methods in your project. If that's the case then you have to set the startup object to the object that you want to run.
Right-click on the project in the Solution Explorer and select
Properties (or select Project >> Projectname Properties from the
toolbar).
On the Application form, select the appropriate value
for Startup Object.

Converting "Some quote enclosed text" to Some_underlined_text with resharper

I have up to now been using a macro to convert "This sort of text" to This_sort_of_text.
This is very useful for typing test class names.
(Details here: http://blogs.msdn.com/b/elee/archive/2010/02/28/my-bdd-naming-macro.aspx)
Unfortunately Visual Studio 11 (2012) does not support macros any more.
Can the same be done with resharper?
If not i might look into creating a VS Extension... unless anyone knows one exists.
You can develop a ReSharper plugin for it, it could be easier than a VS extension. See here for more info Resharper API for selected text and remote code generation . You can use document.ReplaceText method to make modifications.

Resources