|
| 1 | +--- |
| 2 | +title: Creating User Functions in Visual Studio Report Designer |
| 3 | +description: Learn how to create custom user functions in VS Report Designer. |
| 4 | +type: how-to |
| 5 | +page_title: How to Create and Utilize User Functions in VS Report Designer |
| 6 | +slug: creating-user-functions-in-vs-designer |
| 7 | +tags: reporting, user, function, designer, vs, visual, studio, net, framework |
| 8 | +res_type: kb |
| 9 | +ticketid: 1680822 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| ---- | ---- | ---- | |
| 16 | +| | Visual Studio Report Designer |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +This tutorial shows how to create and utilize custom [user functions]({%slug telerikreporting/designing-reports/connecting-to-data/expressions/extending-expressions/user-functions%}) in the [Visual Studio Report Designer]({%slug telerikreporting/designing-reports/report-designer-tools/desktop-designers/visual-studio-report-designer/overview%}). |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +Let's start with creating a Class library (**.NET Framework**) project and add a **Report** class using the [Item Templates]({%slug treporting-integration-with-visual-studio%}) in Visual Studio. |
| 25 | + |
| 26 | +### User Functions Defined in the Report class |
| 27 | + |
| 28 | +If your custom user function as a **public static** (**Public Shared** in *VB.NET*) method is a part of the current Report class: |
| 29 | + |
| 30 | +````CSharp |
| 31 | +public partial class Report1 : Telerik.Reporting.Report |
| 32 | +{ |
| 33 | + public Report1() |
| 34 | + { |
| 35 | + InitializeComponent(); |
| 36 | + } |
| 37 | + |
| 38 | + [Function(IsVisible = true, Category = "My Functions", Namespace = "My", Description = "Say Hi")] |
| 39 | + public static string Greet(string name) |
| 40 | + { |
| 41 | + return string.Format("Hello, {0}", name); |
| 42 | + } |
| 43 | +} |
| 44 | +```` |
| 45 | + |
| 46 | +It can be invoked from an expression by its name, specifying the necessary parameters in the braces, e.g., setting the **Value** of a **TextBox** to: |
| 47 | + |
| 48 | +```` |
| 49 | += Greet("Dess") |
| 50 | +```` |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +### User Functions Defined in an External Assembly |
| 55 | + |
| 56 | +1. If your custom user functions are linked from an external assembly (e.g., MyFunctions.dll), for the designer to recognize them, you will have to extend the configuration of the start application. |
| 57 | + |
| 58 | + For the Visual Studio Report Designer, this is the **'devenv.exe.config'** file that resides in *'C:\Program Files (x86)\Microsoft Visual Studio X.0\Common7\IDE'* by default (it is recommended to create a backup copy before modifying it). |
| 59 | + |
| 60 | + >important Do not forget to close the Visual Studio instance before modifying its configuration file. Run `Notepad` (or other editing tool) as an administrator to perform the changes. |
| 61 | + |
| 62 | + ````XML |
| 63 | +<configSections> |
| 64 | + <section |
| 65 | + name="Telerik.Reporting" |
| 66 | + type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting" |
| 67 | + allowLocation="true" |
| 68 | + allowDefinition="Everywhere"/> |
| 69 | + </configSections> |
| 70 | + <Telerik.Reporting> |
| 71 | + <AssemblyReferences> |
| 72 | + <add name="MyFunctions" /> |
| 73 | + </AssemblyReferences> |
| 74 | + </Telerik.Reporting> |
| 75 | +```` |
| 76 | +
|
| 77 | +
|
| 78 | +1. Copy the custom assembly in the same folder as the devenv.exe.config. |
| 79 | +1. You can type the expression by specifying the full name of the function and passing a parameter of the expected type. |
| 80 | +
|
| 81 | + ```` |
| 82 | += MyFunctions.GetImageFromURL("image url") |
| 83 | +```` |
| 84 | + |
| 85 | +The custom function accepts an image url and returns an Image object that can be assigned to a PictureBox: |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +## See Also |
| 90 | + |
| 91 | +- [Visual Studio Templates]({%slug treporting-integration-with-visual-studio%}) |
| 92 | +- [Custom User Functions]({%slug telerikreporting/designing-reports/connecting-to-data/expressions/extending-expressions/user-functions%}) |
0 commit comments