How to add a custom property?

BenTam 1,801 Reputation points
2024-01-18T04:50:05.02+00:00

Dear All,

On another thread (see https://learn.microsoft.com/en-us/answers/questions/1494052/how-to-add-custom-properties-in-default-controls?source=docs), Jose Pablo Ramirez gave me the answer.

Yes, one can further add to controls as they are just classes, like any other class. Create a new class and derive from TextBox:

C#

Copy
public class MyTextBox : TextBox
{
    [Browsable(true)]
    public string AllCapsText
        => Text.ToUpper();
}

Then I ask him for a comment.

  1. Create a new project "ControlLibrary" and add your class to the project, i.e., right-click on the project name and choose Add->Class. On the code window of the new class, paste the code you provided.
  2. Add the new project "ControlLibrary" to my project "NewTims".
  3. Drag "MyTextBox" from the Toolbox to a form of my project "NewTims"
  4. I find the Property "AllCapsText" gray. When I press "Start" to run the form and enter text to the "myTextBox1", the text does not automatically turn capital.
    CustomProperty

Then I ask him "How to solve the problems?". However, so far I haven't received any reply. I didn't know if he didn't receive my comment. Since I typed @José Pablo Ramirez, normally after I entered "@J" a popup menu will be shown. However, when on this popup menu, I didn't find his name. Therefore I didn't know if he received my comment. If you can solve my problem, please help. TIA

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-01-18T05:32:44.48+00:00

    Hi @BenTam-3003 , Welcome to Microsoft Q&A,

    What you want and what you asked are not the same in the previous question.

    I have carefully analyzed your current problem. You need to add a textchange event in the textbox to change the case. The specific code is as follows:

    using System;
    using System.Windows.Forms;
    
    namespace xxxx
    {
        public partial class MyTextBox : TextBox
        {
            private bool allCapsText;
            public MyTextBox()
            {
                InitializeComponent();
                this.TextChanged += MyTextBox_TextChanged;
            }
            private void MyTextBox_TextChanged(object sender, EventArgs e)
            {
                if (AllCapsText)
                {
                    int selectionStart = this.SelectionStart;
                    int selectionLength = this.SelectionLength;
    
                    this.Text = this.Text.ToUpper();
    
                    this.SelectionStart = selectionStart;
                    this.SelectionLength = selectionLength;
                }
            }
    
            public bool AllCapsText
            {
                get { return allCapsText; }
                set { allCapsText = value; }
            }
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
        }
    }
    
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.