Inside Man
Inside Man

Reputation: 4305

Change Naming Conventions for Custom Control in VS Designer - C#

I have created a custom textbox like this:

public class CustomTextBox : TextBox
{
    // Some Customization here
}

after the rebuild I can see customTextbox in the toolbox, in design mode whenever I add one of these controls into the form, its name will be customTextBox1, I need to change this naming Conventions to txtControl1. whenever a user adds one of these custom text boxes, its name should start with txtControl by default.

how to achieve this?

Upvotes: 1

Views: 96

Answers (1)

Cody Liang
Cody Liang

Reputation: 1191

After my test, INameCreationService can indeed achieve the effect you want. You can refer to the following steps:

1: Create a Windows Forms control library project based on .NET Framework.

2: Add a custom control class CustomTextBox to the project:

using System.ComponentModel;
using System.Windows.Forms;

namespace CustomControlDemo
{
    // Specify the designer as the CustomTextBoxDesigner that we will implement later
    [Designer(typeof(CustomTextBoxDesigner))]
    public class CustomTextBox : TextBox
    {
        // You can add other custom logic here
    }
}

3: Add the CustomTextBoxDesigner class to implement the custom designer:

using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms.Design;

namespace CustomControlDemo
{
    public class CustomTextBoxDesigner : ControlDesigner
    {
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            IServiceContainer serviceContainer = (IServiceContainer)GetService(typeof(IServiceContainer));
            if (serviceContainer != null)
            {
                INameCreationService existingService = serviceContainer.GetService(typeof(INameCreationService)) as INameCreationService;
                if (!(existingService is CustomNameCreationService))
                {
                    serviceContainer.RemoveService(typeof(INameCreationService), true);
                    serviceContainer.AddService(typeof(INameCreationService), new CustomNameCreationService(), true);
                }
            }
        }
    }
}

4: Add the CustomNameCreationService class to implement the customization

using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;

namespace CustomControlDemo
{
    public class CustomNameCreationService : INameCreationService
    {
        public string CreateName(IContainer container, Type dataType)
        {
            Debug.WriteLine($"CreateName called for type: {dataType.Name}");

            if (container == null)
                throw new ArgumentNullException(nameof(container));
            if (dataType == null)
                throw new ArgumentNullException(nameof(dataType));

            string baseName = "txtControl";
            int index = 1;
            while (container.Components[baseName + index] != null)
            {
                index++;
            }

            string newName = baseName + index;
            Debug.WriteLine($"Generated name: {newName}");
            return newName;
        }

        public bool IsValidName(string name)
        {
            return !string.IsNullOrEmpty(name);
        }

        public void ValidateName(string name)
        {
            if (!IsValidName(name))
                throw new Exception("Invalid name: " + name);
        }
    }
}

5: Save the file and generate the solution. Create a new Windows Forms application project (.NET Framework) and reference the control library project just now. Open the form designer. Find CustomTextBox from the toolbox and drag and drop it.

6: Effect diagram: enter image description here

One thing to note: Unlike the .NET Framework legacy designer, in the .NET version of the WinForms designer, the INameCreationService does not appear to be used or is not registered correctly.

Upvotes: 1

Related Questions