From b8531632cc304856c63f05d8fbf0b16c89744d5e Mon Sep 17 00:00:00 2001 From: "Andy De George (adegeo)" Date: Tue, 30 Apr 2024 12:32:22 -0700 Subject: [PATCH 1/4] Add step --- .../net/winforms/controls-design/extend-existing.md | 9 +++++++-- .../extend-existing/csharp/CustomControlProject.csproj | 2 +- .../extend-existing/vb/CustomControlProjectVB.vbproj | 6 +----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md index 1b72e019b9..cb5813511a 100644 --- a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md +++ b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md @@ -1,7 +1,7 @@ --- 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" @@ -9,6 +9,7 @@ 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 , 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 method to change the way the control looks. @@ -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"::: diff --git a/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/csharp/CustomControlProject.csproj b/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/csharp/CustomControlProject.csproj index e1a0735019..663fdb88b6 100644 --- a/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/csharp/CustomControlProject.csproj +++ b/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/csharp/CustomControlProject.csproj @@ -2,7 +2,7 @@ WinExe - net7.0-windows + net8.0-windows enable true enable diff --git a/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/vb/CustomControlProjectVB.vbproj b/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/vb/CustomControlProjectVB.vbproj index 26b38e2551..a3579038b2 100644 --- a/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/vb/CustomControlProjectVB.vbproj +++ b/dotnet-desktop-guide/net/winforms/controls-design/snippets/extend-existing/vb/CustomControlProjectVB.vbproj @@ -2,7 +2,7 @@ WinExe - net7.0-windows + net8.0-windows Sub Main true WindowsForms @@ -15,10 +15,6 @@ - - - - True From a9413db8e5ab08897e9cda334646f588f0f7dfcf Mon Sep 17 00:00:00 2001 From: "Andy De George (adegeo)" Date: Tue, 30 Apr 2024 12:39:12 -0700 Subject: [PATCH 2/4] minor acro improvements --- .../net/winforms/controls-design/extend-existing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md index cb5813511a..7ec2891c41 100644 --- a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md +++ b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md @@ -44,7 +44,7 @@ After [you add a custom control to your project](#add-a-custom-control-to-a-proj 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`. + If you're using Visual Basic, the base class is defined in the _\*.designer.vb_ file of your control. Open that file and set the base class to `System.Windows.Forms.Button`. 01. Add a class-scoped variable named `_counter`. @@ -66,6 +66,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."::: From 5988e884e7fa954e87109300ad255ea2b3961fe8 Mon Sep 17 00:00:00 2001 From: "Andy De George (adegeo)" Date: Tue, 30 Apr 2024 12:52:25 -0700 Subject: [PATCH 3/4] minor --- .../net/winforms/controls-design/extend-existing.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md index 7ec2891c41..311a778069 100644 --- a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md +++ b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md @@ -44,7 +44,8 @@ After [you add a custom control to your project](#add-a-custom-control-to-a-proj 01. Change the base class from `Control` to `Button`. - If you're using Visual Basic, the base class is defined in the _\*.designer.vb_ file of your control. Open that file and set the base class to `System.Windows.Forms.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`. From 710a9b7fb6cd58e2c2f08ce15cdbd2eeb5c43c87 Mon Sep 17 00:00:00 2001 From: "Andy De George (adegeo)" Date: Tue, 30 Apr 2024 13:58:46 -0700 Subject: [PATCH 4/4] highlight VB in important note --- .../net/winforms/controls-design/extend-existing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md index 311a778069..c50ca34b56 100644 --- a/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md +++ b/dotnet-desktop-guide/net/winforms/controls-design/extend-existing.md @@ -45,7 +45,7 @@ After [you add a custom control to your project](#add-a-custom-control-to-a-proj 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`. + > 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`.