Results 1 to 7 of 7

Thread: [RESOLVED] How to swift a contextmenu on the Rightside direction when it reaches at the end of

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Resolved [RESOLVED] How to swift a contextmenu on the Rightside direction when it reaches at the end of

    How do you switch the direction of a contextMenu as it draws closer to the end of a sentence? Some of my popups are longer, and it needs to be turned on the left side to allow the user to view the whole menu. I have observed the RightToLeft option on the properties window, but that does not solve my problem. Infact, it will be on the Right side now that a long list will be blocked from view. What I want is a slight modification of my code to detect the end of a sentence, and to switch to left side at the end of a sentence or closer.

    Code:
    ContextMenuStrip1.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength))

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to swift a contextmenu on the Rightside direction when it reaches at the end

    Not sure I understand the problem completely, but if you take some measurements in the menus opening event you should be able to show the menu exactly where you want it.

    Quick test, if the context menu hangs over the right side of the RTB I move the menu to the front of the selection. Basically you just have to do some math to re-position the menu.

    Code:
    Public Class Form1
    
        ' show the menu normally. (at right of selection)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ContextMenuStrip1.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength))
        End Sub
    
        ' when menu opens check its location and correct if needed.
        Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
            Dim cmsLimitRightSide = RichTextBox1.Width  ' < set limit for menu max right location.
            Dim cmsWidth = ContextMenuStrip1.Width + 4 ' width of menu + some extra pixels to compensate for "shadows under menus".
            Dim pntSelect = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength)
            ' if menu is too far right (past our right limit), then move it to the start of the rtb selection.
            If pntSelect.X + cmsWidth > cmsLimitRightSide Then
                Dim pntStart = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
                ContextMenuStrip1.Show(RichTextBox1, New Point(pntStart.X - cmsWidth, pntStart.Y))
                ' Or use ToolStripDropDownDirection for more options, 
                ' ContextMenuStrip1.Show(RichTextBox1, pntStart, ToolStripDropDownDirection.Left)
            End If
        End Sub
    
    End Class
    Last edited by Edgemeal; Jul 20th, 2016 at 10:03 PM. Reason: changed cmsLimitRightSide to RTB width

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: How to swift a contextmenu on the Rightside direction when it reaches at the end

    Edgemeal

    I don't know how much intelligent you are, but I must say, I thank you more than anything in this world! I will get back to you as soon as I have tested it! Thanks

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to swift a contextmenu on the Rightside direction when it reaches at the end

    Quote Originally Posted by nqioweryuadfge View Post
    Edgemeal

    I don't know how much intelligent you are, but I must say, I thank you more than anything in this world! I will get back to you as soon as I have tested it! Thanks
    Its really nothing more then a little bit of math, and you could use ToolStripDropDownDirection Enumeration to set the new location like dbasnett showed in your other thread. Whatever floats your boat,...

    Code:
    If pntSelect.X + cmsWidth > cmsLimitRightSide Then
        Dim pntStart = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
        ContextMenuStrip1.Show(RichTextBox1, pntStart, ToolStripDropDownDirection.Left)
    End If
    Last edited by Edgemeal; Jul 20th, 2016 at 10:08 PM. Reason: add link

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: How to swift a contextmenu on the Rightside direction when it reaches at the end

    Ok, thanks. I wanted to leave you some reputation, but the application is citing that I should spread it around first. The previous code you wrote for me has encountered some problems when I tried to integrate it on top this one. First, you can only point it at the right side instead of below. Check the code and tell me what could be wrong:


    ' show context menu under the start of selected text.
    RichTextBox1.Focus()
    With RichTextBox1
    Dim pt As Point = Me.PointToClient(.PointToScreen(.GetPositionFromCharIndex(.SelectionStart)))
    pt.Y += .Font.Height ' * Assuming all text use same Font/Height!
    ContextMenuStrip1.Show(PointToScreen(pt))

    End With

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to swift a contextmenu on the Rightside direction when it reaches at the end

    Quote Originally Posted by nqioweryuadfge View Post
    The previous code you wrote for me has encountered some problems when I tried to integrate it on top this one. First, you can only point it at the right side instead of below. Check the code and tell me what could be wrong:

    ' show context menu under the start of selected text.
    That previous code was copy/pasted from other source, and possibly out of context, Sorry!

    Again not sure I understand what you're asking, if you want the menu to be one line lower for example using the code above then simply add the textheight to the Y location of the point used to position the menu,..

    Code:
    If pntSelect.X + cmsWidth > cmsLimitRightSide Then
        Dim pntStart = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
        ' show menu one text line lower
        pntStart.Y += RichTextBox1.Font.Height ' * Assuming all text use same Font/Height!
        ContextMenuStrip1.Show(RichTextBox1, New Point(pntStart.X - cmsWidth, pntStart.Y))
    End If
    Other then this you'll need to post all the relevant code and explain in detail what you're trying to do and what the code doesn't do. Hopefully some other eyes here understand the problem better then me and can help.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: How to swift a contextmenu on the Rightside direction when it reaches at the end

    Thanks, I will mark the question as answered.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width