Надежное программирование
В этом примере перед извлечением значения SelectedText устанавливается свойство Text. В большинстве случаев извлекается текст, введенный пользователем. Поэтому если текст слишком короток, в код рекомендуется добавить обработчик ошибок.
How to: Format Characters in a RichTextBox Control
This example writes a sentence, which contains three words in three different font styles (bold, italic, and underlined), to an existing RichTextBox control.
Example
richTextBox1.Rtf = @"{\rtf1\ansi This text is in \b bold\b0, " + @"this is in \i italics\i0, " + @"and this is \ul underlined\ul0.}"; |
Compiling the Code
This example requires: A RichTextBox control named richTextBox1.
Robust Programming
The rich text format is very flexible, but any errors in the format lead to errors in the displayed text.
Форматирование знаков в элементе управления "RichTextBox"
В этом примере выполняется запись предложения, содержащего три слова, написанных разными шрифтами (полужирным, курсивом и с подчеркиванием), в существующий элемент управления RichTextBox.
Пример
richTextBox1.Rtf = @"{\rtf1\ansi This text is in \b bold\b0, " + @"this is in \i italics\i0, " + @"and this is \ul underlined\ul0.}"; |
Компиляция кода[5]
Для этого примера необходимы следующие компоненты. Элемент управления RichTextBox с именем richTextBox.
Надежное программирование
Расширенный текстовый формат очень гибок, но любая ошибка в формате приведет к ошибкам в отображаемом тексте.
How to: Load Text into a RichTextBox Control
This example loads a text file that a user selects in the OpenFileDialog. The code then populates a RichTextBox control with the file's contents.
Example
// Create an OpenFileDialog object.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the filter to look for text files.
openFile1.Filter = "Text Files|*.txt";
// If the user selected a file, load its contents into the RichTextBox.
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
Compiling the Code
This example requires:
· A RichTextBox control named richTextBox1. Insert the code segment into the Form1_Load method. When you run the program, you will be prompted to select a text file.
Загрузка текста в элемент управления "RichTextBox"
В этом примере выполняется загрузка текстового файла, выбранного пользователем в OpenFileDialog. Затем код заполняет элемент управления RichTextBox содержимым файла.
Пример
// Create an OpenFileDialog object.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the filter to look for text files.
openFile1.Filter = "Text Files|*.txt";
// If the user selected a file, load its contents into the RichTextBox.
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
Компиляция кода
Для этого примера необходимы следующие компоненты.
· Элемент управления RichTextBox с именем richTextBox1. Вставьте сегмент кода в метод Form1_Load.[6] При выполнении программы будет выведен запрос на выбор текстового файла.
Dialog Boxes
Дата добавления: 2022-05-27; просмотров: 120;