request
request

Reputation: 530

How to change focus on a groupbox click?

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

Upvotes: 0

Views: 210

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39142

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(); };
}

Upvotes: 2

Related Questions