zoukankan      html  css  js  c++  java
  • 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);
                    }
                }
    
            }
  • 相关阅读:
    第二次结对编程作业
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    第09组 Alpha冲刺(4/6)
    第09组 Alpha冲刺(3/6)
    第09组 Alpha冲刺(2/6)
    第09组 Alpha冲刺(1/6)
    第09组 团队Git现场编程实战
    第09组 团队项目-需求分析报告
  • 原文地址:https://www.cnblogs.com/ljhandsomeblog/p/11214496.html
Copyright © 2011-2022 走看看