Kelly Elton
Kelly Elton

Reputation: 4427

c# wpf richtextbox run mouseup

Hey, I have a richtextbox. To add content I do this

Paragraph p = new Paragraph();
Run r;
r = new Run("[" + user + "]: ");
b = System.Windows.Media.Brushes.Black;
if (user.Equals(Program.LClient.strUserName))
    b = Brushes.Blue;
r.Foreground = b;
r.ToolTip = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString() + "\nClick to whisper";
r.Cursor = Cursors.Hand;
r.Background = Brushes.White;
r.MouseEnter += delegate(object sender, MouseEventArgs e)
{
    r.Background = new RadialGradientBrush(Colors.DarkGray, Colors.WhiteSmoke);
    
};
r.MouseLeave += delegate(object sender, MouseEventArgs e)
{
    r.Background = Brushes.White;
};
r.PreviewMouseUp += delegate(object sender, MouseButtonEventArgs e)
{
    tbMess.Text = "/w " + user + " ";
    tbMess.Focus();
};
p.Inlines.Add(new Bold(r));
rtbChat.Document.Blocks.Add(p);

Everything works except I can register ANY mouseup/down events. Mouse enter and leave work fine, just for some reason can't get any buttonevents. Also, i don't have any mouse events registered to the RichTextBox.

Also, read this before giving alternative ways to do this.

I'm not interested in looking for a new way to do this. I know I could keep an array of the locations of all the different elements that I want to keep track of, but I'm not interested in doing that. I found no documentation saying that the mouseup/down events arn't available for a run, so there has to be a way.

Thanks in advance for the help.

Upvotes: 2

Views: 1117

Answers (1)

Kelly Elton
Kelly Elton

Reputation: 4427

Found the awnser. Aparantly in textbox's and rich text boxes in wpf, the click doesn't register or something. You can use this to register clicks

 r.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(delegate(object sender, MouseButtonEventArgs e)
{
//code here
}));

Upvotes: 3

Related Questions