How to store and use reusable code from Visual Studio tool box

Microsoft Visual Studio provides powerful features helping in rapid code development and makes developers productive. In this article introducing one such feature that reduces expensive copy-paste operations when there is a same piece code snippet to be reused in multiple code blocks. Yes, it’s about how to store and use reusable code from the Visual Studio tool box.



For example: Suppose if you need to add trace log-related code in each code block, then most of the time, you copy the existing line of a code that you already wrote in some code file. Copy and paste operation is expensive when there is a common code to be written across the project.

Store and use reusable code from Visual Studio toolbox:

In Visual Studio, you can keep this piece of code in the toolbox, and you can reuse this code when required. You need to drag and drop the code line from the toolbox to your code editor pane. This is similar to drag-drop Button control to form.

Follow the below steps to see how to move code snippets into the toolbox in Visual Studio.


1
In the Visual Studio Code editor, select the most reusable code snippet. Once the selection is made, drag and drop that code into Tool box pane. The code snippet gets added into the toolbox under the general group.




2
On the toolbox, select the code snippet item and right-click to open the context menu. On the context menu, select Rename menu item. Now, the code snippet item in the toolbox becomes editable. Rename this item to some name through which you can identify what code is inside.



]


3
Once renamed, drag and drop the code snippet item from the toolbox to the code window. It will copy the respective code snippet at the position where the cursor blinks. You can do this any number of times whenever this piece of code is required in your project.




Code snippets helps developers minimize the costliest operations, such as copying the reusable code from one code file and using it. Here you can watch the entire steps in one Video.




– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Recommended:   Tuples in C# - Return more than one value from Method
Share this:
We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo