Hi,
I have a strange situation with right-click
I have one form upon which I have one text box and one rich textbox (among many other controls).
When I right-click the textbox, I get a normal menu with copy-paste-delete etc. controls ( I dont remember defining it...).
When right-clicking the rich textbox, I get nothing.
As I wanted also to have right-click on the rich textbox, I searched to find how the text box right-click is defined, but could not find any sign of it.
I compared the properties - found nothing.
I searched the entire solution for right-click (searched mouse, right, click, etc.) and could not find any clue where the right-click command and definitions are hidden.
It must be defined somewhere, but where?
Following several tests, I found out that text boxes (as well as numeric up-down) will always have right-click menue, while rich text boxes will never have one.
The MSDN library states the following:
"By default, both TextBox and RichTextBox have a context menu that appears when a user right-clicks inside the control. The context menu allows the user to cut, copy, or paste"
Well, this seems to be false, or have I done something to remoev it??
sam
Loading
VulpesPosted Feb 26, 2012, 7:32 AM
I only have VS 2010 (and the Express version to boot) but, on there, if you drag a ContextMenuStrip component onto your form from the ToolBox, a box pops up saying 'Type Here'.
If you then type on successive lines : Cut, Copy and Paste you should find when you scroll through the controls in the Properties Box that 3 new ones have been added: cutToolStripMenuItem, copyToolStripMenuItem and pasteToolStripMenuItem.
You then need to add a Click eventhandler for each of these controls from the Properties Box and type in the code below:
Finally, in the Properties Box for the RichTextBox itself you need to set its ContextMenuStrip property to contextMenuStrip1 or whatever you've named it.
When you right click on the RichTextBox, a context menu should then pop up with these 3 options and function as expected.
Sutha ShaliniPosted Mar 19, 2013, 6:53 AM
SamPosted Feb 26, 2012, 9:19 AM
With your kind help I managed to get what I wanted, and could add more functionality to my RichTextBox Right-click.
It seems somewhat cumbersome, but eventually it runs.
By the way, the MSDN does not refer at all to the VS version, but rather to the .NET framework version, which, in this case can be 3.5 or 4.0RichTextBox Overview
Thanks all for your kind and persistence support,
Sam
SamPosted Feb 26, 2012, 4:33 AM
Thanks for your reply.
As I wrote, the MSDN states that a rich text box has a right click menu by default, (yes, also in VS2008) which does not seem to be the case, but this is probably MS to blame for.
Nevertheless, I tried your proposal as is, but I think your example is not complete (assuming I know all the rest? No, I do not!).
I could not see how you initialize the new components (like bttnCut. etc).
To make it work, I copied the initializations manually one by one from your previous example (which is probably VS2010 and will not run on my 2008), the program compiles OK, and works, but this is not the way to go.
In the Rich text box contextMenuStrip properties there are many options to set, but I assume there is a default that makes it work as-is.
Can you please elaborate and send the whole picture?
There must be a 'clean' way of doing it, without the risky copy.
I found out that many people consider the right-click as a difficult issue. (I can make a simple right-click method that can do anything, but this is not what I am looking for)
Thanks again, sam.
FroglegPosted Feb 25, 2012, 3:17 PM
Drag a context menu onto the form from the toolbox
Add three menu items as below
In richtextbox properties assign contectmenu1
here is code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void bttnCut_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(richTextBox1.SelectedText))
{
return;
}
Clipboard.SetText(richTextBox1.SelectedText);
richTextBox1.SelectedText = string.Empty;
// Clipboard.SetDataObject(objects);
}
private void bttnCopy_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(richTextBox1.SelectedText))
{
return;
}
Clipboard.SetText(richTextBox1.SelectedText);
}
private void bttnPaste_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = Clipboard.GetText();
}
}
}
FroglegPosted Feb 25, 2012, 3:11 PM