ContextMenuStrip - determine source while using keyboard shortcut

O

ommail

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).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 NullReferenceException.

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

Regards
 
A

Aneesh P

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).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 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).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.
 
A

Aneesh P

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).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 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).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.
 
O

ommail

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top