Basic usage of RichTextBox

Function: Rich text editor, supports a variety of text display

Common properties:

 Allow multiple lines to be displayed

  Automatic line wrapping

 show scroll bar

 

ScrollBars property value:

1. Both: Only when the text exceeds the width or length of the RichTextBox, the horizontal scroll bar or the vertical scroll bar, or both scroll bars are displayed.

2. None: Never display any type of scroll bar.

3. Horizontal: The horizontal scroll bar is displayed only when the text exceeds the width of the RichTextBox. The WordWrap property must be set to false for this to occur. (explanation will be given below)

4. Vertical: Only when the file text exceeds the height of the RichTextBox, the vertical scroll bar will be displayed.

5. ForcedHorizontal: When the WordWrap property is set to false, the horizontal scroll bar is displayed. When the text does not exceed the width of the RichTextBox, the scroll bar appears light gray.

6. ForcedVertical: Always display vertical scroll bars. When the text does not exceed the length of the RichTextBox, the scroll bar appears in light gray.

7. ForcedBoth: Always display vertical scroll bars. When the WordWrap property is set to false, a horizontal scroll bar is displayed. When the text does not exceed the width or length of the RichTextBox, both scrollbars appear gray.

 Note: The WordWrap property of RichTextBox: is used to indicate whether the multi-line text box control wraps to the beginning of the next line when necessary. When the property is true, horizontal scroll bars will not be displayed regardless of the value of the ScrollBars property.

Common events:

 Fires when text is selected

 Fires when text changes

 

 

Common methods of RichTextBox control

1) Clear() method - clears all user input in the RichText control. 
2) Copy(), Cut(), Paste() methods - implement the clipboard function of the RichText control;
3) SelectAll() method - select all text in the control. 4) Find() method - implements the search function.
5) SaveFile() method, LoadFile() method - save text and open files.
6) Undo() method, Redo() method - undo the last editing operation and redo the last undone editing operation.  
Description: Often used in conjunction with the CanUndo attribute and CanRedo attribute.
7) LoadFile() - Load text files (*.txt) or RTF files (*.rtf). 
8) SaveFile() - Save text files (*.txt) or RTF files (*.rtf).

Code behind:

 //文本改变时触发
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            //实时显示文本编辑
            label3.Text = richTextBox1.Text;
            //把当前粘贴板上面的内容放置到文本中
            richTextBox1.Paste();
            //查找指定字符
            int index = richTextBox1.Find("h");
            label3.Text = "h的坐标为:" + index.ToString();
            //跳至字符匹配的位置
            richTextBox1.Select(index, 1);

        }

        //选中文本时触发
        private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            label3.Text = richTextBox1.SelectedText;


        }

おすすめ

転載: blog.csdn.net/XiaoWang_csdn/article/details/132130312