Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > RichTextBox > radRichTextBox keypress event problem

Not answered radRichTextBox keypress event problem

Feed from this thread
  • Mobasshir avatar

    Posted on Feb 17, 2012 (permalink)

    • 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

    Reply

  • Svett Svett admin's avatar

    Posted on Feb 21, 2012 (permalink)

    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 >>

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.
  • Posted on Mar 26, 2012 (permalink)

    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. :-)




    Reply

  • J.Hoventer Master avatar

    Posted on May 8, 2012 (permalink)

    Yip, that answer would have been nice. 

    Reply

  • Svett Svett admin's avatar

    Posted on May 8, 2012 (permalink)

    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 >>

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > RichTextBox > radRichTextBox keypress event problem