A simple editing function: Insert Date
I created a simple insert date, the exact function is: insert the current date in the current cursor location or the current selection text. This is the example of the Visual Studio .NET already contains an example of this function with macros. So you can see the same functions through two different ways through macro and plug-ins. This example is very simple, just modify the EXEC function listed in the previous example. Through the previous chapter I have created a basic plugin, the name of this plugin is "TextUtil" is more exactly "TextUtil.connect.TextUtil". In the Exec function I replaced the code for the wizard automatically generated.
Handled = True;
Replace
Handled = INSERTDATE ();
Increase the following INSERTDATA () function, you can add this function anywhere in the Connect object.
Private bool insertdata ()
{
ApplicationObject.actiVedocument! = null)
(TextSelection) ApplicationObject.actiVedocument.selection .Text
= DATETIME.NOW.TOSTRING ("YYY-MM-DD");
Return True;
}
The INSERTDATA function uses System.DateTime and Sytem.String objects. About System.String introduction can be found in the help. Here I noticed three lines of code about Commandbars in OnConnection, this three-line code is to create a menu item in the Tools menu during the process of running.
In addition to the name plugin has been completed. Press F5 to test the plugin. A new IDE instance is running, in this instance, this plugin will appear in the Tools | Add-Explorer Administration dialog. The plugin can be loaded by selecting the leftmost check box in the re-adder management dialog box. Now you can open any text file, then enter "TextUtil.command.TextUtil" in the command window of Visual Studio .Net, you will insert the current date in the current cursor. In fact, the automatic variation has been filled in the command before you complete the input.
When you press F5 to start running an IDE new instance, your plugin is in debug mode. We can discover all exception information in debug mode. Any exception appears in a normal operation, they may not be available. As much as possible to get an abnormal information, then the cause of the error will discover as soon as possible.
Change the name of the command
The wizard has specified the default name (TextUtil) for the menu command. So this name does not have a distinction, I will add more menu commands to change the name of the menu command. At the same time, because I encountered a chaotic situation when changing the name of the command, we must confirm that our plugin will not have the same command name.
The first change is to call AddNameCommand in the onConnection function.
Command command = commands.addnamedcommand (AddinInstance,
"Insertdate",
"INSERT CURRENT DATE",
"INSERT CURRENT DATE",
True, 59, Ref contexTguids,
(int) vscommandstatus.vscommandstatussupported
(int) vscommandstatus.vscommandstatusenable);
Changed in QuertysTatus as follows:
IF (commandname.tolower () == "textusconnect.insertdate")
The wizard should be registered with the plugin information before the initial run load, but the change in the name affects the registration information. This means that we need new plug-in projects and installation items when adding the plug-in function. Then run the installer. Figure 3 Operating plugins (description: Running the macro translator: may be wrong)
just now"
TextUtil.connect.insertdate
"The command can work according to my thoughts (Figure
3
). But how did he work? Let us continue to look down.