472,954 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 8903
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"...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.