2

I have an Windows Form app with multiple Textboxes. I'm trying to add a right-click copy & paste function into it. I have added the right-click menu by using the ContextMenuStrip. However, I'm using this menu for 2 different textboxes in the app. When I select and highlight some text from 1 text box and then do the same thing by selecting and highlighting text from the other textbox, it copies in the text from both textboxes. How do I separate it so that it knows which textbox to copy from?

This is the code I have:

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string leadSelectedText = leadsTextBox.SelectedText;
            string resultSelectedText = resultTextBox.SelectedText;

            Console.WriteLine(leadSelectedText);
            Console.WriteLine(resultSelectedText);
        }
Danny
  • 9,199
  • 16
  • 53
  • 75
  • Why did you tell about the ContextMenuStrip, but show us a copyToolStripMenuItem_Click method? – 3per Nov 23 '15 at 14:53
  • because that's the Item within that Context Menu?? – Danny Nov 23 '15 at 14:56
  • Sorry, look this http://stackoverflow.com/questions/4886327/determine-what-control-the-contextmenustrip-was-used-on – 3per Nov 23 '15 at 15:01

3 Answers3

1

If you want to determine which textbox the mouse is over you could try the following.

        if (leadsTextBox == GetChildAtPoint(MousePosition))
        {
            //copy from leads text
        }
        else if (resultTextBox == GetChildAtPoint(MousePosition))
        {
            //copy from results text
        }

If the mouse doesn't have to be over the textbox to copy, you could always store which textbox was last clicked.

Textbox lastClicked;

private void leadsTextbox_mouseDown()
{
   if (!string.IsNullOrEmpty(leadsTextbox.SelectText))
       lastClicked = leadsTextbox;
}
cshearn
  • 96
  • 4
  • I think when the user click menu item, the mouse cursor may not be over the textbox. – 3per Nov 23 '15 at 14:55
0

You could use ActiveControl to get the currently active textbox. You would first have to click into this textbox to activate it. But If you just select text and then right-click this will do.

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
  System.Windows.Forms.Control control = this.ActiveControl;
        if (control.GetType() == typeof(System.Windows.Forms.TextBox))
            System.Console.WriteLine(((System.Windows.Forms.TextBox)control).SelectedText);
}
drvolcano
  • 110
  • 9
0

I did something like this,

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        List<TextBox> box = new List<TextBox>();
        box = pMainScreen.Controls.OfType<TextBox>().Where(c => c.Name.Contains("")).ToList();

        foreach(TextBox b in box)
        {
            if (b.SelectionLength > 0)
                label7.Text = b.Text;
        }
    }

So here are few things about this function,
~This will identify the textbox where the text had been previously highlighted
~Issue: It is possible that more than one textbox can have highlighted text at a time, so it will only identify the last one it hits in the loop

You'll need to add some validation ontop of this, but I'm pretty sure this does what your OP describe.
-Also it will be Console.WriteLine(b.Text); for your program.
-All my controls were inside a panel called pMainScreen, you can use this.controls.oftype.blahblah instead to identify your textboxes.

Timmy
  • 543
  • 1
  • 7
  • 19