I have a rich text box(richTextBox1) in my program as shown bellow. But when I right click on it, it doesn\'t pop up a “copy, cut, past” window. Can you please tell
Thanks to @Jaex
https://stackoverflow.com/a/36624233/5132252
https://stackoverflow.com/a/435510/5132252
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
internal static extern IntPtr GetFocus();
private Control GetFocusedControl()
{
Control focusedControl = null;
// To get hold of the focused control:
IntPtr focusedHandle = GetFocus();
if (focusedHandle != IntPtr.Zero)
// Note that if the focused Control is not a .Net control, then this will return null.
focusedControl = Control.FromHandle(focusedHandle);
return focusedControl;
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Clipboard.ContainsText())
{
var FocusedControl = GetFocusedControl();
if (FocusedControl != null)
switch (FocusedControl.GetType().Name)
{
case "RichTextBox":
{
var RichTextBox = FocusedControl as RichTextBox;
RichTextBox.Paste();
break;
}
case "TextBox":
{
var TextBox = FocusedControl as TextBox;
TextBox.Paste();
break;
}
}
}
}