bacjoe

I'm working an MDI app where I've added a non-MDI form that acts as a floating toolbar. This form has only a toolstrip control with a series of ToolStripButtons that allows the user to select from several different operation modes. When I click on any of the tool strip buttons, I set the checked property for that button to true, set the checked property to false on all the other buttons then set a variable that indicates a mode for the rest of the app.

The problem I'm having is that when another form has the focus and I click one of the buttons on the toolbar, it appears that the first click only sets focus to the toolbar form and I have to click again to actually get the ToolStripButton click event.

Is there a way to get the click event the first time I click on the button




Re: Windows Forms General ToolStrip Button Focus

Yu Guo – MSFT

Hi, bacjoe,

Based on my understanding, you want to your ToolStripButton get clicked with one click in an inactive Form, don't you

I think althrough this behavior is by design, you can still change it with some coding.

You can simulate a click when the Form is Activated.

For example,

Code Block

private void Form1_Activated(object sender, EventArgs e)

{

if (toolStripButton1.Bounds.Contains(PointToClient(Cursor.Position)))

{

toolStripButton1.PerformClick();

}

}

More info

http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.activated.aspx

Hope this helps,

Regards






Re: Windows Forms General ToolStrip Button Focus

bacjoe

Thanks, , this worked perfectly. I actually loop through all the buttons as shown below and it does exactly what I need.




Code Block


private void Form1_Activated(object sender, EventArgs e)
{
foreach (object item in ToolStrip!.Items)
{
if (item is ToolStripButton)
{
if (((ToolStripButton)item).Bounds.Contains(PointToClient(Cursor.Position)))
{
((ToolStripButton)item).PerformClick();
break;
}
}
}
}