Skip to content

Commit

Permalink
Add step
Browse files Browse the repository at this point in the history
  • Loading branch information
adegeo committed Apr 30, 2024
1 parent 75682b3 commit b853163
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 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,11 @@ 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`.

If you're using Visual Basic, you need to open the _\*.designer.vb_ file of your control and set the base class to `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 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>
<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 b853163

Please sign in to comment.