0

I'm trying to reset the focus when clicking outside a textbox placed inside a groupbox. What is the best way to do so? As I saw right there is no such event like mouseclick/click for a groupbox.

It should move the focus when a click is done, not when the mouse leaves the box or anything similar.

Any ideas?

enter image description here

request
  • 450
  • 1
  • 4
  • 10

1 Answers1

2

As I saw right there is no such event like mouseclick/click for a groupbox.

Here's a LITERAL answer to your problem.

In the Load() event of your Form, cast the GroupBox back to the generic Control class so you can wire up the Click() event (or a similar event from the ones available):

private void Form1_Load(object sender, EventArgs e)
{
    ((Control)groupBox1).Click += Ctl_Click;
}

private void Ctl_Click(object sender, EventArgs e)
{
    textBox1.Focus();
}

Or with an anonymous delegate so you don't need the separate helper method:

private void Form1_Load(object sender, EventArgs e)
{
    ((Control)groupBox1).Click += (s2, e2) => { textBox1.Focus(); };
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40