You can do it by creating a context menu strip add menu item Paste and assign it shortcut key Ctrl+V. Now for any control you wish to have Ctrl+V functionlaity set its ContextMenyStrip property to the context menu strip you have created for example contextmenustrip1.
Now Create a class variable for a active control for example c and on Mouse Enter (focus)event of each control you want to have Ctrl+V funcaitonlay set c to the that control on which got focuse so that we have active control reference.
Now in menu item click write the code to set the text for example c.text="some data from clipboard" etc.
I can't see that this will do the trick either. My main goal here is, when the user presses CTRL + F3 in a textbox /datagrid etc the program copies some text into that textbox/datagrid.
My program copies the text to the clipboard. I just need to automatically paste it to where the cursor is located. I can't bind this functionallity to one textbox. It has to be valid for all textboxes / datagrids / listboxes etc etc.
I do not have much experinece in c#, but i can't se that your solution will help me the way i want my program to work.
I was having a similiar problem as you were, and the reason ctrl+v wasn't working for me was because the app hadn't loaded, or been activated, yet. Try waiting for half a second and see if that makes a difference.
I have got another work around to solve your problem.Override the method ProcessCmdKey() in your container class.In your case it will be Form1().The ProcessCmdKey method will trap the ley message from the parent order.Once we trap the key,get the data from clipboard.Now you can paste it to the active control in the container.I have tried with two text boxes.It is working.Please see the following code
I must admit that that code block does similar things on my PC... one thing worth noting though, you may want to insulate your SendWait within an if statement so you don’t keep banning on pasting to the system over and over again if nothing has changed. Something like this (which could be improved with changes above):
I've tried that. But that dosn't work. My program just freezes. Any othe ideas
My test-code:‥
namespace WindowsApplication11 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Application.AddMessageFilter(new KeyFilter()); //This sets up the filter for your messages } }
Can c# perform a CTRL+V
utterplush
Hi,
You can do it by creating a context menu strip add menu item Paste and assign it shortcut key Ctrl+V.
Now for any control you wish to have Ctrl+V functionlaity set its ContextMenyStrip property to the context menu strip you have created for example contextmenustrip1.
Now Create a class variable for a active control for example c and on Mouse Enter (focus)event of each control you want to have Ctrl+V funcaitonlay set c to the that control on which got focuse so that we have active control reference.
Now in menu item click write the code to set the text for example c.text="some data from clipboard" etc.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ContextMenu
{
public partial class Form1 : Form
{
Control c = new Control();
public Form1()
{
InitializeComponent();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
c.Text = "hi";
}
private void label1_MouseEnter(object sender, EventArgs e)
{
c = label1 ;
}
private void textBox1_MouseEnter(object sender, EventArgs e)
{
c = textBox1;
}
}
}
Hope this will give you idea to start with.
Primate
eprog
Thanks.
I can't see that this will do the trick either. My main goal here is, when the user presses CTRL + F3 in a textbox /datagrid etc the program copies some text into that textbox/datagrid.
My program copies the text to the clipboard. I just need to automatically paste it to where the cursor is located. I can't bind this functionallity to one textbox. It has to be valid for all textboxes / datagrids / listboxes etc etc.
I do not have much experinece in c#, but i can't se that your solution will help me the way i want my program to work.
Tnx.
Karthikeyan S
ActiveControl is a property available with ContainerControl class.ActiveControl indicates the control which is active..ie, which contain the focus
Dale N
The following code gets the name if the active control is a text box.By comparing the name we can determeine which text box
Control
cnt = this.ActiveControl;if (cnt is TextBox)
{
string name = cnt.Name;
}
reader25
I must be having a bad day, because i don't understand how this works. Can i use ActiveControl do determine which textbox i am in
Can you give me a sample code
Lars E.
Mikky
That didn't work.
It i try the:
SendKeys.SendWait("^v");
The pc just freezes and the program is not responding.
Lars
Imtiaz34639
System.Threading.
Thread.Sleep(500);EnterSB
I can't do that because i don't know where the cursor is.
I want to copy the text from the clipboard to the place where the cursor is. That can be Textboxes / Cells in grid etc.
The only solution i can think of is to make my program perform a "ctrl+v". Is it possible
Lars
mattpoll
I have got another work around to solve your problem.Override the method ProcessCmdKey() in your container class.In your case it will be Form1().The ProcessCmdKey method will trap the ley message from the parent order.Once we trap the key,get the data from clipboard.Now you can paste it to the active control in the container.I have tried with two text boxes.It is working.Please see the following code
protected
override bool ProcessCmdKey(ref Message msg, Keys keyData){
bool bCtrl = false; Keys valKeys = (Keys)(int)((short)keyData); if ((keyData & Keys.Control) == Keys.Control){
bCtrl =
true;}
if ((valKeys == Keys.F3) && bCtrl){
try{
IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject(); if (iData.GetDataPresent(DataFormats.Text) || iData.GetDataPresent(DataFormats.Text)){
String text = (String)iData.GetData(DataFormats.Text); this.ActiveControl.Text = text;}
}
catch (Exception ex){
MessageBox.Show(ex.Message);}
}
return base.ProcessCmdKey(ref msg, keyData);}
Asheesh
I must admit that that code block does similar things on my PC... one thing worth noting though, you may want to insulate your SendWait within an if statement so you don’t keep banning on pasting to the system over and over again if nothing has changed. Something like this (which could be improved with changes above):
if (f1 || f2 || f3 || f4 || f5 || f6 || f7 || f8 || f9 || f10 || f11 | f12)
{
SendKeys.SendWait("^v")
}
I know that this doesn’t completely solve your problem but will surly help in reducing issues.
Mark Sargent
Thanks
I've tried that. But that dosn't work. My program just freezes.
Any othe ideas
My test-code:‥
namespace WindowsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Application.AddMessageFilter(new KeyFilter()); //This sets up the filter for your messages
}
}
public class KeyFilter : IMessageFilter
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern short GetKeyState(int keyCode);
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x0100)
{
Keys keys = Control.ModifierKeys;
bool control = GetKeyState(0x11) < 0; //Control
bool shift = GetKeyState(0x10) < 0; //Shift
bool f1 = GetKeyState(0x70) < 0;
bool f2 = GetKeyState(0x71) < 0;
bool f3 = GetKeyState(0x72) < 0;
bool f4 = GetKeyState(0x73) < 0;
bool f5 = GetKeyState(0x74) < 0;
bool f6 = GetKeyState(0x75) < 0;
bool f7 = GetKeyState(0x76) < 0;
bool f8 = GetKeyState(0x77) < 0;
bool f9 = GetKeyState(0x78) < 0;
bool f10 = GetKeyState(0x79) < 0;
bool f11 = GetKeyState(0x7A) < 0;
bool f12 = GetKeyState(0x7B) < 0;
if (control)
{
if (f1)
{
Clipboard.SetDataObject("control + f1", true);
}
if (f2)
{
Clipboard.SetDataObject("control + f2", true);
}
if (f3)
{
Clipboard.SetDataObject("control + f3", true);
}
if (f4)
{
Clipboard.SetDataObject("control + f4", true);
}
if (f5)
{
Clipboard.SetDataObject("control + f5", true);
}
}
SendKeys.SendWait("^v");
}
return false; // dont actually filter the message
}
}
}
Zahid Younas
It is very possible with the SendKeys class.
In order to send a control+v you'd simply do the the following in C#:
SendKeys.SendWait("^v");
or in VB.NET:
SendKeys.SendWait("^v")
DuckJones
Thanks.
I'm still not getting this to work. Here is a stupid question. How can i get the active control in the container
Lars E