本文整理汇总了C#中System.Windows.Forms.RichTextBox.SelectionLength属性的典型用法代码示例。如果您正苦于以下问题:C# RichTextBox.SelectionLength属性的具体用法?C# RichTextBox.SelectionLength怎么用?C# RichTextBox.SelectionLength使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Windows.Forms.RichTextBox
的用法示例。
在下文中一共展示了RichTextBox.SelectionLength属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ModifySelectedText
private void ModifySelectedText()
{
// Determine if text is selected in the control.
if (richTextBox1.SelectionLength > 0)
{
// Set the color of the selected text in the control.
richTextBox1.SelectionColor = Color.Red;
// Set the font of the selected text to bold and underlined.
richTextBox1.SelectionFont = new Font("Arial",10,FontStyle.Bold | FontStyle.Underline);
// Protect the selected text from modification.
richTextBox1.SelectionProtected = true;
}
}
示例2: RichTextBoxes
//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
public class RichTextBoxes : Form
{
RichTextBox rtxt;
public RichTextBoxes()
{
Size = new Size(400, 500);
rtxt = new RichTextBox();
rtxt.Parent = this;
rtxt.Text = "Enter text here.";
rtxt.Multiline = true;
rtxt.BorderStyle = BorderStyle.Fixed3D;
rtxt.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
rtxt.Dock = DockStyle.Fill;
rtxt.DetectUrls = true;
rtxt.AutoWordSelection = true;
rtxt.BulletIndent = 10;
rtxt.ShowSelectionMargin = true;
MenuItem mnuImport = new MenuItem("&Import",new EventHandler(mnuImport_Click));
MenuItem mnuFile = new MenuItem("&File",new MenuItem[] {mnuImport});
MenuItem mnuDash1 = new MenuItem("-");
MenuItem mnuDash2 = new MenuItem("-");
MenuItem mnuUndo = new MenuItem("&Undo",new EventHandler(mnuUndo_Click),Shortcut.CtrlZ);
MenuItem mnuCut = new MenuItem("Cu&t", new EventHandler(mnuCut_Click), Shortcut.CtrlX);
MenuItem mnuCopy = new MenuItem("&Copy",new EventHandler(mnuCopy_Click),Shortcut.CtrlC);
MenuItem mnuCopyRtf = new MenuItem("Copy &Rtf", new EventHandler(mnuCopyRtf_Click));
MenuItem mnuPaste = new MenuItem("&Paste",new EventHandler(mnuPaste_Click),Shortcut.CtrlV);
MenuItem mnuDelete = new MenuItem("&Delete",new EventHandler(mnuDelete_Click));
MenuItem mnuSelectAll = new MenuItem("Select &All", new EventHandler(mnuSelectAll_Click),Shortcut.CtrlA);
MenuItem mnuSelect5 = new MenuItem("Select First &5",new EventHandler(mnuSelect5_Click),Shortcut.Ctrl5);
MenuItem mnuClear = new MenuItem("Clea&r",new EventHandler(mnuClear_Click));
MenuItem mnuEdit = new MenuItem("&Edit",new MenuItem[] {mnuUndo, mnuDash1,
mnuCut, mnuCopy, mnuCopyRtf, mnuPaste,
mnuDelete, mnuDash2, mnuSelectAll,
mnuSelect5, mnuClear});
MenuItem mnuScrollToCaret = new MenuItem("&Scroll to Caret",new EventHandler(mnuScrollToCaret_Click));
MenuItem mnuView = new MenuItem("&View",new MenuItem[] {mnuScrollToCaret});
MenuItem mnuAlignLeft = new MenuItem("Align&Left",new EventHandler(mnuAlignLeft_Click));
MenuItem mnuAlignRight = new MenuItem("Align&Right",new EventHandler(mnuAlignRight_Click));
MenuItem mnuAlignCenter = new MenuItem("Align&Center",new EventHandler(mnuAlignCenter_Click));
MenuItem mnuBullet = new MenuItem("&Bullet",new EventHandler(mnuBullet_Click));
MenuItem mnuAlign = new MenuItem("&Align",new MenuItem[] {mnuAlignLeft, mnuAlignRight, mnuAlignCenter});
MenuItem mnuRed = new MenuItem("&Red",new EventHandler(mnuRed_Click));
MenuItem mnuBold = new MenuItem("Bo&ld",new EventHandler(mnuBold_Click));
MenuItem mnuHang = new MenuItem("&Hanging Indent",new EventHandler(mnuHang_Click));
MenuItem mnuIndent = new MenuItem("&Indent",new EventHandler(mnuIndent_Click));
MenuItem mnuRightIndent = new MenuItem("&Right Indent", new EventHandler(mnuRightIndent_Click));
MenuItem mnuFormat = new MenuItem("For&mat",new MenuItem[] {mnuBullet, mnuAlign,
mnuRed, mnuBold, mnuHang, mnuIndent,
mnuRightIndent});
Menu = new MainMenu(new MenuItem[] {mnuFile, mnuEdit,
mnuView, mnuFormat});
}
static void Main()
{
Application.Run(new RichTextBoxes());
}
private void mnuImport_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"c:\";
ofd.Filter = "RTF files (*.rtf)|*.rtf|" +
"All files (*.*)|*.*";
ofd.FilterIndex = 1; // 1 based index
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
StreamReader reader = new StreamReader(ofd.FileName);
rtxt.Rtf = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
private void mnuUndo_Click(object sender, EventArgs e)
{
if (rtxt.CanUndo == true)
{
rtxt.Undo();
rtxt.ClearUndo();
}
}
private void mnuCut_Click(object sender, EventArgs e)
{
if (rtxt.SelectedText != "")
rtxt.Cut();
}
private void mnuCopy_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.Copy();
}
private void mnuCopyRtf_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
{
Clipboard.SetDataObject(rtxt.SelectedRtf);
}
}
private void mnuPaste_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
if (rtxt.CanUndo == true)
{
if (rtxt.SelectionLength > 0)
{
if (MessageBox.Show(
"Do you want to overwrite the currently selected text?",
"Cut & Paste", MessageBoxButtons.YesNo) == DialogResult.No)
rtxt.SelectionStart = rtxt.SelectionStart +
rtxt.SelectionLength;
}
rtxt.Paste();
}
}
}
private void mnuDelete_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.SelectedText = "";
}
private void mnuClear_Click(object sender, EventArgs e)
{
rtxt.Clear();
}
private void mnuSelect5_Click(object sender, EventArgs e)
{
if (rtxt.Text.Length >= 5)
{
rtxt.Select(0,5);
}
else
{
rtxt.Select(0,rtxt.Text.Length);
}
}
private void mnuSelectAll_Click(object sender, EventArgs e)
{
rtxt.SelectAll();
}
private void mnuScrollToCaret_Click(object sender, EventArgs e)
{
rtxt.ScrollToCaret();
}
private void mnuBullet_Click(object sender, EventArgs e)
{
rtxt.SelectionBullet = !rtxt.SelectionBullet;
}
private void mnuAlignLeft_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Left;
}
private void mnuAlignRight_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Right;
}
private void mnuAlignCenter_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Center;
}
private void mnuRed_Click(object sender, EventArgs e)
{
if (rtxt.SelectionColor == Color.Red)
rtxt.SelectionColor = Color.Black;
else
rtxt.SelectionColor = Color.Red;
}
private void mnuBold_Click(object sender, EventArgs e)
{
if (rtxt.SelectionFont.Bold )
rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Regular);
else
rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Bold);
}
private void mnuHang_Click(object sender, EventArgs e)
{
if (rtxt.SelectionHangingIndent == 10 )
rtxt.SelectionHangingIndent = 0;
else
rtxt.SelectionHangingIndent = 10;
}
private void mnuIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionIndent == 10 )
rtxt.SelectionIndent = 0;
else
rtxt.SelectionIndent = 10;
}
private void mnuRightIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionRightIndent == 50 )
rtxt.SelectionRightIndent = 0;
else
rtxt.SelectionRightIndent = 50;
}
}