Customtextbox not adding from designer

Pankaj tripathi 125 Reputation points
2023-10-28T13:22:08.32+00:00

Hello ,

i recently asked a question in https://learn.microsoft.com/en-us/answers/questions/1406472/textbox-as-textboxcolumn
and @gekka suggested a solution which works . I had modified some part of it according to my requirement but after cleaning and rebuilding multiple times it can not be drop from the toolbar to the form . i am not from the visual studio design . Am i missing something or it is a bug . Please help .
modifications are

  1. can't take more than specified characters
  2. should have backcolor according to the containing form when dropped on it
  3. should change backcolor when gets focused
  4. should change back to original color
namespace Pankajtripathi
{
    public partial class CustomTextBox : TextBox
    {
        private int maxCharacters;

        public int MaxCharacters
        {
            get { return maxCharacters; }
            set
            {
                maxCharacters = value;
                Text = Text.Length > maxCharacters ? Text.Substring(0, maxCharacters) : Text;
            }
        }

        public CustomTextBox()
        {
            CustomTextBoxWork.AttachKeyEvent(this);
        }
    }

    class CustomTextBoxWork
    {
        private static Color originalBackColor;

        public static void AttachKeyEvent(TextBox txb)
        {
            txb.KeyDown += Txb_KeyDown;
            txb.LostFocus += Txb_LostFocus;
            originalBackColor = txb.Parent.BackColor; // Set the originalBackColor to the parent's back color
        }

        private static void Txb_KeyDown(object sender, KeyEventArgs e)
        {
            var txb = (TextBox)sender;
            if (e.KeyCode == Keys.Escape)
            {
                txb.Text = "";
            }
            else if (e.KeyCode == Keys.Enter)
            {
                txb.BackColor = Color.Yellow; // Change the back color to a different color when Enter is pressed
                Control ctl = txb;
                while (ctl.Parent != null)
                {
                    var next = ctl.Parent.GetNextControl(ctl, true);
                    if (next != null)
                    {
                        next.Focus();
                        e.SuppressKeyPress = true;
                        e.Handled = true;
                        return;
                    }
                    ctl = ctl.Parent;
                }
            }
        }

        private static void Txb_LostFocus(object sender, EventArgs e)
        {
            var txb = (TextBox)sender;
            txb.BackColor = originalBackColor; // Revert back to the original back color (parent's back color) when focus is lost
        }
    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,738 questions
C#
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.
9,496 questions
0 comments No comments
{count} vote

Accepted answer
  1. Viorel 106.5K Reputation points
    2023-10-28T18:47:54.7+00:00

    The new textbox object does not have yet a parent, therefore txb.Parent.BackColor does not work.

    Try to remove the originalBackColor member. In Txb_LostFocus you can do this: txb.BackColor = txb.Parent.BackColor, or use another appropriate colour.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful