0

Im making a calculator.and for the buttons that type numbers, I wrote a condition that if the focus was on text box 1, it would enter the text there, if not, it would enter text box 2. But unfortunately the code does not work and I dont understand the problem. (WindosForm(.Net framework))

if (textBox1.Focus() == true)
        {
 textBox1.Text = textBox1.Text + "1";
        }
else
        {
 textBox2.Text = textBox2.Text + "1";
        }
Se Az
  • 1
  • 1
  • What UI framework is this? Winforms, WPF, UWP, WebForms, something else? – gunr2171 Jun 01 '22 at 12:14
  • Windosform(.net framework) – Se Az Jun 01 '22 at 12:16
  • Does this answer your question? [What is the preferred way to find focused control in WinForms app?](https://stackoverflow.com/questions/435433/what-is-the-preferred-way-to-find-focused-control-in-winforms-app) – gunr2171 Jun 01 '22 at 12:21
  • 1
    `Focus()` _sets_ the control as active (and returns if it was successful), not if it _does currently_ have focus. See the link above for what you're really asking - how to get the control which is currently in focus. – gunr2171 Jun 01 '22 at 12:22
  • "But unfortunately the code does not work and I dont understand the problem." welcome to stack overflow. We also don't understand the problem if you don't describe it precisely. Meaning: Tell us your expected outcome and tell us the actual outcome. The discrepancy between those 2 is the problem. :) and please post the context of this code. Where does it sit? inside a method? inside an event handler? inside the constructor. The context is of utmost importance – Mong Zhu Jun 01 '22 at 12:29

3 Answers3

0

Subscribe to "Enter" event for your two textbox and save it. Use the same method for the two textboxes.

TextBox focusedTB;
private void textBox_Enter(object sender, EventArgs e)
{
    focusedTB = sender as TextBox;
}
...
this.textBox1.Enter += new System.EventHandler(this.textBox_Enter);
...
this.textBox2.Enter += new System.EventHandler(this.textBox_Enter);

Now you know the last textbox that got focus.

private void button1_Click(object sender, EventArgs e)
{
    focusedTB.Text += "1";
}
Remi THOMAS
  • 854
  • 6
  • 11
0

Your code appears to be attempting to check if the control is focused. The correct way to do that is:

if (textBox1.Focused)
{
    // Because 'Focused' is a property. 'Focus()' is a method.
    textBox1.Text = textBox1.Text + "1";
}
.
.
.

The answer to your question Why I can not change focus? is that textBox1 receives the focus every time you call this:

if (textBox1.Focus()) 

As mentioned in one of the comments, here's how the Focus method works:

// Summary:
//     Sets input focus to the control.
//
// Returns:
//     true if the input focus request was successful; otherwise, false.
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool Focus();

Note: This is a copy-paste of metadata that you can look at by right-clicking over Focus() in your code and selecting Go to Definition then expanding the definition.

IVSoftware
  • 5,732
  • 2
  • 12
  • 23
-1

I think you talk about Windows Form ? You cannot manage like this but use event "Enter" of your textboxes, when you click inside the textbox, you give the focus to this textbox and you can do anything inside. Here I put the right focuses TextBox in a variable.

private TextBox _textBoxFocused; //this is always the righ TextBox

private void textBox1_Enter(object sender, EventArgs e)
{
    _textBoxFocused = textBox1;
}
private void textBox2_Enter(object sender, EventArgs e)
{
    _textBoxFocused = textBox2;
}
Digital3D
  • 161
  • 8