WinForm控件之【RichTextBox】


基本介紹

高級文本控件,提供高級文本輸入和編輯功能,如字符或段落格式的設置。

常設置屬性

DetectUrls:指示是否自動將URL的格式設置為鏈接;

EnableAutoDragDrop:是否啟用文本、圖片和其他數據的拖放操作;

BorderStyle:指示編輯控件是否應帶有邊框或邊框類型;

Lines:多行編輯中的文本行,作為字符串值的數組;

MaxLength:指定可以在編輯控件中輸入的最大字符數;

Multiline:控制編輯控件的文本是否能夠跨越多行;

ScrollBars:定義控件滾動條的行為;

WordWrap:指示多行編輯控件是否自動換行;

Enabled:指示是否啟用該控件,true為啟用狀態用戶可編輯,false為禁用狀態用戶不可編輯;

Name:指示代碼中用來標識該對象的名稱;

Text:獲取或設置多格式文本框中的文本;

Rtf:獲取或設置控件文本,包括所有RTF格式代碼;

 

事例舉例

 

 

相關代碼

     private string rtf;
        /// <summary>
        /// 多信息文本格式內容
        /// </summary>
        public string Rtf
        {
            get
            {
                return this.richTextBox1.Rtf;
            }
            set
            {
                this.richTextBox1.Rtf = value;
            }
        }
        private string textValue;
        /// <summary>
        /// 文本格式內容
        /// </summary>
        public string TextValue
        {
            get
            {
                return this.richTextBox1.Text;
            }
            set
            {
                this.richTextBox1.Text = value;
            }
        }

        private OpenFileDialog openImageDialog = new OpenFileDialog();
        private ColorDialog colorDialog1 = new ColorDialog();

        //工具欄按鈕項單擊事件觸發字符和段落的格式設置
        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Tag != null)
            {
                switch (e.ClickedItem.Tag.ToString())
                {
                    case "Bold":
                        if (this.richTextBox1.SelectionFont != null)
                        {
                            this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, this.richTextBox1.SelectionFont.Style ^ FontStyle.Bold);
                        }
                        else
                        {
                            this.richTextBox1.SelectionFont = new Font(this.richTextBox1.Font, this.richTextBox1.Font.Style ^ FontStyle.Bold);
                        }

                        break;
                    case "Itilic":

                        if (this.richTextBox1.SelectionFont != null)
                        {
                            this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, this.richTextBox1.SelectionFont.Style ^ FontStyle.Italic);
                        }
                        else
                        {
                            this.richTextBox1.SelectionFont = new Font(this.richTextBox1.Font, this.richTextBox1.Font.Style ^ FontStyle.Italic);
                        }
                        break;
                    case "Underline":
                        if (this.richTextBox1.SelectionFont != null)
                        {
                            this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, this.richTextBox1.SelectionFont.Style ^ FontStyle.Underline);
                        }
                        else
                        {
                            this.richTextBox1.SelectionFont = new Font(this.richTextBox1.Font, this.richTextBox1.Font.Style ^ FontStyle.Underline);
                        }
                        break;
                    case "Cut":
                        this.richTextBox1.Cut();
                        break;
                    case "Copy":
                        this.richTextBox1.Copy();
                        break;
                    case "Paste":
                        this.richTextBox1.Paste();
                        break;
                    case "InsertImg":
                        if (this.openImageDialog.ShowDialog() == DialogResult.OK)
                        {
                            string fileName = this.openImageDialog.FileName;
                            try
                            {
                                Bitmap data = new Bitmap(fileName);
                                Clipboard.SetDataObject(data);
                                DataFormats.Format clipFormat = DataFormats.GetFormat(DataFormats.Bitmap);
                                if (this.richTextBox1.CanPaste(clipFormat))
                                {
                                    this.richTextBox1.Paste(clipFormat);
                                }
                            }
                            catch (Exception exception)
                            {
                                MessageBox.Show("圖片插入失敗" + exception.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            }
                        }

                        break;
                    case "Color":
                        if (this.colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            this.richTextBox1.SelectionColor = this.colorDialog1.Color;
                        }
                        break;
                    case "Undo":
                        this.richTextBox1.Undo();
                        break;
                    case "Redo":
                        this.richTextBox1.Redo();
                        break;
                    case "Left":
                        this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;

                        break;
                    case "Center":
                        this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;

                        break;
                    case "Right":
                        this.richTextBox1.SelectionAlignment = HorizontalAlignment.Right;

                        break;
                    default:

                        break;
                }
            }
        }

        //字符或段落的字體設置
        private void tscb_Typeface_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.tscb_Typeface.SelectedItem != null)
            {
                string familyName = this.tscb_Typeface.SelectedItem.ToString().Trim();
                if (this.richTextBox1.SelectionFont != null)
                {
                    this.richTextBox1.SelectionFont = new Font(familyName, this.richTextBox1.SelectionFont.Size, this.richTextBox1.SelectionFont.Style);
                }
                else
                {
                    this.richTextBox1.SelectionFont = new Font(familyName, this.richTextBox1.Font.Size, this.richTextBox1.Font.Style);
                }
            }

        }

        //字符或段落的字號設置
        private void tscb_Size_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.tscb_Size.SelectedItem != null)
            {
                int num = Convert.ToInt32(this.tscb_Size.SelectedItem.ToString().Trim());
                if (this.richTextBox1.SelectionFont != null)
                {
                    this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont.FontFamily, (float)num, this.richTextBox1.SelectionFont.Style);
                }
                else
                {
                    this.richTextBox1.SelectionFont = new Font(this.richTextBox1.Font.FontFamily, (float)num, this.richTextBox1.Font.Style);
                }
            }

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM