[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))
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
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!
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
Re: creating a gap between a highlighted word and a contextmenustrip
Quote:
Originally Posted by
nqioweryuadfge
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
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...
Re: creating a gap between a highlighted word and a contextmenustrip
So, how do we go about creating this popup Box?
Re: creating a gap between a highlighted word and a contextmenustrip
Quote:
Originally Posted by
nqioweryuadfge
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?
Re: creating a gap between a highlighted word and a contextmenustrip
Quote:
Originally Posted by
nqioweryuadfge
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.
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!
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
Re: creating a gap between a highlighted word and a contextmenustrip
Quote:
Originally Posted by
nqioweryuadfge
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!
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!