0

How to paste the text of a button.OnClick to the currently focused TextBox? My form has one button btn1 with text "this is test" and two textboxes, txt1 and txt2.

When btn1 is clicked, its text must be pasted to whatever textbox is currently on focus.

The event of my btn1.OnClick is

txt1.text = btn1.text;

When I change focus to txt2, how can I paste the text of btn1 to txt2.text as well? So when btn1 is clicked, it's text must be pasted to whatever textbox is in focus.

deralbert
  • 856
  • 2
  • 15
  • 33
  • It should be `txt2.text = txt1.text;` – Vivek Nuna Jun 07 '20 at 09:57
  • @viveknuna, i know, but how do i do that on runtime ? What i meant was when btn1 is clicked, the value of whatever textbox is in focus must change. – Markowisz Stanislaw Jun 07 '20 at 10:00
  • must changes to what? – Vivek Nuna Jun 07 '20 at 10:02
  • To the text of btn1. The text of btn1 is "test" , so when btn1 is clicked, whatever textbox is in focus, its text will be "test" as well, hope its clear now – Markowisz Stanislaw Jun 07 '20 at 10:04
  • You can do something like this `if (txt1.Focused){ btn1.text = txt1.text; } else if(txt2.Focused) {btn1.text = txt2.text;}` – Vivek Nuna Jun 07 '20 at 10:05
  • I know, but this is not dynamic solution, imagine in a form where you have lots of textboxes, then writing bunch of conditions wont be a good idea , there must be a solution where when my button is clicked, if currently focused control is a textbox , then textbox.text = btn1.text. – Markowisz Stanislaw Jun 07 '20 at 10:08
  • If you know everythin and why are you asking question. You should mention everything in your question – Vivek Nuna Jun 07 '20 at 10:11
  • But clicking a button actually takes focus. You actually want to paste to whatever control ***was*** in focus. Otherwise there's comedy here: 1) Pick up a piece of food. Step 2) Pick up your knife and fork and eat whatever you are holding. – Wyck Jun 07 '20 at 14:17

2 Answers2

1

By the time the click event for the button has fired, the button now has focus instead of the textbox. So you need to capture which textbox last had the focus and use that.

Here is a rough and quick implementation, which should work as long as all your textboxes are on the form on load. It will work even with textboxes which aren't a direct child of the form (for example, contained within a panel or tab page):

    public IEnumerable<Control> GetAll(Control control, Type type)
    {
        var controls = control.Controls.Cast<Control>();

        return controls.SelectMany(ctrl => GetAll(ctrl, type))
                                  .Concat(controls)
                                  .Where(c => c.GetType() == type);
    }

    private TextBox lastFocussedTextbox;

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach(TextBox textbox in GetAll(this, typeof(TextBox)))
        {
            textbox.LostFocus += (_s, _e) => lastFocussedTextbox = textbox;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if(lastFocussedTextbox != null)
        {
            lastFocussedTextbox.Text = button1.Text;
        }
    }

Credit for GetAll function: https://stackoverflow.com/a/3426721/13660130

Steve Springer
  • 206
  • 2
  • 4
1
Declare global variable

private Control _focusedControl;

Attach below event to all your textboxes.
private void TextBox_GotFocus(object sender, EventArgs e)
{
    _focusedControl = (Control)sender;
}
Then in your button click event.
private void btn1_Click(object sender, EventArgs e)
{
    if (_focusedControl != null)
    {
    //Change the color of the previously-focused textbox
        _focusedControl.Text = btn1.Text;
    }
}
Rajanikant Hawaldar
  • 314
  • 1
  • 5
  • 12