radRichTextBox keypress event problem

5 posts, 0 answers
  1. Mobasshir
    Mobasshir avatar
    1 posts
    Member since:
    Jan 2012

    Posted 17 Feb 2012 Link to this post

    • 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
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 21 Feb 2012 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jesper
    Jesper avatar
    1 posts
    Member since:
    Dec 2011

    Posted 26 Mar 2012 Link to this post

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




  5. Jacques
    Jacques avatar
    135 posts
    Member since:
    Jun 2007

    Posted 08 May 2012 Link to this post

    Yip, that answer would have been nice. 
  6. Svett
    Admin
    Svett avatar
    728 posts

    Posted 08 May 2012 Link to this post

    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 >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready