Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do make a ContextMenu to display items in a vertical line in VB.NET?

My problem is that when items are displayed in the context, they are displayed one character per line.


Here is my problem code:

VB
Public Class Form1
    Friend WithEvents RichTextBox1 As New RichTextBox With {.Dock = DockStyle.Fill}
    Friend WithEvents ReplaceMenu As New ContextMenuStrip

    Private replacements As New Dictionary(Of String, String)


    Private nextCheckIndex As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Controls.Add(RichTextBox1)


        replacements.Add("a bad advice", "a bad suggestion")
        replacements.Add("antelope", "gazelle")
        replacements.Add("awkward", "clumsy")
        replacements.Add("anxiety", "stress")


        RichTextBox1.Text = "You are anxiety a bad antelope, irregardless of your a bad advice. You provided a bad advice, irregardless of your intention. "
    End Sub
Posted
Updated 25-May-16 22:38pm
v4
Comments
CHill60 12-Dec-15 9:22am    
Where's the rest of your code? Nothing here is setting up a ContextMenu
[no name] 12-Dec-15 9:24am    
One second.
[no name] 12-Dec-15 9:28am    
Check now!
[no name] 12-Dec-15 9:29am    
Press F8 to test the program

1 solution

Put a breakpoint inside your routine CheckForReplacementText and examine what is returned by
For Each replacement In replacements(checkWord)
replacements(checkWord) returns a single String - first time around it returns "a bad advice". So that For Each loop is looping around the individual characters in that string, not as you believed, each of the entries in the Dictionary.

You also have some issues around how you are keeping track of which suggestions you have already made ... not sure why you think recursion is necessary, but if you are going to use it remember to increment nextCheckIndex appropriately

[EDIT] - how checkWord should be added to the context menu
With ReplaceMenu.Items.Add(replacements(checkWord))
    .AutoToolTip = True
    .ToolTipText = replacements(checkWord)
End With
 
Share this answer
 
v2
Comments
[no name] 12-Dec-15 9:54am    
I did not understand it
CHill60 12-Dec-15 9:57am    
Did not understand what? How to put a breakpoint on, where to put it, how to examine the contents of a variable?
CHill60 12-Dec-15 10:08am    
You don't put breakpoints on in any language - it's the IDE that allows for that i.e. Visual Studio.
There are several ways to put a breakpoint in place (or remove it): Highlight the line you want to stop at and hit F9 OR select the Debug menu, Toggle Breakpoint OR click in the left-hand grey margin next to the line in question.
Once execution of the program pauses you can examine the variables by hovering over them, looking in the Locals and/or Autos window, typing "?" + variable name or expression in the Immediate window, or by setting a watch
CHill60 12-Dec-15 11:00am    
I agree it doesn't solve the problem but it does show what is going on i.e. why you have a single letter per line of your context menu.
I've updated my solution to make it clearer.
You will still need to deal with how you identify which items should appear on that menu. Hint - you probably do not need a recursive routine to do that (especially as you clear the menu within the routine)
CHill60 12-Dec-15 11:05am    
There is a solution here. I am not going to write all of your code for you, you have to put some effort in for yourself.
I've pointed out what the problem was and how to fix it. I've also pointed out what your next problem is going to be and hinted at what to avoid when you're fixing it.
If you want more than that you will probably have to start paying someone to do your homework for you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900