Hi all,
This post is an old post but the subject still exists.
I think I found a pretty nice and good solution for this problem.
I also think asLuc that using"OnPropertyChanged" it's not always a good idea. from the other hand writing each time code to update the data source or end the current edit, for each button on the ToolStrip is a pain.
There for I am using a (derived) inherited ToolStrip that according to my tests solves this issue. The following is the code, I have commented the lines that are specific to my project.
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace Client.UIControls
{
/// <summary>
///
/// </summary>
/// <remarks>Efy Teicher</remarks>
[ToolboxBitmap(typeof(ToolStrip))]
public partial class UCToolStrip : ToolStrip
{
public UCToolStrip()
{
InitializeComponent();
//this.BackgroundImage = global::Ness.Client.UIControls.Properties.Resources.SilverStyile;
//this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
//this.Dock = System.Windows.Forms.DockStyle.Bottom;
//this.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
//this.GripStyle = ToolStripGripStyle.Hidden;
//this.RightToLeft = RightToLeft.Yes;
this.CausesValidation = true;
}
// Let user change this property in desigen time
[Browsable(true)]
[DefaultValue(true)]
public new bool CausesValidation
{
get { return base.CausesValidation; }
set
{
base.CausesValidation=value;
}
}
/// <summary>
/// Handle any button click in order to validate focused object,
/// the object focused on the form will validate and still kip focus.
///<remarks>
///We want to make sure that controls on the containing container or even the containing form are validated, because we need them to update there Binding sources.
///</remarks>
/// </summary>
/// <param name="e"></param>
protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
{
base.OnItemClicked(e);
if (base.CausesValidation)
{
var form = this.FindForm();
if (form != null)
{
form.Validate();
}
}
}
}
}
(Please leave the credit remark commentfor this part of code if using the code or copying to a different site for benefit of the community).
Now, I am happy that in the first place I have used in our project derived and wrapped objects.
This works also on TextBoxes and GridView’s that are placed on user controls and child panels.
Pay tension to the methods we used: CausesValidation (as described here: http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.causesvalidation.aspx) is by default ‘false�and it’s not browsble at runtime, also worser even changing the value of ToolStrip.CausesValidation at runtime did not bring the expected result and did not Cause the expected validation (as described here: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22967093.html ), this is why I needed to override OnItemClicked methode.
The solusion was based on this part in the ToolStrip course http://www.scribd.com/doc/7336829/Windows-Forms-ToolStrip-Course look in page 6:
Focus/Validation
Forthemostpart,whenactivatedbythemenukey(ALT),theMenuStrip/ToolStripwilltrynottotakefocusawayfromthecontrolthatiscurrentlyfocus.
IfthereisacontrolhostedwithintheMenuStriporadropdownoftheMenuStrip,theControlwillgainfocuswhentabbedto.Ingeneral,the
GotFocus/LostFocus/Enter/LeaveeventsonMenuStripmaynotfirewhenkeyboardactivated.Insuchcases,theMenuStrip.
MenuActivate,MenuStrip.MenuDeactivateeventscanbeusedinstead.
BydefaultToolStrip.CausesValidationisFalse.ValidationcanbeperformedbymanuallycallingValidate()ontheForm.
Any feedback is appreciated.
Efy