Need to retrieve text behind cursor

SyntaxEditor for Windows Forms Forum

Posted 15 years ago by Nassim Farhat
Version: 4.0.0276
Avatar
Hello ActiPro team,

I have two quick questions.
Q1-
I need a dialog window to open up when I click on ctrl-B in the syntaxeditor control. SO I guess that this can be accomplshed using keypress events, if so do you have an example for this?

Q2- Also, I want to be able to press on ctrl-B and capture the text where the cursor is placed on and pass that text to my dialog in the keypress event. (just to let you know, I used dynamic grammer definition to get up and running fast)

SO, if I have such a code segment:

if (test == "sigma1")
{
int new_variable;
new_variable = 5;
}

And my cursor is on new_variable, I want to capture that text into a string and pass it to my dialog, is this easy to do? And if for example I have "new_variable=5" all stuck together, can the same thing be accomplished? Meaning that if the cursor is on new_variable it will capture only new_variable into a string and not the "=5" part?

Thank you for your time
nassim

Comments (7)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Nassim,

1) You could look at the KeyDown event for that key combination. KeyPress only does typed characters, KeyDown will do hotkeys like Ctrl+B. We don't have a sample but it's standard Windows Forms so you can find samples all over the web for that.

2) Try this:
string text = editor.SelectedView.GetCurrentWordText();


Actipro Software Support

Posted 15 years ago by Nassim Farhat
Avatar
Thank you so much for your help,
Everything works perfectly as you said!

Regards
Nassim
Posted 15 years ago by Nassim Farhat
Avatar
Thanks for your hints from the last message.

I have another issue:
Right now I would like to select the word, highlighted and delete/replace it with a new text.

For the highlight I'm doing the following:

  private void HighLightSelectedWord()
        {



            HighlightingStyle highlightingStyle = new HighlightingStyle("style", null, Color.Empty, Color.LightGray);

            // Create a layer
            SpanIndicatorLayer layer = new SpanIndicatorLayer("test", 1);
            this.STsyntaxEditor.Document.SpanIndicatorLayers.Add(layer);


            TextRange textRange = this.STsyntaxEditor.SelectedView.GetCurrentWordTextRange();

            // Create a highlight span indicator
            HighlightingStyleSpanIndicator indicator = new HighlightingStyleSpanIndicator("indicator", highlightingStyle);
            layer.Add(indicator, textRange);
        }
And then I call the following to delete the selected text:

this.STsyntaxEditor.SelectedView.DeleteSelectedText(ActiproSoftware.SyntaxEditor.DocumentModificationType.Delete, ActiproSoftware.SyntaxEditor.DocumentModificationOptions.SelectInsertedText);
But It does not delete the word that I higlighted.

Would you have some hints on maybe a better way of doing this? Or maybe I'm way off?

Reagards
Nassim
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Nassim,

I think you may be confusing some things. The "selection" is what is the blue highlighting as you select text with the mouse/keyboard and has nothing to do with span indicators. I think your span indicator code is unnecessary.

The EditorView.DeleteSelectedText only deletes any text that is currently selected, again not relating at all to the span indicators.

I would suggest you do something like this instead, which does a programmatic delete of some text:
TextRange textRange = this.STsyntaxEditor.SelectedView.GetCurrentWordTextRange();
this.STSyntaxEditor.Document.DeleteText(DocumentModificationType.Delete, textRange);


Actipro Software Support

Posted 15 years ago by Nassim Farhat
Avatar
YEs...yes... that's exactly what I wanted to do. Wow, I was way off!
thank you so much for this. I got things working the way I wanted.

There was one more thing I was wondering though, how can I automatically highlight the word behind the cursor (i.e. make a blue highlighting of text selection which is represented by the word that the cursor is on)? So for that one, I'm pretty sure I have to use the spanindicator, so I did so, but after adding the "HighlightingStyleSpanIndicator" on my text range I'm unable Remove it afterwards!

I saw a post that said:

"The Document.Indicators collection has a GetSpanIndicatorsForOffset method that returns an array containing the SpanIndicator objects that contain the specified offset. Use that to know what indicators are at an offset."

But I was unable to find the Document.Indicators collection? Am I missing an assembly reference to get this?

Thanks again for your time
Nassim
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
If you mean text selection, then you'd do something like:
editor.SelectedView.Selection.SelectRange(textRange);
Indicators are not needed for that, they are used for things like showing breakpoints, etc. Does this solve your problem?


Actipro Software Support

Posted 15 years ago by Nassim Farhat
Avatar
Wow, that was quick thank you for your support...

Acutally I found what could help me.

Like you said "Indicators are not needed for that", I went directly to the selection and did the following:

TextRange textRange = this.STsyntaxEditor.SelectedView.GetCurrentWordTextRange();
this.STsyntaxEditor.SelectedView.Selection.SelectWord();
Fixed my issue perfectly!

Thanks again for your great support.

Regards
Nassim
The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.