在wpf中,如果要取得richtextbox中所選擇的反白文字訊息可以使用
richTextBox1.Selection.Text.ToString();來取得
以下範例為在richtextbox中按右鍵會跳出一個菜單選項,點後可以取得所反白的文字訊息
請在xaml中加入一個richtextbox1的物件
並加入此參考
System.Windows.Forms
=====================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
namespace DemoLrightMenuinlist
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
//TODO:初始化 notifyIcon菜單
System.Windows.Controls.ContextMenu notifyIconMenu = new System.Windows.Controls.ContextMenu();
//TODO:系統列選單
System.Windows.Controls.MenuItem notifyIconMenuItemHome = new System.Windows.Controls.MenuItem();
notifyIconMenuItemHome.Header = "Addcount(&H)";
notifyIconMenuItemHome.Click += new RoutedEventHandler(notifyIconMenuItemHome_Click);
notifyIconMenu.Items.Add(notifyIconMenuItemHome);
richTextBox1.ContextMenu = notifyIconMenu;
}
void notifyIconMenuItemHome_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine(richTextBox1.Selection.Text.ToString());
}
}
}