Richtextbox in windows application
But the RichTextBox provides needed features to programs. This code example shows how colored text blocks are generated in the RichTextBox.
Next Two data arrays are looped over. Info The SelectionBackColor property changes the currently selected text in the RichTextBox to have the background color you assign to it.
Tip If there is no current selection, then the caret is used to start the selection. When entering text in the RichTextBox, sometimes a link to a web page will be entered. If you set the DetectUrls property to true, the RichTextBox will add hyperlink styles to any possible link. However If you do not add the LinkClicked event handler, you will not be able to act upon a click.
Example that demonstrates RichTextBox, C :. This is a commonly used and useful one. Whenever the text changes for any reason in the RichTextBox, the code in the TextChanged event handler executes. Next In this example, we assign the window's title text to the text in the RichTextBox.
Select, SelectedText. The RichTextBox also offers the ability to select text based on a starting index and a count of characters, with the Select method.
You could use the IndexOf method to search for the location of the text string you want to select. It also demonstrates using the FileDialog. FileName , FileDialog. DefaultExt , SaveFileDialog. CreatePrompt , and SaveFileDialog.
OverwritePrompt members. This version of the SaveFile method enables you to save the entire contents of the control to the data stream that is already opened.
The data stream can then save the information to a file. This version of the SaveFile method also enables you to specify a data format of the information that will be sent to the Stream object. The example uses the SaveFileDialog class to display a dialog to request the path and file name from the user.
The code then saves the contents of the control to that file. The example uses this version of the SaveFile method to specify that the file be saved as an ASCII text file instead of the standard rich text format.
If the file name that is passed to the path parameter already exists at the specified directory, the file will be overwritten without notice. This version of the SaveFile method enables you to specify a file type to save the contents of the control to.
You can use this feature to ensure that the file is saved in the proper format based on the contents of the control. The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 2. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property.
If you click on these properties in Properties window, the Color Dialog pops up. Alternatively, you can set background and foreground colors at run-time. The following code snippet sets BackColor and ForeColor properties. The default value of border style is Fixed3D. It is used to access the control in the code. The following code snippet sets and gets the name and text of a RichTextBox control.
TextLength; Append Text One way to append text to a RichTextBox is simply set Text property to current text plus new text you would want to append something like this. If this property is not set, pressing the TAB key simply moves to the next control on the Form. If this property is set to true , horizontal scroll bars are not displayed regardless of the ScrollBars property setting. The following code snippet makes both vertical and horizontal scrollbars active on a RichTextBox control and they will be visible when the scrolling is needed on a RichTextBox control.
If you click on the Font property in the Properties window, you will see Font name, size and other font options. The following code snippet sets Font property at run-time. The following code snippet sets the maximum length of a RichTextBox to 50 characters. The following code snippet sets the ReadOnly property to true. By default, shortcuts are enabled. The following code snippet disables shortcuts in a RichTextBox.
Note however that the Text property has no formatting; it has only text. See the Rtf property for the text including the formatting. The following code snippet reads contents of a RichTextBox in a string. The Lines property returns an array of strings where each element of the returned array is a line.
The following code snippet reads a RichTextBox contents line by line. The SelectionStart property represents the starting index of the selected text and SelectionLength property represents the number of characters to be selected after the starting character. The following code snippet sets the selection on a RichTextBox.
The following code snippet uses Clear method to clear the contents of a RichTextBox. The following code snippet shows how to use SelectAll and DeselectAll methods. The following code snippet shows how to use Cut, Copy, Paste, and Undo methods. GetDataPresent DataFormats.
0コメント