Results 1 to 13 of 13

Thread: [RESOLVED] creating a gap between a highlighted word and a contextmenustrip

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Resolved [RESOLVED] creating a gap between a highlighted word and a contextmenustrip

    I am creating a gap between a highlighted word and a contextmenustrip. My contextMenu is abstracting the item I want to replace so users cannot see what they are changing. This is creating a lot of problems for me. How can you do this? Here is my code:

    Code:
                For Each replacement In replacements(checkWord)
    
                    ContextMenuStrip1.Items.Add(replacement.ToLowerInvariant, Nothing, Sub(sender As Object, e As EventArgs)
                                                                                           RichTextBox1.SelectedText = GetWordWithOutBracketedText(kamau)
                                                                                       End Sub)
                Next
    
                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: creating a gap between a highlighted word and a contextmenustrip

    Not excatly sure what you need to do in your case but heres a couple snippets I copied from my code bank that might give you some ideas,..

    Code:
    Dim pt As Point = Me.PointToClient(TextBox1.PointToScreen(TextBox1.GetPositionFromCharIndex(TextBox1.SelectionStart)))
    pt.Y += TextBox1.Font.Height
    ContextMenuStrip1.Show(PointToScreen(pt))
    Code:
    ' Show panel under selected word in RTB
    Private Sub MnuReplace_Click(sender As Object, e As EventArgs) Handles MnuReplace.Click
        ' get location of selected word in RTB on the screen.
        Dim pt As Point = Me.PointToClient(rtbCode.PointToScreen(rtbCode.GetPositionFromCharIndex(rtbCode.SelectionStart)))
        ' Check X location, make sure panel wont overlap right side of form.
        If pt.X + PanelReplace.Width > Me.DisplayRectangle.Right Then
            pt.X = Me.DisplayRectangle.Right - PanelReplace.Width
        End If
        ' Set Y location, if panel placed under word will it overlap bottom of form?
        If pt.Y + TextRenderer.MeasureText(rtbCode.SelectedText, rtbCode.Font).Height + PanelReplace.Height > Me.DisplayRectangle.Bottom Then
            ' panel would overlap bottom of form so adjust Y above selected word.
            pt.Y -= PanelReplace.Height
        Else
            ' Set Y so panel is under the selected word.
            pt.Y += TextRenderer.MeasureText(rtbCode.SelectedText, rtbCode.Font).Height
        End If
        ' move panel into place.
        PanelReplace.Location = pt
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: creating a gap between a highlighted word and a contextmenustrip

    I was saying, when the suggestion appears, it appears next to the word on the right side, thus preventing the other words to be replaced from being seen! It should appear below that sentence not on the same sentence at the side. Do you understand better now?

    If you are replacing the words: The whole thing should be erased

    With: The whole concept should be erased.

    The contextMenu will only allow you to see: The whole thing, but should be erased.

    This is what is causing the problem here! It should appear at the next line instead of appearing on the Right side of the same sentence! Can you help me with that please? I can practically do nothing!

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: creating a gap between a highlighted word and a contextmenustrip

    Give this a try. It assumes that RichTextBox1.ContextMenuStrip is set to ContextMenuStrip1.

    Code:
        Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
            Dim pos As Drawing.Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
            ContextMenuStrip1.Show(RichTextBox1, pos, ToolStripDropDownDirection.AboveRight)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: creating a gap between a highlighted word and a contextmenustrip

    Quote Originally Posted by nqioweryuadfge View Post
    It should appear below that sentence not on the same sentence at the side.
    Thats what my first example shows how to do,

    Code:
        ' show context menu under the start of selected text.
    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
    EDIT: replace code
    Last edited by Edgemeal; Jun 18th, 2016 at 02:40 PM.

  6. #6
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: creating a gap between a highlighted word and a contextmenustrip

    Personally I think the contextmenu is a poor choice for this kind of operation.

    If I were doing it I would use an always on top form to display the selections and have it popup at a fixed (but redefinable) position. It would also allow you to move it out of the was should you need to do so.

    Just sayin...
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: creating a gap between a highlighted word and a contextmenustrip

    So, how do we go about creating this popup Box?

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: creating a gap between a highlighted word and a contextmenustrip

    Quote Originally Posted by nqioweryuadfge View Post
    So, how do we go about creating this popup Box?
    Is the purpose of the context menu / popup to give the user a choice of items?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: creating a gap between a highlighted word and a contextmenustrip

    Quote Originally Posted by nqioweryuadfge View Post
    So, how do we go about creating this popup Box?
    It would just be another form you define at design-time with the topmost property set to true.
    or make it a dialog box if you do not want the user changing text while it is shown
    populate it as you like with whatever controls you like.

    Tain't rocket science.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: creating a gap between a highlighted word and a contextmenustrip

    Yes, thanks for the code. Let me try it, then I will get back to you!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: creating a gap between a highlighted word and a contextmenustrip

    It looks good! But is there a way to remove the highlighted text once you select the next item? Here is a rough code, but it doesn't reset the color. What I observed is that one cannot pinpoint which word is being highlighted next because the previous words are highlighted! So, I thought maybe there should be one way to reset the color once you move to the next item to allow the user to know which word has been highlighted next! Can you help with this final problem?

    Code:
                    endindex = checkWord.Length
                    RichTextBox1.Select(foundIndex, endindex)
                    RichTextBox1.SelectionColor = Color.Blue

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

    Re: creating a gap between a highlighted word and a contextmenustrip

    Quote Originally Posted by nqioweryuadfge View Post
    It looks good! But is there a way to remove the highlighted text once you select the next item?
    This Q really has nothing to do with this thread and should be posted in its own thread, its best to post one Q per thread.

    The solution may depend on how text is displayed, maybe try something like in this example, otherwise you'll probably have to create a few variables and keep track of what was selected previously and restore that text only.

    If the original problem of this thread has been solved then please mark this thread as resolved from the "Thread Tools" menu near the top-right of this thread. Thanks!

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: [RESOLVED] creating a gap between a highlighted word and a contextmenustrip

    Edgemeal! All your answers here have worked except for the huge chunk of code that you had first posted. So, the smaller ones have worked! I just returned to say thank you! You too dbasnett!

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