Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello Brothers, Programmer, Master.
 
Please help me... Let me learn by your example. Smile | :)
 
Description :
The word are shown in a popup when a word is selected and right-clicked.
For example,
INPUT : "Barcelona is my favorite football club".
 
After I selected the word "football", then the right-clicked and the word are shown in a popup menu "This is football".
 
When I clicked those word in popup menu, it will replace the word in INPUT
with the word in pop up menu, like this example.
 
OUTPUT : Barcelona is my favorite This is football club."
 
Please.. help me.
 
Here the code
ContextMenu contextMenu = new ContextMenu();
private EventHandler menuHandler;
 
public Form1()
{
    InitializeComponent();
    menuHandler = new System.EventHandler(this.Menu_Click);// what's menu_click?
}
 
private void Menu_Click(object sender, EventArgs e)
{
    richTextBox1.SelectionFont = new Font("Times New Roman", 12);
    richTextBox1.SelectionColor = Color.Black;
 
    richTextBox1.SelectedText = ((MenuItem)sender).Text;
}
 
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
   try
   {
      if (e.Button == MouseButtons.Right)
      {
         Point point = new Point(e.X, e.Y);
         int index = richTextBox1.GetCharIndexFromPosition(point);
         textBox1.Text = Convert.ToString(index);
 
         int length = 1;
 
         if (!Char.IsWhiteSpace(richTextBox1.Text[index]))
         {
             while (index > 0 && !Char.IsWhiteSpace(richTextBox1.Text[index - 1]))
             { index--; length++; }
 
              while (index + length < richTextBox1.Text.Length &&
                  !Char.IsWhiteSpace(richTextBox1.Text[index + length]) &&
                  (!Char.IsPunctuation(richTextBox1.Text[index + length]) ||
                  richTextBox1.Text[index + length] == Char.Parse("'"))
              ) length++;
 
              richTextBox1.SelectionStart = index;
              richTextBox1.SelectionLength = length;
              contextMenu.MenuItems.Clear(); // error here
              contextMenu.MenuItems.Add("This is "+richTextBox1.SelectedText, menuHandler); //error here
             //What's next Sir?
             }
         }
     }
  }
Posted 15-Mar-13 3:13am
Edited 15-Mar-13 19:33pm
v8
Comments
Jegan Thiyagesan at 15-Mar-13 8:22am
   
Sounds like this is your homework!
anyhow,
what have you searched for?
What have you tried?
where did you got stuck?
Show us your code fragments.
Berry Harahap at 15-Mar-13 8:57am
   
@Jegan Thiyagesan
I've search for toolTip, but it just show information, can't be clicked.
Then, I'm conceiving ContextMenuStrip. I'm not good at ContextMenuStrip, I need suggestion Sir.
If you have reference, please give me. :) :) :) Nice.
Cheers
Jegan Thiyagesan at 19-Mar-13 9:24am
   
you can always use a label instead of tool tip or context menu. The label can displayed your selected text as well as it can take mouse click event.
 
So
Label lbl = new Label();
lbl.width = 100;
lbl.Height = 50;
lbl.Location = new Point(richTextBox1.Location.X + 100, richTextBox1.Location.Y + 100);
lbl.Click += button_Click;
lbl.Visible = false;
lbl.SentToBack();
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectedText.Length > 0)
{
// do your football comparison
// then
if (football)
{
lbl.Text = "this is football";
lbl.Visible = true;
lbl.BringToFront();
}
}
}
 
void button_Click(object sender, RoutedEventArgs e)
{
lbl.Visible = false;
lbl.SentToBack();
richTextBox1.Text = richTextBox1.Text.Replace("football", "This is football"); // set or append the text here.
 
// also get the index and the length of "this is football", change the font if you want.
}

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You really should try using Google or Yahoo or any other search engine.
However here is a step by step guide to setting up a context menu
http://www.c-sharpcorner.com/uploadfile/mahesh/context-menu-in-C-Sharp/[^]
And to be on the safe side here is some information about RichTextBox http://www.dotnetperls.com/richtextbox[^]
 
[Edit]
Here is a (probably) better solution. On my form I have a RichTextBox (richTextBox1) into which I have placed some text. and a TextBox (textBox1) into which I have also typed some text. I highlight some text in richTextBox1 and use mouse-right-click to show the context menu. When I click on the menu it replaces the selected text with the words from the menu item.
 
       private void DefineYourMenu()
        {
            // Set up a context menu item as the text from the text box
            ContextMenu Contextmenu1 = new ContextMenu();
            MenuItem menuItem1 = new MenuItem(this.textBox1.Text); 
            // You need to add a handler for the click event on the menu item
            menuItem1.Click += menuItem1_Click; 
            
            // Add the new menu item to the context menu
            Contextmenu1.MenuItems.Add(menuItem1); 
            // "attach" this context menu to the richtext box
            richTextBox1.ContextMenu = Contextmenu1;
        }
        private void menuItem1_Click(object sender, EventArgs e) 
        {
            // Replace the highlighted text with the menu option selected
            richTextBox1.Text = richTextBox1.Text.Replace(richTextBox1.SelectedText.Trim(), ((MenuItem)sender).Text);
        }
        private void richTextBox1_Click(object sender, EventArgs e)
        {
            // This is as convenient a place as any to set up the menu
            DefineYourMenu();
        }
  Permalink  
v2
Comments
Berry Harahap at 16-Mar-13 0:26am
   
Sir @CHill60
Any suggestion for my code above (question)?
Please :) :) :)
CHill60 at 19-Mar-13 9:11am
   
I've added to my original solution

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



Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 19 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100