Fire an event after Richtextbox Textchanged event - c#

I am writing a lexer and i also want my text editor to change the color of the keywords. While typing! like VS and ... ! but the problem is TextChanged event of a RichTextBox fires BEFOR adding the text to it.
Is there any pre defined event that fire write AFTER adding the text to the richTextBox ? if not how can i create one that fire write after entering the text!
PS : i am writing a WPF project!
private void rtxMain_TextChanged (object sender, TextChangedEventsArgs e) {
visual.getLastWord();
}

TextChnaged event fires after changing the text of RichTextBox. Here is a working example of it:
private void richtextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string text = new TextRange(richtextBox1.Document.ContentStart, richtextBox1.Document.ContentEnd).Text;
MessageBox.Show(text);
}
Whatever I type into the richtextBox1, it shows me the exact same value in the MessageBox. It means it fires the event after changing the Text.

you can use the keyUp event it will fire when a key on the keyboard is let go off.
RichTextBox r = new RichTextBox();
r.KeyUp += (s, e) =>
{
//your logic
};
You might need to add extra logic to avoid keypresses like shift which isnt hard to do and maybe not even neccesary.

Related

WPF MouseLeftButtonDown makes FlowDocument unable to select text

I add a function that adds text to FlowDocument when the mouse clicks.
There is no Click event in FlowDocument, so I listen to FlowDocument.MouseLeftButtonDown and MouseLeftButtonUp and check whether the mouse moves between down and up. When I click the mouse left button, the text successfully adds. However, I can't select any text in the FlowDocument.
I tried PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp. The behavior is the same. Isn't there a PostMouseLeftButtonDown?
My Code:
Point mouseDownPoint;
private void doc_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
mouseDownPoint = Mouse.GetPosition(doc);
e.Handled = true;
}
private void doc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var mouseUpPoint = Mouse.GetPosition(doc);
if ((mouseUpPoint - mouseDownPoint).Length < 8) /* add text */;
}
The control handles the event internally.
If you register the event handler programmatically like this, your doc_MouseLeftButtonUp event handler should get invoked (note that last handledEventsToo parameter):
doc.AddHandler(ContentElement.MouseLeftButtonUpEvent,
(MouseButtonEventHandler)doc_MouseLeftButtonUp, true);
Note that you may also have to take care of the MouseLeftButtonUp that is raised by the control itself.
I found the solution. Listen to FlowDocument.MouseLeftButtonDown and do not use e.Handled=true and listen to FlowDocumentScrollViewer.PreviewMouseLeftButtonUp will get text selection and add text behavior at the same time.

user control and raising events from controls placed in user control

So I would like to know what is wrong with the following code, especially from a theoretical point of view.
I have a user control in which I've added a text box.
When I click in the text box I would like the Mouse clicked event raised in the user control.
To my mind, the solution should be:
Create an event handler for the mouse click event in the text box.
in this event handler, raise the mouse click event for the user control.
so this is what i have:
private void txtLog_MouseClick(object sender, MouseEventArgs e)
{
this.OnMouseClick(e);
}
i have tried it and it doesn't work, why is this?
P.S. I would really like to know why this is wrong! A correct solution is great, but I'm really trying to understand where I'm going wrong here. Thank :-)
Well, you could just click on your textbox in design mode and in the property window in events tab add the click event. or if you want to do it in runtime you can do it like this:
textbox.Click += Txt_Click;
private static void Txt_Click(object sender, EventArgs e)
{
// do your thing
}
or even shorter:
textbox.Click += (s,e) =>
{
//do your thing
};
you should do these three steps
declare an MouseClick delegation method for textbox
assign method to textbox
add this delegation to the this (form) OnMouseClick event [on user control constructor]
Step1:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
}
Step2:
this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);
Step3:
public myUserControl()
{
InitializeComponent();
this.MouseClick += new MouseEventHandler(textBox1_MouseClick);
}

Get changed text from rich textbox

So, I am looking for the best way to get the text that has changed either using the keyPressed event, put preferably the TextChanged event. What I'm looking for is what has been changed. I have a program that should send events to another window and populate the rich textbox there with a color. I tried the following using the keyPressed event:
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char keyChar = (char) e.KeyChar;
if (Char.IsLetterOrDigit(keyChar) || Char.IsSeparator(keyChar)
|| Char.IsWhiteSpace(keyChar))
{
string changedText = keyChar.ToString();
VinduEventArgs ve = new VinduEventArgs(colorDialog1.Color, changedText);
VinduEndret(this, ve);
}
}
But it doesn't really work as it's buggy, doesn't show all signs, and seems kindof like a bad solution.

How to Undo and Redo in C# (Rich Text Box)

I've been trying to get undo and redo working in my text editor for about 3 days now. It's doing my head in.
I have a text box (named richTextBoxPrintCtrl1), which I would like to be able to undo and *redo *(word by word).
So if I click the undo button, it undoes the last word. And if I then clicked the redo button, it redoes the last word.
Could somebody help me get this working?
richTextBoxPrintCtrl1.Undo(); doesn't work very well. It deletes everything typed in the text box.
Thanks in advance for your help.
I know this question has been asked many times before, but I can't get it working using the information from the questions I've browsed here on SO.
Ok, I'll post some code to help you get started. first you need to listen for the TextChanged event.
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
and you need to declare a stack in the class
Stack<string> undoList = new Stack<string>();
In the text changed handler, you need to add the contents of the textbox to the stack
void textBox1_TextChanged(object sender, EventArgs e)
{
undoList.Push(textBox1.Text);
}
Then you need to handle the undo, so you could simply use CTRL-Z
textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Z && (e.Control)) {
textBox1.Text = undoList.Pop();
}
}
You can have the RichTextBox build the undo stack for you word by word, plus keep track of the caret position, by handling one of the KeyDown-, KeyPress- or KeyUp events like this:
private void rtb_KeyDown(object sender, KeyEventArgs e)
{
// Trick to build undo stack word by word
RichTextBox rtb = (RichTextBox)sender;
if (e.KeyCode == Keys.Space)
{
this.SuspendLayout();
rtb.Undo();
rtb.Redo();
this.ResumeLayout();
}
// eztnap
}
Since the RichTextBox does the job for you, you just have to call rtb.Undo() or rtb.Redo() from wherever you need to.

After F1 was pressed help is not displayed

I do not understand why i did not get the help message after pressing the F1 key. When on windows form i got for instance one button and it has the focus the message is displayed as expected (after pressing F1) but when i got an empty form this is not happening. I suppose that an empty form will have by default focus set on it. ( i read that this event will be raised after pressing F1 for the control which got the focus)
Is this the right behavior, or i am missing something about the "HelpRequested" event on an empty form ? Is this the right way to raise the event based on focus or it could be configured to be raised also on another event (something like onMouseOver) ? May i create my own event and raise it ? (i do not want to add a special button only for help, for example press this button and display the help, help should be displayed only after F1 was pressed).
This is the code:
private void Form1_Load(object sender, EventArgs e)
{
Form1.ActiveForm.HelpRequested += new HelpEventHandler(helpReq);
//button1.HelpRequested += new HelpEventHandler(helpReq);
}
private void helpReq(object sender, HelpEventArgs hlpevent)
{
MessageBox.Show(((Control)sender).Text);
}
using Form1.ActiveForm, is not recommended. Change it to this
there is no Text property associated with Control. Do you mean Tag?
after performing the help event, you should set the HelpEventArgs.Handled to true
instead of using events, you can just override OnHelpRequested in your form.
I would do someething like this:
protected override void OnHelpRequested(object sender, HelpEventArgs e)
{
MessageBox.Show((Control) sender).Tag);
e.Handled = true;
base.OnHelpRequested(sender, e);
}

Categories

Resources