Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add step to change base class #1819

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading