none
How do I determine the (horizontal) size of the "selection margin" of a rich-text box? RRS feed

  • Question

  • WHAT I HAVE:

    Visual Basic 2015, WinForms

    MY PROBLEM:

    When a RichTextBox control's ShowSelectionMargin property is True, there's a left padding before any text where a user can click/double-click in order to select an entire line/paragraph. But I need to be able to determine the width of that selection-margin region (when it exists) so I can synchronize my ruler control's left edge with the leftmost possible character x-position in the rich-text control.

    The RichTextBox's GetPositionFromCharIndex(0).X method always returns the same value whether the ShowSelectionMargin property is True or False. But the actual leftmost possible character x-position is different depending on this property's value. How do I figure out the (horizontal) size of the selection margin?

    Robert Gustafson







    Tuesday, January 2, 2018 1:50 AM

Answers

  • Would the pixel count for the selection margin be the same for any display screen that uses the app?


    Robert Gustafson

     Yes,  it does appear to be a fixed value of 8 as Castorix31 has mentioned.  Although the GetPositionFromCharIndex function says it is actually 1 or 9.  I believe they use a fixed 1 pixel margin which is why it is offset to 1 or 9.   You would also need to take the RichTextBox.ZoomFactor property into account when you calculate it.

     To reproduce the same results as the GetPositionFromCharIndex function without actually having to switch the selection to 0 before getting it,  you can just use the code below to calculate it.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SelMarginWidth As Integer = GetSelMarginWidth(RichTextBox1)
            MessageBox.Show("Selection Margin: " & SelMarginWidth.ToString)
        End Sub

        Private Function GetSelMarginWidth(Rtb As RichTextBox) As Integer
            Dim smWidth As Integer = CInt(1 * Rtb.ZoomFactor)
            If Rtb.ShowSelectionMargin Then smWidth = CInt(9 * Rtb.ZoomFactor)
            Return smWidth
        End Function
    End Class
     

     EDIT:  I only tried this with my screen font scaling set to 100% so,  you may want to test it with your screen scaling set to at least 150% to see what it says.


    If you say it can`t be done then i`ll try it

    Tuesday, January 2, 2018 10:37 AM

All replies

  • WHAT I HAVE:

    Visual Basic 2015, WinForms

    MY PROBLEM:

    When a RichTextBox control's ShowSelectionMargin property is True, there's a left padding before any text where a user can click/double-click in order to select an entire line/paragraph. But I need to be able to determine the width of that selection-margin region (when it exists) so I can synchronize my ruler control's left edge with the leftmost possible character x-position in the rich-text control.

    The RichTextBox's GetPositionFromCharIndex(0).X method always returns the same value whether the ShowSelectionMargin property is True or False. But the actual leftmost possible character x-position is different depending on this property's value. How do I figure out the (horizontal) size of the selection margin?

    Robert Gustafson







     There is no built in,  or not built in methods that i have seen to get the width of the Selection Margin.  It would be easier to just add your own SelectionMarginWidth Integer type property to your UserControl Editor class.  Then you can use the value of that property to offset the Left margin using the method i showed you in your last question to set the Margins.  That is about all the options i see possible for doing this.

     PS - Please edit your thread and change it to a Question thread instead of a Discussion thread.  Also,  i am out of here for the night so,  i will not be replying back until tomorrow after work.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, January 2, 2018 3:35 AM
    Tuesday, January 2, 2018 3:34 AM
  • Would the pixel count for the selection margin be the same for any display screen that uses the app?


    Robert Gustafson

    Tuesday, January 2, 2018 4:28 AM
  • Are you sure that GetPositionFromCharIndex(0).X does not work? Seems that it returns 1 or 9 depending on ShowSelectionMargin.


    • Edited by Viorel_MVP Tuesday, January 2, 2018 6:19 AM
    Tuesday, January 2, 2018 6:18 AM
  • Hi RobertGustafason,

    I use the following code and get different value.

     Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
            Dim val = RichTextBox1.SelectionStart
            Dim value = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart).X
            Console.WriteLine("The selectionstart is {0}, x is {1}", val, value)
        End Sub

    If I misunderstand your mean, you can provide your code here, and let us to know how do you do.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 2, 2018 8:20 AM
    Moderator
  • But I need to be able to determine the width of that selection-margin region (when it exists)

    It is the "Selection Bar" (ES_SELECTIONBAR) and it has a default size of 8 pixels

    (then the RichTextBox control uses HIMETRIC units for its calculations)

    Tuesday, January 2, 2018 9:45 AM
  • Would the pixel count for the selection margin be the same for any display screen that uses the app?


    Robert Gustafson

     Yes,  it does appear to be a fixed value of 8 as Castorix31 has mentioned.  Although the GetPositionFromCharIndex function says it is actually 1 or 9.  I believe they use a fixed 1 pixel margin which is why it is offset to 1 or 9.   You would also need to take the RichTextBox.ZoomFactor property into account when you calculate it.

     To reproduce the same results as the GetPositionFromCharIndex function without actually having to switch the selection to 0 before getting it,  you can just use the code below to calculate it.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SelMarginWidth As Integer = GetSelMarginWidth(RichTextBox1)
            MessageBox.Show("Selection Margin: " & SelMarginWidth.ToString)
        End Sub

        Private Function GetSelMarginWidth(Rtb As RichTextBox) As Integer
            Dim smWidth As Integer = CInt(1 * Rtb.ZoomFactor)
            If Rtb.ShowSelectionMargin Then smWidth = CInt(9 * Rtb.ZoomFactor)
            Return smWidth
        End Function
    End Class
     

     EDIT:  I only tried this with my screen font scaling set to 100% so,  you may want to test it with your screen scaling set to at least 150% to see what it says.


    If you say it can`t be done then i`ll try it

    Tuesday, January 2, 2018 10:37 AM