This is a migrated thread and some comments may be shown as answers.

radRichTextBox keypress event problem

4 Answers 156 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Mobasshir
Top achievements
Rank 1
Mobasshir asked on 17 Feb 2012, 07:31 AM
  • I am facing the following problem in radRichTextBox of winform that works fine in .net RichTextBox control.

    1.radRichTextBox  has no SelectionStart  property that i need .
    2. In keypress  event e.KeyChar does not work.
     private void radRichTextBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
     if (e.KeyChar == 122)
                {
                char c= (char)0x0995;
                     e.KeyChar = c;
                    e.Handled = false;         
                
                }
            }
    here do not override the value.
    3.   e.Handled = true;
     does not work

4 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 21 Feb 2012, 12:11 PM
Hello Mobasshir,

Your question has already been answered in the support ticket that you have opened previously. Please, see our answer there for more information.

We kindly ask you to use just one support channel to contact us. Posting the same questions more than once slows down our response time because we will need to review and address two or more tickets instead of one. 

Thank you for your understanding.

Kind regards,
Svett
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Jesper
Top achievements
Rank 1
answered on 26 Mar 2012, 02:04 PM
Dear Svett
... now I know there is an answer (I have the same question as asked in this thread), but not where I can find it. That slows down my production rate. Please provide a link to the information that resolves a case.
Thank you for your understanding. :-)




0
Jacques
Top achievements
Rank 2
answered on 08 May 2012, 03:08 PM
Yip, that answer would have been nice. 
0
Svett
Telerik team
answered on 08 May 2012, 03:52 PM
Hello guys,

@Jesper, it seems that there was some issue with posting my reply to this forum thread. It was send on May 28, but it never arrived. Thanks to Jacques's post, this thread got back to our attention, thus we found out about this case. I will inform the appropriate department in order to make sure that such cases no longer appear. Please excuse us for the inconvenience caused. 

Here is the answer:
If you want to get the selection range in RadRichTextBox you should use the CaretPosition property of the Selection property. Due to the wide variety of features supported by RadRichTextBox, the selection API is different than the API of the standard RichTextBox control. You can use the following code snippet to determine the start and end position: 
DocumentPosition startPosition = this.radRichTextBox1.Document.CaretPosition;
DocumentPosition endPosition = startPosition;
  
if (this.radRichTextBox1.Document.Selection.Ranges.Count > 0)
{
    startPosition = this.radRichTextBox1.Document.Selection.Ranges.First.StartPosition;
    endPosition = this.radRichTextBox1.Document.Selection.Ranges.Last.EndPosition;
}

More about the selection API can be found in the online documentation

To get the selected text use the following code snippets:
private readonly static PropertyInfo CurrentPositionHandlerProperty = typeof(DocumentPosition).GetProperty("CurrentPositionHandler", BindingFlags.NonPublic | BindingFlags.Instance);

private string GetText()
{
    DocumentPosition startPos = new DocumentPosition(this.radRichTextBox1.Document);
    startPos.MoveToFirstPositionInDocument();
  
    DocumentPosition endPos = this.radRichTextBox1.Document.CaretPosition;
  
    StringBuilder textBuilder = new StringBuilder();
  
    while (startPos < endPos)
    {
        SpanLayoutBox spanLayoutBox = startPos.GetCurrentSpanBox();
        string text = spanLayoutBox != null ? spanLayoutBox.Text : null;
  
        if (spanLayoutBox != null &&
            spanLayoutBox == endPos.GetCurrentSpanBox())
        {
            SpanBoxPositionHandler positionHandler = CurrentPositionHandlerProperty.GetV
                                                     as SpanBoxPositionHandler;
  
            if (positionHandler != null)
            {
                text = text.Substring(0, positionHandler.CurrentPositionInSpanBox);
            }
        }
  
        if (!string.IsNullOrEmpty(text))
        {
            textBuilder.Append(text);
        }
  
        if (!startPos.MoveToNextSpanBox())
        {
            break;
        }
    }
  
    return textBuilder.ToString();
}

If you want to alter the KeyPress event you should create a custom InputBehavior:
public class CustomInputBehavior : InputBehavior
{
    public CustomInputBehavior(DocumentView view)
        : base(view)
    {
  
    }
  
    public override bool ProcessKeyPress(KeyPressEventArgs e)
    {
        if (e.KeyChar == 'z')
        {
            // HANDLE YOUR LOGIC HERE
        }
  
        return base.ProcessKeyPress(e);
    }
}

Then you should replace the default input behavior in the following way:
this.radRichTextBox1.DocumentView.InputBehavior = new CustomInputBehavior(this.radRichTextBox1.DocumentView);

I hope that you guys find this information useful.

All the best,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Mobasshir
Top achievements
Rank 1
Answers by
Svett
Telerik team
Jesper
Top achievements
Rank 1
Jacques
Top achievements
Rank 2
Share this question
or