Skip to content

Commit

Permalink
Merge branch 'main' into live
Browse files Browse the repository at this point in the history
  • Loading branch information
cxwtool committed May 1, 2024
2 parents 69fc4fd + ffc0293 commit f4c0385
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
title: Customize an existing control
description: Learn how to inherit from existing controls so that another control has all of its functionality and visual properties.
ms.date: 06/01/2023
ms.date: 04/30/2024
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "inheritance [Windows Forms], Windows Forms custom controls"
- "custom controls [Windows Forms], inheritance"
---

# Extend an existing control

If you want to add more features to an existing control, you can create a control that inherits from an existing control. The new control contains all of the capabilities and visual aspect of the base control, but gives you opportunity to extend it. For example, if you created a control that inherits <xref:System.Windows.Forms.Button>, your new control would look and act exactly like a button. You could create new methods and properties to customize the behavior of the control. Some controls allow you to override the <xref:System.Windows.Forms.Control.OnPaint%2A> method to change the way the control looks.
Expand Down Expand Up @@ -41,7 +42,12 @@ After [you add a custom control to your project](#add-a-custom-control-to-a-proj
:::code language="csharp" source="./snippets/extend-existing/csharp/CustomControl2.cs" id="control":::
:::code language="vb" source="./snippets/extend-existing/vb/CustomControl2.vb" id="control":::

01. First, add a class-scoped variable named `_counter`.
01. Change the base class from `Control` to `Button`.

> [!IMPORTANT]
> If you're using **Visual Basic**, the base class is defined in the _\*.designer.vb_ file of your control. The base class to use in Visual Basic is `System.Windows.Forms.Button`.
01. Add a class-scoped variable named `_counter`.

:::code language="csharp" source="./snippets/extend-existing/csharp/CustomControl1.cs" id="counter":::
:::code language="vb" source="./snippets/extend-existing/vb/CustomControl1.vb" id="counter":::
Expand All @@ -61,6 +67,6 @@ After [you add a custom control to your project](#add-a-custom-control-to-a-proj
:::code language="csharp" source="./snippets/extend-existing/csharp/CustomControl1.cs" id="control":::
:::code language="vb" source="./snippets/extend-existing/vb/CustomControl1.vb" id="control":::

Now that the control is created, compile the project to populate the **Toolbox** window with the new control. Open a form designer and drag the control to the form. When you run the project and click the button, you'll see that it counts the clicks and paints the text on top of the button.
Now that the control is created, compile the project to populate the **Toolbox** window with the new control. Open a form designer and drag the control to the form. Run the project and press the button. Each press increases the number of clicks by one. The total clicks are printed as text on top of the button.

:::image type="content" source="media/extend-existing/toolbox.png" alt-text="Visual Studio Toolbox window for Windows Forms showing a custom control.":::
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<StartupObject>Sub Main</StartupObject>
<UseWindowsForms>true</UseWindowsForms>
<MyType>WindowsForms</MyType>
Expand All @@ -15,10 +15,6 @@
<Import Include="System.Windows.Forms" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\csharp\CustomControlProject.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="My Project\Application.Designer.vb">
<DesignTime>True</DesignTime>
Expand Down

0 comments on commit f4c0385

Please sign in to comment.