none
My custom textbox causes the IDE to crash RRS feed

  • Question

  • Good day everyone

    I'm trying to make a custom textbox with some custom properties.

    Here's my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;
    
    
    namespace Installment_System
    {
        class Textbox_Customized : TextBox
        {
    
            public Textbox_Customized()
            {
                NumberOnly = false;
                TextOnly = false;
                HighlightSelected = false;
                HighlightColor = Color.Aquamarine;
            }
            #region NumberOnly
            [Browsable(true)]
            [Category("Extended Properties")]
            [Description("Sets value to accept numbers only")]
            [DisplayName("Number Only")]
            public bool NumberOnly
            {
                get
                {
                    return this.NumberOnly;
                }
                set
                {
                    this.NumberOnly = value;
                }
            }
            #endregion
    
            #region TextOnly
            [Browsable(true)]
            [Category("Extended Properties")]
            [Description("Sets value to accept text only")]
            [DisplayName("Text Only")]
            public bool TextOnly
            {
                get
                {
                    return this.TextOnly;
                }
                set
                {
                    this.TextOnly = value;
                }
            }
            #endregion
    
            #region HighlightSelected
            [Browsable(true)]
            [Category("Extended Properties")]
            [Description("Sets value to highlight textbox when selected")]
            [DisplayName("Highlight Selected")]
            public bool HighlightSelected
            {
                get
                {
                    return this.HighlightSelected;
                }
                set
                {
                    this.HighlightSelected = value;
                }
            }
            #endregion
    
            #region HighlightColor
            [Browsable(true)]
            [Category("Extended Properties")]
            [Description("Sets what color when highlighted")]
            [DisplayName("Highlight Color")]
            public Color HighlightColor
            {
                get
                {
                    return this.HighlightColor;
                }
                set
                {
                    this.HighlightColor = value;
                }
            }
            #endregion
    
    
            protected override void OnKeyPress(KeyPressEventArgs e)
            {
                base.OnKeyPress(e);
                char[] numbers = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
                char c = e.KeyChar;
                if (NumberOnly)
                {
                    if (numbers.Contains(c) == false)
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
                else if (TextOnly)
                {
                    if (numbers.Contains(c) == true)
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
            }
    
            protected override void OnEnter(EventArgs e)
            {
                base.OnEnter(e);
                this.BackColor = HighlightColor;
            }
    
            protected override void OnLeave(EventArgs e)
            {
                base.OnLeave(e);
                this.BackColor = SystemColors.Window;
            }
    
        }
    }

    • Moved by CoolDadTx Thursday, July 6, 2017 1:50 PM Winforms related
    Thursday, July 6, 2017 7:54 AM

All replies

  • My guess, just from looking, is that your properties are referring to themselves and going into an endless loop when you try to get or set them!

    Either use auto-properties:

     public bool HighlightSelected {get;set;}

    Or a backing variable:

    private bool _highlightSelected;
    
    public bool HighlightSelected
    {
          get
          {
              return _highlightSelected;
          }
          set
          {
              _highlightSelected = value;
          }
    }
    Here is some more info on properties.

    Thursday, July 6, 2017 8:36 AM
  • My guess, just from looking, is that your properties are referring to themselves and going into an endless loop when you try to get or set them!

    Either use auto-properties:

     public bool HighlightSelected {get;set;}

    Or a backing variable:

    private bool _highlightSelected;
    
    public bool HighlightSelected
    {
          get
          {
              return _highlightSelected;
          }
          set
          {
              _highlightSelected = value;
          }
    }

    Thanks!

    Backing the variable solved it.

    Thursday, July 6, 2017 9:29 AM