473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ContextMenuStrip - determine source while using keyboard shortcut

I have two RichTextBox controls on a form, and single
ContextMenuStrip
control which serves for both textboxes.

I need to determine which RichTextBox control invokes an event handler
in
ContextMenuStrip.

So I use that code:
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
((RichTextBox)contextMenuStrip.SourceControl).Past e();
}

It works good if I press right mouse button and select paste item,
but if I try Ctrl+V instead of use mouse I get NullReferenceException.

What to do, to make this thing work both for using mouse and keyboard
shortcut?

Regards

Jun 3 '07 #1
3 8948
On Jun 4, 12:56 am, omm...@wp.pl wrote:
I have two RichTextBox controls on a form, and single
ContextMenuStrip
control which serves for both textboxes.

I need to determine which RichTextBox control invokes an event handler
in
ContextMenuStrip.

So I use that code:
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
((RichTextBox)contextMenuStrip.SourceControl).Past e();

}

It works good if I press right mouse button and select paste item,
but if I try Ctrl+V instead of use mouse I get NullReferenceException.

What to do, to make this thing work both for using mouse and keyboard
shortcut?

Regards
This problem has occured since you have specified Ctrl+V as shortcut
key for paste. In that case if we press Ctrl + V the SourceControl for
contextmenu will not be set and will be null. Just check the
condition.

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (contextMenuStrip1.SourceControl == null)
{
foreach( Control ctrl in this.Controls)
{
RichTextBox activeBox = ctrl as RichTextBox;
if (activeBox != null && activeBox.ContainsFocus)
activeBox.Paste();
}
return;
}
((RichTextBox)contextMenuStrip1.SourceControl).Pas te();
}

If you don't want to use this type of logic, you can remove the
shortcut key on context menu. Richtextcontrol will auto detect Ctrl+V
combination and paste data from clipboard.


Jun 4 '07 #2
On Jun 4, 12:56 am, omm...@wp.pl wrote:
I have two RichTextBox controls on a form, and single
ContextMenuStrip
control which serves for both textboxes.

I need to determine which RichTextBox control invokes an event handler
in
ContextMenuStrip.

So I use that code:
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
((RichTextBox)contextMenuStrip.SourceControl).Past e();

}

It works good if I press right mouse button and select paste item,
but if I try Ctrl+V instead of use mouse I get NullReferenceException.

What to do, to make this thing work both for using mouse and keyboard
shortcut?

Regards

This problem has occured since you have specified Ctrl+V as shortcut
key for paste in context menu item. In that case if we press Ctrl + V
the SourceControl for
contextmenu will not be set and will be null. Just check the
condition.

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (contextMenuStrip1.SourceControl == null)
{
foreach( Control ctrl in this.Controls)
{
RichTextBox activeBox = ctrl as RichTextBox;
if (activeBox != null && activeBox.ContainsFocus)
activeBox.Paste();
break;
}
return;
}
((RichTextBox)contextMenuStrip1.SourceControl).Pas te();
}
If you don't want to use this type of logic, you can remove the
shortcut key on context menu. Richtextcontrol will auto detect Ctrl+V
combination and paste data from clipboard. If we specify it
explicitly, Ctrl+V for context menu item will be detected.

Jun 4 '07 #3
I try this simple solution and it works fine:
private void kopiujToolStripMenuItem_Click(object sender, EventArgs e)
{
RichTextBox control = ActiveControl as RichTextBox;
if (control != null)
{
control.Copy();
}
}

Regards

Jun 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
7
by: Dave | last post by:
How do I get my ContextMenuStrip to receive key down preview events? I have the event hooked but I don't see the key strokes in the event? Dave
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
1
by: =?Utf-8?B?TGVudXNpYQ==?= | last post by:
In my program I dynamically create treeViews and dynamically add nodes to them. To each node I attach ContextMenuStrip which suppose allow delete selected node e.t.c. How from inside click...
7
by: Joe Cool | last post by:
Let's say I have a Treeview control on a form. Each leaf node in the Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and all ToolStripMenuItems Click event is handled by a comment...
1
slapshock
by: slapshock | last post by:
Good day, please help me on my problem.... i want to create a context menu strip on run time but i got this error, please help me, i tried to search on the net but i cant fine any solution...
9
by: horizon | last post by:
I have a single ContextMenuStrip (with a single menu item) associated with two ListViews, How do I determine which of the two ListViews the menu event occured on? I could probably find the control...
8
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
2
by: eBob.com | last post by:
I've been working on an app which has an array of RichTextBoxes. And I have a context menu for the RTBs. The context menu has two levels; the first level has two items, "Load Sample Text File"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.