copy and paste text
-
Hi, I am programmig an app with a few window forms and users can copy text from controls(e.g. textBox) from one form to another by highlighting the text and click the copy menuItem in the mainMenu (not using contextMenu coz there would be no problem by using contextMenu with the control) The problem is, how can the app know which control the text is copied from when the user clicks the copy menuItem ? Do I need to register a control highlight or ???? event to each control so the app can know which control is being highlighted with text ? But it seems quite tedious coz there are many textBoxes on each form and may affect the performance coz many event listeners are registerd. Any better solution ? Thanks
-
Hi, I am programmig an app with a few window forms and users can copy text from controls(e.g. textBox) from one form to another by highlighting the text and click the copy menuItem in the mainMenu (not using contextMenu coz there would be no problem by using contextMenu with the control) The problem is, how can the app know which control the text is copied from when the user clicks the copy menuItem ? Do I need to register a control highlight or ???? event to each control so the app can know which control is being highlighted with text ? But it seems quite tedious coz there are many textBoxes on each form and may affect the performance coz many event listeners are registerd. Any better solution ? Thanks
Hi, I think in the mainMenu click event you can just check the focus control and then if the focus control is a TextBox then you can get the selected text from it to use in copy operation.
Saqib
-
Hi, I think in the mainMenu click event you can just check the focus control and then if the focus control is a TextBox then you can get the selected text from it to use in copy operation.
Saqib
-
but if I click the mainMenu, the focus will shift to the mainMenu, then there is no way to get the textbox ??
you can get the focus for TextBox try this code in menu click event.;)
if (this.textBox1.Focused) { // .... } else if (this.textBox2.Focused) { // .... }
Saqib
-
Hi, I am programmig an app with a few window forms and users can copy text from controls(e.g. textBox) from one form to another by highlighting the text and click the copy menuItem in the mainMenu (not using contextMenu coz there would be no problem by using contextMenu with the control) The problem is, how can the app know which control the text is copied from when the user clicks the copy menuItem ? Do I need to register a control highlight or ???? event to each control so the app can know which control is being highlighted with text ? But it seems quite tedious coz there are many textBoxes on each form and may affect the performance coz many event listeners are registerd. Any better solution ? Thanks
If you want to copy and paste text from one control to another, you use the ClipBoard.
-
Hi, I think in the mainMenu click event you can just check the focus control and then if the focus control is a TextBox then you can get the selected text from it to use in copy operation.
Saqib
you can use this code it is working , i tested when you click the menuItem the textBox focus will not change
private void PastItem_Click(object sender, System.EventArgs e)
{
if (textBox1.Focused)
{
textBox1.Text = str;
}
else if (textBox2.Focused)
{
textBox2.Text = str;
}
else if(textBox3.Focused)
{
textBox3.Text = str;
}
}private void CopyItem_Click(object sender, System.EventArgs e)
{
if(textBox1.Focused)
{
str = textBox1.SelectedText;
}
else if (textBox2.Focused)
{
str = textBox2.SelectedText;
}
else if(textBox3.Focused)
{
str = textBox3.SelectedText;
}
}erfan