# Creating Style Rules

Style definitions can be stored in a Style Rule. Style Rules can be created in the designer or in code.

Style Rules are defined as one of the following four types:

* A **TypeSelector** applies to all report items of a particular type.
* An **AttributeSelector** applies to all report items with a particular attribute (such as Color=Red).
* A **StyleSelector** applies to all report items with a particular style name.
* A **DescendantSelector** applies to all report items descended from another report item, which in turn can be specified by any type of selector.

See [Understanding Style Selectors](https://www.telerik.com/products/reporting/documentation/styling/styling-reports/understanding-style-selectors) for more information about the types of Style Rules.

## Creating Rules Using the StyleRule Collection Editor

1. Open the report in design view.
2. Select the Report object.
3. Click in the **StyleSheet** property, and then click the ellipsis button to open the StyleRule Collection Editor.

## Creating a TypeSelector StyleRule

### Using the StyleRule Collection Editor

1. In the StyleRule Collection Editor, click **Add**. A new rule will be added to the Members list and you will see the Selectors and Style properties for the rule.

   ![Image of the StyleRule Collection Editor window, showing the existing style rules and the properties of the currently selected style rule](https://www.telerik.com/products/reporting/documentation/assets/e61986afd4277bfc23e06d91a899f8f0/reportstylerulea.png)

2. Click the **Selectors** property, then click the ellipsis button that appears to the right of **(Collection)**. This action will open TypeSelector Collection Editor.

3. Click the drop-down arrow on the **Add** button to choose the type of Selector to create.

4. Click **TypeSelector** from the list. Note: TypeSelector is the default type created when you click the **Add** button.

   ![Image of the TypeSelector Collection Editor window, showing the available values of the Add dropdown button. The TypeSelector option is highlighted](https://www.telerik.com/products/reporting/documentation/assets/13af0ee2c64611c0cd9481a5cec6b0fa/reportstyleruleb.png)

5. Click the **Type** property of the new TypeSelector, and then click the ellipsis button.

   ![Image of the TypeSelector Collection Editor window, showing the ellipsis button found next to the Type property of the currently selected type selector](https://www.telerik.com/products/reporting/documentation/assets/3d19e69ca83fa5da22adb243291520fd/reportstylerulec.png)

6. Select the **Telerik.Reporting.TextBox** and click **OK**. The StyleRule will now apply to all **TextBox** report items on the report.

7. Click the plus sign next to **Style** to expand the **Style** properties.

8. Modify any properties you wish to define formatting to be applied to the **TextBox** report item.

9. Click **OK** to save the rules and close the editor window.

   ![Image of the StyleRule Collection Editor window, showing the expanded Style properties of the currently selected TypeSelector rule](https://www.telerik.com/products/reporting/documentation/assets/839fdb1434110b606fee113a0cf82c9c/reportstyleruled.png)

### Creating a TypeSelector StyleRule in Code

The following code demonstrates the four steps required to create a **TypeSelector** **StyleRule** programmatically:

1. Create a new **StyleRule** called **MyStyleRule**.
2. Add a **TypeSelector** to the **StyleRule** and define the type as **Telerik.Reporting.TextBox**.
3. Apply some formatting rules to the **StyleRule**.
4. Add the **StyleRule** to the **Style Sheet**.

```C#
//Create a StyleRule
Telerik.Reporting.Drawing.StyleRule myStyleRule = new Telerik.Reporting.Drawing.StyleRule();
//Add a TypeSelector
myStyleRule.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[]
{
    new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.TextBox))
});
//Add formatting
myStyleRule.Style.BackgroundColor = System.Drawing.Color.Linen;
myStyleRule.Style.Color = System.Drawing.Color.DodgerBlue;
myStyleRule.Style.Font.Name = "Courier New";
//Add rule to Style Sheet
this.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] { myStyleRule });
```

```VB.NET
'Create a StyleRule
Dim MyStyleRule As Telerik.Reporting.Drawing.StyleRule = New Telerik.Reporting.Drawing.StyleRule
'Add a TypeSelector
MyStyleRule.Selectors.AddRange(New Telerik.Reporting.Drawing.ISelector() _ 
{New Telerik.Reporting.Drawing.TypeSelector(GetType(Telerik.Reporting.TextBox))})
'Add formatting
MyStyleRule.Style.BackgroundColor = System.Drawing.Color.Linen
MyStyleRule.Style.Color = System.Drawing.Color.DodgerBlue
MyStyleRule.Style.Font.Name = "Courier New"
'Add rule to Style Sheet
Me.StyleSheet.AddRange(New Telerik.Reporting.Drawing.StyleRule() {MyStyleRule})
```

## Creating a StyleSelector StyleRule

### Using the StyleRule Collection Editor

1. In the StyleRule Collection Editor, click **Add**. A new rule will be added to the Members list and you will see the **Selectors** and **Style** properties for the rule.

2. Click the **Selectors** property, and then click the ellipsis button to open the TypeSelector Collection Editor window.

3. Click the drop-down arrow on the **Add** button to choose the type of Selector to create.

4. Click **StyleSelector** from the list.

5. Type a name for the Style into the **StyleName** property.

   ![Image of the TypeSelector Collection Editor window, showing the properties of the currently selected StyleSelector](https://www.telerik.com/products/reporting/documentation/assets/f713bd39d679a8899f5bc3c38c70d724/reportstylerulee.png)

6. Click **OK** to return to the StyleRule Collection Editor.

7. Expand the **Style** property and modify the formatting properties as desired.

8. When finished, click **OK** to close the StyleRule Collection Editor.

### Creating a StyleSelector StyleRule in Code

The following code demonstrates the four steps required to create a **StyleSelector** **StyleRule** programmatically:

1. Create a new **StyleRule** called **MyStyleRule**.
2. Add a **TypeSelector** to the **StyleRule** and give it a **StyleName** , such as CaptionStyle.
3. Apply some formatting rules to the **StyleRule**.
4. Add the **StyleRule** to the **Style Sheet**.

```C#
//Create a StyleRule
Telerik.Reporting.Drawing.StyleRule myStyleRule = new Telerik.Reporting.Drawing.StyleRule();
//Add a StyleSelector
myStyleRule.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[]
{
    new Telerik.Reporting.Drawing.StyleSelector("CaptionStyle")
});
//Add formatting
myStyleRule.Style.BackgroundColor = System.Drawing.Color.Linen;
myStyleRule.Style.Color = System.Drawing.Color.DodgerBlue;
myStyleRule.Style.Font.Name = "Courier New";
//Add rule to Style Sheet
this.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] { myStyleRule });
```

```VB.NET
'Create a StyleRule
Dim MyStyleRule As Telerik.Reporting.Drawing.StyleRule = New Telerik.Reporting.Drawing.StyleRule 
'Add a StyleSelector
MyStyleRule.Selectors.AddRange(New Telerik.Reporting.Drawing.ISelector() _
{New Telerik.Reporting.Drawing.StyleSelector("CaptionStyle")})
'Add(formatting)
MyStyleRule.Style.BackgroundColor = System.Drawing.Color.Linen
MyStyleRule.Style.Color = System.Drawing.Color.DodgerBlue
MyStyleRule.Style.Font.Name = "Courier New"                   
'Add rule to Style Sheet
Me.StyleSheet.AddRange(New Telerik.Reporting.Drawing.StyleRule() {MyStyleRule})
```

### Applying StyleSelector StyleRules

Each report item has a **StyleName** property.

Type the **StyleName** of the desired **StyleRule** into the report item's **StyleName** property to apply the formatting of the **StyleRule**.

![Image showing the properties window of a TextBox item, with the StyleName property highlighted in blue](https://www.telerik.com/products/reporting/documentation/assets/1683a4e85fa5a1500685fc86982901e5/reportstylerulef.png)

## Creating a DescendantSelector StyleRule

### Using the StyleRule Collection Editor

1. In the StyleRule Collection Editor, click **Add**. A new rule will be added to the Members list and you will see the **Selectors** and **Style** properties for the rule.

2. Click the **Selectors** property, and then click the ellipsis button to open the TypeSelector Collection Editor window.

3. Click the drop-down arrow on the **Add** button to choose the type of Selector to create.

4. Click **DescendantSelector** from the list.

   ![Image of the TypeSelector Collection Editor window, showing the available values of the Add dropdown button. The DescendantSelector option is highlighted](https://www.telerik.com/products/reporting/documentation/assets/8d01c25923b51bd6c61cd96b215d2d34/reportstyleruleh.png)

   The **DescendantSelector** is made up of additional **Selectors**.

5. Click the **Selectors** property, and then click the ellipsis button to open up a new TypeSelector Collection Editor window.

   ![Image of the TypeSelector Collection Editor window, showing the ellipsis button next to the Selectors property of the currently selected DescendantSelector](https://www.telerik.com/products/reporting/documentation/assets/059fa4d5c006a7cc586e9648de8c4858/reportstylerulei.png)

   In this new TypeSelector Collection Editor window, you will define the Selectors that make up the DescendantSelector.

6. Click **Add** to add a new **TypeSelector**.

7. Click **Type** , and then click the ellipsis button to open up the Select Type dialog.

8. Select the **Telerik.Reporting.DetailSection** type.

9. Add another new **TypeSelector** and set its type to **Telerik.Reporting.TextBox**.

   ![Image of the TypeSelector Collection Editor window, showing that an additional TypeSelector of type Telerik.Reporting.TextBox has been added to the collection](https://www.telerik.com/products/reporting/documentation/assets/eb0cf3cd5639a9c17a5e40e833eb0643/reportstylerulej.png)

10. Click **OK** to close the window and return to the TypeSelector Collection Editor window for the DescendantSelector.

11. Click **OK** to close this window and return to the StyleRule Collection Editor window.

    ![Image of the StyleRule Collection Editor window, showing the properties of the DescendantSelector after all the changes have been made](https://www.telerik.com/products/reporting/documentation/assets/6a6691e5772f709912f93e9063de3dc3/reportstylerulek.png)

12. Expand the **Style** property to define the formatting for the **DetailSection TextBox StyleRule**.

13. Click **OK** when complete.

### Creating a DescendantSelector StyleRule in Code

The following code demonstrates the four steps required to create a **DescendantSelector** **StyleRule** programmatically:

1. Create a new **StyleRule** called **MyStyleRule**.
2. Create a new **DescendantSelector**.
3. Add **Selectors** to the **DescendantSelectors**. If you are using **TypeSelectors** , define the types too.
4. Add a **TypeSelector** to the **StyleRule** and give it a **StyleName** , such as CaptionStyle.
5. Apply some formatting rules to the **StyleRule**.
6. Add the **StyleRule** to the **Style Sheet**.

```C#
//Create StyleRule and DescendantSelector
Telerik.Reporting.Drawing.StyleRule myStyleRule = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.DescendantSelector myDescendantSelector = new Telerik.Reporting.Drawing.DescendantSelector();
//Define the Selectors and Types of the DescendantSelector
myDescendantSelector.Selectors.AddRange(
    new Telerik.Reporting.Drawing.ISelector[]
    {
        new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.ReportHeaderSection)),
        new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.TextBox))
    });
//Add the DescendantSelector to the StyleRule
myStyleRule.Selectors.AddRange(
    new Telerik.Reporting.Drawing.ISelector[] { myDescendantSelector });
//Apply Formatting
myStyleRule.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.Ridge;
myStyleRule.Style.Color = System.Drawing.Color.Navy;
myStyleRule.Style.Font.Name = "Arial";
//Add rule to Style Sheet
this.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] { myStyleRule });
```

```VB.NET
'Create StyleRule and DescendantSelector
Dim MyStyleRule As Telerik.Reporting.Drawing.StyleRule = New Telerik.Reporting.Drawing.StyleRule
Dim MyDescendantSelector As Telerik.Reporting.Drawing.DescendantSelector =
New Telerik.Reporting.Drawing.DescendantSelector
'Define the Selectors and Types of the DescendantSelector
MyDescendantSelector.Selectors.AddRange(New Telerik.Reporting.Drawing.ISelector() _
{New Telerik.Reporting.Drawing.TypeSelector(GetType(Telerik.Reporting.ReportHeaderSection)),
New Telerik.Reporting.Drawing.TypeSelector(GetType(Telerik.Reporting.TextBox))})
'Add the DescendantSelector to the StyleRule
MyStyleRule.Selectors.AddRange(New Telerik.Reporting.Drawing.ISelector() {DescendantSelector1})
'Apply formatting
MyStyleRule.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.Ridge
MyStyleRule.Style.Color = System.Drawing.Color.Navy
MyStyleRule.Style.Font.Name = "Arial"
'Add the StyleRule to the Style Sheet
Me.StyleSheet.AddRange(New Telerik.Reporting.Drawing.StyleRule() {MyStyleRule})
```
