473,659 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ContextMenuStri p - determine source while using keyboard shortcut

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

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

So I use that code:
private void pasteToolStripM enuItem_Click(o bject sender, EventArgs e)
{
((RichTextBox)c ontextMenuStrip .SourceControl) .Paste();
}

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 NullReferenceEx ception.

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

Regards

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

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

So I use that code:
private void pasteToolStripM enuItem_Click(o bject sender, EventArgs e)
{
((RichTextBox)c ontextMenuStrip .SourceControl) .Paste();

}

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 NullReferenceEx ception.

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 pasteToolStripM enuItem_Click(o bject sender, EventArgs e)
{
if (contextMenuStr ip1.SourceContr ol == null)
{
foreach( Control ctrl in this.Controls)
{
RichTextBox activeBox = ctrl as RichTextBox;
if (activeBox != null && activeBox.Conta insFocus)
activeBox.Paste ();
}
return;
}
((RichTextBox)c ontextMenuStrip 1.SourceControl ).Paste();
}

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
ContextMenuStri p
control which serves for both textboxes.

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

So I use that code:
private void pasteToolStripM enuItem_Click(o bject sender, EventArgs e)
{
((RichTextBox)c ontextMenuStrip .SourceControl) .Paste();

}

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 NullReferenceEx ception.

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 pasteToolStripM enuItem_Click(o bject sender, EventArgs e)
{
if (contextMenuStr ip1.SourceContr ol == null)
{
foreach( Control ctrl in this.Controls)
{
RichTextBox activeBox = ctrl as RichTextBox;
if (activeBox != null && activeBox.Conta insFocus)
activeBox.Paste ();
break;
}
return;
}
((RichTextBox)c ontextMenuStrip 1.SourceControl ).Paste();
}
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 kopiujToolStrip MenuItem_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
6079
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 during production. Usually, this only wraps code used to control quitting the whole app versus just shutting a form, but it can also control many other things. However, as part of the build before delivering an update, I have to remember to...
7
7587
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
2308
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 add-ons for this purpose? If not, any urls which demonstrates code snippets management?
1
5934
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 event handling function to find which node was selected? sender is set to ToolStripItem clicked.
7
7836
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 event handler. How do I determine which Treeview Node signalled the menu event in the common event handler?
1
5104
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 there... my code is: Imports System.ComponentModel.CancelEventArgs Public Class VinteryBase Private m_New As Boolean Sub CreateMenu() Dim mnuItem As ToolStripMenuItem
9
2333
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 through coordinates, but is there an "Object" variable somewhere holding this information?
8
5293
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 has the focus and handle ctrl-c, ctrl-v, or any other shortcuts. I have the same shortcuts working in my app, but have not determined how to find out which control has focus. Would I set up a loop or code for each control at form level. Any help...
2
3432
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" and "Insert Row >"; the "Insert Row >" item on the first level invokes, if that's the right word, a second level which has two items, "Above" and "Below". This all worked when it was a ContextMenu. But I am trying to change it to use the new menu...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.