A Transparent Textbox

5:13 AM

I have tried so hard to find a transparent textbox on the web but all in vain.
so i have tried to create a one..

This a User Control not a Custom Control so it has less functionality that the traditional texbox but i have tried to cover most of the important properties.

here are some pics..





This Control is inherited from UserControl


Properties:


#region Properties
        [Browsable(false), Category("Appearance")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override Image BackgroundImage
        {
            get
            {
                return base.BackgroundImage;
            }
            set
            {
                base.BackgroundImage = value;
            }
        }

        [Browsable(false), Category("Appearance")]
        public override ImageLayout BackgroundImageLayout
        {
            get
            {
                return base.BackgroundImageLayout;
            }
            set
            {
                base.BackgroundImageLayout = value;
            }
        }


        [Browsable(true), Category("Appearance")]
        public  Font  _Font
        {
            get { return this.Font; }
            set
            {
                txt.Font = value;
                lbl.Font = value;
                Invalidate();
            }
        }

        [Browsable(true), Category("Appearance")]
        public  Color _ForeColor
        {
            get { return this.ForeColor; }
            set
            {
                txt.ForeColor = value;
                lbl.ForeColor = value;
                this.ForeColor = value;
                Invalidate();
            }
        }

        [Browsable(true), Category("Appearance")]
        public  Color _BackColor
        {
            get { return this.BackColor; }
            set
            {
                if (value != Color.Transparent) { txt.BackColor = value; this.BackColor = value; }
                Invalidate();
            }
        }

        [Browsable(true), Category("Appearance")]
        public ScrollBars _ScrollBars
        {
            get { return txt.ScrollBars; }
            set { txt.ScrollBars = value; Invalidate(); }
        }

        [Browsable(true), Category("Appearance")]
        public string Text
        {
            get { return txt.Text; }
            set { txt.Text = value; lbl.Text = value; Invalidate(); }
        }

        [Browsable(true), Category("Appearance")]
        public string[] _Lines
        {
            get { return txt.Lines; }
            set { txt.Lines = value; Invalidate(); }

        }

        [Browsable(true), Category("Appearance")]
        public HorizontalAlignment _TextAlignMent
        {
            get { return txt.TextAlign; }
            set 
            {   txt.TextAlign = value;
                if (value == HorizontalAlignment.Center) { lbl.TextAlign = ContentAlignment.TopCenter; }
                if (value == HorizontalAlignment.Left ) { lbl.TextAlign = ContentAlignment.TopLeft ; }
                if (value == HorizontalAlignment.Right) { lbl.TextAlign = ContentAlignment.TopRight ; }
                Invalidate();
            }
        }

        [Browsable(true), Category("Behavior")]
        public bool AcceptsTab
        {
            get { return txt.AcceptsTab; }
            set { txt.AcceptsTab = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public bool AcceptsReturn
        {
            get { return txt.AcceptsReturn ; }
            set { txt.AcceptsReturn = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public override bool AllowDrop
        {
            get { return txt.AllowDrop; }
            set { txt.AllowDrop = value; lbl.AllowDrop = value; Invalidate(); }
            
        }

        [Browsable(true), Category("Behavior")]
        public CharacterCasing CharacterCasing
        {
            get { return txt.CharacterCasing; }
            set { txt.CharacterCasing = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public bool HideSelection
        {
            get { return txt.HideSelection; }
            set { txt.HideSelection = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public int MaxLength
        {
            get { return txt.MaxLength; }
            set { txt.MaxLength = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public char PasswordChar
        {
            get { return txt.PasswordChar; }
            set { 
                    txt.PasswordChar = value;
                    lbl.Text ="";
                    for (int i = 0; i < value.ToString().Length; i++)
                    {
                        lbl.Text += PasswordChar;
                    }
                    Invalidate();
                }
        }

        [Browsable(true), Category("Behavior")]
        public bool Multiline
        {
            get { return txt.Multiline; }
            set { txt.Multiline = value; }
        }

        [Browsable(true), Category("Behavior")]
        public bool ReadOnly
        {
            get { return txt.ReadOnly; }
            set { txt.ReadOnly = value; }
        }

        [Browsable(true), Category("Behavior")]
        public bool ShortcutsEnabled
        {
            get { return txt.ShortcutsEnabled; }
            set { txt.ShortcutsEnabled = value; Invalidate(); }
        }

        [Browsable(true), Category("Behavior")]
        public bool UseSystemPasswordChar
        {
            get { return txt.UseSystemPasswordChar; }
            set { txt.UseSystemPasswordChar = value; Invalidate(); }
        }
        
        [Browsable(true), Category("Behavior")]
        public bool WordWrap
        {
            get { return txt.WordWrap; }
            set { txt.WordWrap = value; }
        }

        [Browsable(true), Category("Misc")]
        public AutoCompleteStringCollection AutoCompleteCustomSource
        {
            get { return txt.AutoCompleteCustomSource; }
            set { txt.AutoCompleteCustomSource = value; }
        }

        [Browsable(true), Category("Misc")]
        public AutoCompleteMode AutoCompleteMode
        {
            get { return txt.AutoCompleteMode;  }
            set { txt.AutoCompleteMode= value; }
        }

        [Browsable(true), Category("Misc")]
        public AutoCompleteSource  AutoCompleteSource
        {
            get { return txt.AutoCompleteSource;  }
            set { txt.AutoCompleteSource = value; }
        }
        #endregion

        #region Events
        [Browsable(true), Category("Misc")]
        public event EventHandler TextChanged
        {
            add { txt.TextChanged += value; }
            remove { txt.TextChanged -= value; }
        }

        #endregion
Other Importand Event and Methods
#region Events
        [Browsable(true), Category("Misc")]
        public event EventHandler TextChanged
        {
            add { txt.TextChanged += value; }
            remove { txt.TextChanged -= value; }
        }

        #endregion

        private void lbl_MouseEnter(object sender, EventArgs e)
        {
            if (txt.PasswordChar != '\0')// && txt.PasswordChar.ToString() != "")
            {
                lbl.Text = "";
                for (int i = 0; i < txt.Text.ToString().Length; i++)
                {
                    lbl.Text += PasswordChar;
                }
            }
            else
            {
                lbl.Text = txt.Text;
            }
            txt.BringToFront();
            lbl.SendToBack();
            txt.Focus();
        }
        public void Clear()
        {
            txt.Clear();
            lbl.Text = "";
        }

        private void txt_MouseLeave(object sender, EventArgs e)
        {
            
            txt.SendToBack();
            lbl.BringToFront();
            if (txt.PasswordChar != '\0')
            {
                lbl.Text = "";
                for (int i = 0; i < txt.Text.ToString().Length; i++)
                {
                    lbl.Text += PasswordChar;
                }
            }
            else
            {
                lbl.Text = txt.Text;
            }
        }

        private void TransTextBoxR_Load(object sender, EventArgs e)
        {
            if (txt.PasswordChar != '\0')
            {
                lbl.Text = "";
                for (int i = 0; i < txt.Text.ToString().Length; i++)
                {
                    lbl.Text += PasswordChar;
                }
            }
            else
            {
                lbl.Text = txt.Text;
            }
        }
For Full Source Code email me at : sandeepparekh9@gmail.com

1 comments:

Post a Comment