Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to determine which textbox is focused and add text to it...........?

Tags:

c#

winforms

im doing a complex calculator in C#. the first textbox accepts the real part and the second accepts the immaginary part. i wanna be able to use the mouse to enter values also. so if i click button1 it will concat "1" to the value in the textbox on which the focus is on . i cant determine which textbox is focused. i tried the stuff some people posted, like using GotFocus for exemple, and non had worked..

    private Control focusedControl;

    private void TextBox_GotFocus(object sender, EventArgs e)
    {
        focusedControl = (Control)sender;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        if (focusedControl != null)
        {
            focusedControl.Focus();
            SendKeys.Send("1"); 

        }

    }
like image 281
joseph Avatar asked Jan 23 '26 18:01

joseph


1 Answers

        if ((txtBox as Control).Focused)
        {

        }
like image 162
GaidenFocus Avatar answered Jan 26 '26 10:01

GaidenFocus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!