Visual| maximize a simple editing function: Insert Date
I created a simple insertion date feature that inserts the current date at the current cursor position or where the text is currently selected. This is visual Studio. NET already contains an example of using macros to implement this functionality. So you can see that the same functionality is done with macros and Plug-ins in two different ways. This example is very simple and requires you to modify one of the exec functions listed earlier. Through the previous chapters I have created a basic plugin, the name of which is "textutil" more specifically "TextUtil.Connect.TextUtil". In the EXEC function I replaced the code that the wizard automatically generated.
handled = TRUE;
Replaced by
handled = Insertdate ();
Add the following insertdata () function to add the function anywhere in the Connect object.
private bool InsertData ()
{
if (applicationobject.activedocument!= null)
((TextSelection) applicationObject.ActiveDocument.Selection). Text
=datetime.now.tostring ("Yyy-mm-dd");
return true;
}
The InsertData function uses the System.DateTime and Sytem.string objects. You can refer to the Help for an introduction to System.String. Here I've noticed three lines of code in OnConnection about CommandBars, which is to create menu items in the Tools menu during a later run.
Except the name plugin has been completed. Press F5 to test the plugin. A new instance of the IDE runs, in which case the plugin will appear in the Tools | add-in Management dialog box. You can load plug-ins by selecting the leftmost check box in the Add-Ins Management dialog box. Now you can open any text file and then visual Studio. NET, enter "TextUtil.Command.TextUtil" at the current cursor position in the command window to insert the current date. In fact, the automatic mutation already fills the command before you finish typing.
When you press F5 to start running a new instance of the IDE, your plugin is in debug mode. We can find all the exception information in debug mode. Any exceptions appear in normal operations, and they may not be available. If you get as much information as possible, the cause of the error will be discovered as soon as possible.
Change command name
The wizard has specified a default name (Textutil) for the menu command. So this name is not a feature description, I will add more menu commands, change the menu command name. At the same time, because I encountered a name disorder when I changed the command name, we had to make sure that our plugin would not have the same command name.
The first change is where the Addnamecommand is called in the OnConnection function.
if (commandname.tolower () = = "Textutil.connect.insertdate")
The wizard should register the plug-in information before the initial run load, but the change in name affects the registration information. This means that we need to regenerate the plug-in project and install the project when the plug-in function is added. Then run the installer again.
Figure 3 Running the plugin (original: Running the Macro Translator: May be wrong)
Now the "TextUtil.Connect.InsertDate" command can be worked out in my mind (Figure 3). But how does he work? Let's keep looking down.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.