If the ToolStrip is anything like the old ToolBar, it doesn't receive focus. I assume that the reason you want to do this is because you're running into problems with databound controls on your Form/UserControl because they don't lose focus when a ToolBar is clicked (this is also the case with Menus). Thus it is not forcing the control's value into it's databound object. You need something that will force this for every control you have. It can get complicated.
I have a method that I have named ForceBind() in most of my controls (those that have it implement an Interface I have created for this type of behavior). If the control is a container object that implements that Interface (such as a Panel, UserControl, etc.) it will call the ForceBind() method of *it's* ActiveControl (if *that* control implements the Interface).
The actual ForceBind() method is pretty complicated in my controls and relies on the fact that I have a DataBind() method on my controls that tell it which DataTable and DataColumn I am binding that control to. However, I've done a little experimenting and I see that we can find other ways of finding those fields. Here's the ForceBind() method for MyTextBox (along with the code for determining the m_BoundTable, m_BoundColumn and m_BoundProperty fields):
private DataTable m_BoundTable = (DataTable)this.DataBindings[0].DataSource;
private string m_BoundColumn = this.DataBindings[0].DataBindings[0].BindingMemberInfo.BindingMember;
private string m_BoundProperty = this.DataBindings[0].PropertyName;
private CurrencyManager oCurrency = (CurrencyManager)this.BindingContext[m_BoundTable];
public virtual void ForceBind()
{
if (this.DataBindings.Count == 0)
return;
int nRow = -1;
if (this.m_BoundTable != null && this.m_BoundColumn != "")
{
if (this.oCurrency != null)
nRow = this.oCurrency.Position;
if (nRow >= 0)
{
DataRow row = this.m_BoundTable.DefaultView[nRow].Row;
object oValue = row[this.m_BoundColumn];
switch (this.m_BoundProperty)
{
case "Text" :
oValue = this.Text;
break;
case "Tag" :
oValue = this.Tag;
break;
}
if (oValue == null)
oValue = DBNull.Value;
System.RuntimeTypeHandle handle = System.Type.GetTypeHandle(this);
System.Type eType = System.Type.GetTypeFromHandle(handle);
ConvertEventArgs e = new ConvertEventArgs(oValue, eType);
this.ParseHandler(this, e);
if (row[this.m_BoundColumn] != e.Value)
{
try
{
row[this.m_BoundColumn] = e.Value;
}
catch (Exception)
{ }
this.OnValidated(new EventArgs());
}
}
}
}
~~Bonnie