Extended RichTextBox Control

hi. I really needed a Richtext box with Word like functionality.. i mean Bold,Italic etc etc Function. So i decided to write a Custom Control for this Purpose.

Extended RichTextBox Contains Following Controls

1. Rich Text Box [ rtxtBox ]
2. A tool Strip with Basic button including word Formatting Functionality.. [ tsStrip ]
3. A font Dialog [ fntD ], Color Dialog [ clrD ], OpenFileDialog [ ofD ]
here is a pic.

1. First Add a RichText box and a tool strip as show in figure to your Custom Control.
2. Add basic buttons to tool strip according to screen shot (You can add your own image to buttons)
3. And here is the coding..

Add This Properties to your Custom Control


[Browsable(true), Category("Appearance")] 
        public override Font Font 
        { 
            get { return rtxtBox.Font  ; } 
            set { rtxtBox.Font  = value; 
                Invalidate(); } 
        } 
        [Browsable(true), Category("Appearance")] 
        public override Color  BackColor 
        { 
            get { return rtxtBox.BackColor ; } 
            set 
            { 
                rtxtBox.BackColor  = value; 
                Invalidate(); 
            } 
        } 
        [Browsable(true), Category("Appearance")] 
        public override Color ForeColor 
        { 
            get { return rtxtBox.ForeColor ; } 
            set 
            { 
                rtxtBox.ForeColor  = value; 
                Invalidate(); 
            } 
        } 
         
        [Browsable(true), Category("Appearance")] 
        public  string[]  Lines 
        { 
            get { return rtxtBox.Lines; } 
            set { rtxtBox.Lines = value; } 
        } 
        [Browsable(true), Category("Appearance")] 
        public RichTextBoxScrollBars ScrollBars 
        { 
            get { return rtxtBox.ScrollBars; } 
            set { rtxtBox.ScrollBars = value; } 
        } 
        [Browsable(true), Category("Appearance")] 
        public override string Text 
        { 
            get { return rtxtBox.Text;  } 
            set { rtxtBox.Text = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool AcceptsTab 
        { 
            get { return rtxtBox.AcceptsTab;  } 
            set { rtxtBox.AcceptsTab = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool AutoWordSelection 
        { 
            get { return rtxtBox.AutoWordSelection; } 
            set { rtxtBox.AutoWordSelection = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public int BulletIndent 
        { 
            get { return rtxtBox.BulletIndent; } 
            set { rtxtBox.BulletIndent  = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool DetectUrls 
        { 
            get { return rtxtBox.DetectUrls; } 
            set { rtxtBox.DetectUrls  = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool EnableAutoDragDrop 
        { 
            get { return rtxtBox.EnableAutoDragDrop ; } 
            set { rtxtBox.EnableAutoDragDrop = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool HideSelection 
        { 
            get { return rtxtBox.HideSelection ; } 
            set { rtxtBox.HideSelection = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public int MaxLength 
        { 
            get { return rtxtBox.MaxLength ; } 
            set { rtxtBox.MaxLength = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool Multiline 
        { 
            get { return rtxtBox.Multiline ; } 
            set { rtxtBox.Multiline = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool ReadOnly 
        { 
            get { return rtxtBox.ReadOnly ; } 
            set { rtxtBox.ReadOnly = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public int RightMargin 
        { 
            get { return rtxtBox.RightMargin ; } 
            set { rtxtBox.RightMargin = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool ShortcutsEnabled 
        { 
            get { return rtxtBox.ShortcutsEnabled; } 
            set { rtxtBox.ShortcutsEnabled = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool ShowSelectionMargin 
        { 
            get { return rtxtBox.ShowSelectionMargin ; } 
            set { rtxtBox.ShowSelectionMargin = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public bool WordWrap  
        { 
            get { return rtxtBox.WordWrap; } 
            set { rtxtBox.WordWrap  = value; } 
        } 
        [Browsable(true), Category("Behavior")] 
        public float ZoomFactor 
        { 
            get { return rtxtBox.ZoomFactor ; } 
            set { rtxtBox.ZoomFactor = value; } 
        }  

Code For Font Button on tool strip to change Richtextbox font

private void tbrFont_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                fntD.Font = rtxtBox.Font; 
            } 
            else 
            { 
                fntD.Font = null; 
            } 
             
            fntD.ShowApply = true; 
            if (fntD.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
            { 
                rtxtBox.SelectionFont = fntD.Font; 
            } 
        } 


Here Is the Rest of The Code:

//Left Alignment 
        private void tbrLeft_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionAlignment = HorizontalAlignment.Left; 
        } 
        //Center Alignment 
        private void tbrCenter_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionAlignment = HorizontalAlignment.Center; 
        } 
        //Right Alignment 
        private void tbrRight_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionAlignment = HorizontalAlignment.Right; 
        } 
        //Bold 
        private void tbrBold_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Bold == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                else 
                { 
                    newFont = FontStyle.Bold; 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //Italic 
        private void tbrItalic_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Italic == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                else 
                { 
                    newFont = FontStyle.Italic; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //Underline 
        private void tbrUnderline_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Underline  == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic  == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                else 
                { 
                    newFont = FontStyle.Underline; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Strikeout == true) { newFont = newFont | FontStyle.Strikeout; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //StrikeThrough 
        private void tbrStrikeThrough_Click(object sender, EventArgs e) 
        { 
            if (rtxtBox.SelectionFont != null) 
            { 
                System.Drawing.Font currFont = rtxtBox.SelectionFont; 
                System.Drawing.FontStyle newFont; 
                if (rtxtBox.SelectionFont.Strikeout == true) 
                { 
                    newFont = FontStyle.Regular; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline  == true) { newFont = newFont | FontStyle.Underline; } 
                } 
                else 
                { 
                    newFont = FontStyle.Strikeout; 
                    if (rtxtBox.SelectionFont.Bold == true) { newFont = newFont | FontStyle.Bold; } 
                    if (rtxtBox.SelectionFont.Italic == true) { newFont = newFont | FontStyle.Italic; } 
                    if (rtxtBox.SelectionFont.Underline == true) { newFont = newFont | FontStyle.Underline; } 
                } 
                rtxtBox.SelectionFont = new Font(currFont.FontFamily, currFont.Size, newFont); 
            } 
        } 
        //Add Bullet 
        private void addBulletToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            rtxtBox.BulletIndent = 10; 
            rtxtBox.SelectionBullet = true; 
        } 
        //Remove Bullet 
        private void removeBulletToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            rtxtBox.SelectionBullet = false; 
        } 
        //Font Fore Color 
        private void tbrFontColor_Click(object sender, EventArgs e) 
        { 
            clrD.Color = rtxtBox.ForeColor; 
            if (clrD.ShowDialog() == DialogResult.OK) 
            { 
                rtxtBox.SelectionColor = clrD.Color; 
            } 
           
        } 
        //Font Back Color 
        private void tbrBackColor_Click(object sender, EventArgs e) 
        { 
            clrD.Color = rtxtBox.SelectionBackColor ; 
            if (clrD.ShowDialog() == DialogResult.OK) 
            { 
                rtxtBox.SelectionBackColor  = clrD.Color; 
            } 
        } 
        
        //KeyDown Event  [Shortcuts] 
        private void rtxtBox_KeyDown(object sender, KeyEventArgs e) 
        { 
            if (e.Control && e.KeyCode == Keys.B) 
            { 
                tbrBold_Click(null, null); 
            } 
            if (e.Control && e.KeyCode == Keys.I ) 
            { 
                tbrItalic_Click(null, null); 
            } 
            if (e.Control && e.KeyCode == Keys.U) 
            { 
                tbrUnderline_Click(null, null); 
            } 
            if (e.Control && e.KeyCode == Keys.S) 
            { 
                tbrStrikeThrough_Click(null, null); 
            } 
        } 

If you want the Complete Source Code then mail me on : sandeepparekh9@gmaill.com

转载于:https://www.cnblogs.com/samblog/archive/2012/04/01/2428798.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值