Search code examples
c#winformscustom-controlsbindingnavigator

ToolStripButtons of a custom BindingNavigator appear locked in the Form Designer


I'm trying to create a custom BindingNavigator control in with some extra ToolStripButton (Edit and ExportToExcel).
The ToolStripButton is added to the BindingNavigator, but I cannot select this new ToolStripButton, e.g., to add code in its Click event handler. Actually, the ToolStripButtons appear locked.

Here is my code and an image that should describe the problem:

using System.Windows.Forms;

public class BindingNavigator : System.Windows.Forms.BindingNavigator
{
    public ToolStripButton btnEdit;
    public ToolStripButton btnExcelExport;

    public BindingNavigator()
    {
        this.LayoutCompleted += BindingNavigator_LayoutCompleted;
    }

    public void BindingNavigator_LayoutCompleted(object sender, System.EventArgs e)
    {
        if (this.Items.Contains(btnEdit))
            return;
        if (this.Items.Count >= 11)
        {
            btnEdit = new ToolStripButton();
            btnEdit.Image = global::BaseControls.Properties.Resources.Edit___16x16;
            btnEdit.Name = "btnEdit";
            this.Items.Insert(10, btnEdit);

            this.Items.Add(new ToolStripSeparator());

            btnExcelExport = new ToolStripButton();
            btnExcelExport.Image = global::BaseControls.Properties.Resources.Excel___16x16;
            btnExcelExport.Name = "btnExcelExport";
            this.Items.Insert(13, btnExcelExport);
        }
    }
}

my bindingnavigator


Solution

  • The BindingNavigator class has a dedicated Designer, BindingNavigatorDesigner, derived from ToolStripDesigner.
    The Designer calls the public virtual AddStandardItems() method, which is then called by Form Designer when a BindingNavigator is added to a Form.

    To make your Buttons functional, override this method in a custom class and add new buttons here.
    Now your ToolStripButtons are serialized in the Form Designer and, if you double-click one of your custom buttons, a Click handler is added to the Form.

    Notes you can find in the .Net Source Code:

    Override this method in derived classes to define additional or alternative standard items. To ensure optimal design-time support for your derived class, make sure each item has a meaningful value in its Name property. At design time, this will be used to generate a unique name for the corresponding member variable. The item's Name property will then be updated to match the name given to the member variable.

    BindingNavigator Custom
    As you can see, now the buttons are fully functional

    I suggest you modify your custom BindingNavigator like this:

    ▶ Don't name your Custom Control BindingNavigator

    public class BindingNavigatorEx : BindingNavigator
    {
        private ToolStripItem btnEdit;
        private ToolStripItem btnExcelExport;
    
        public BindingNavigatorEx() { }
    
        public override void AddStandardItems()
        {
            base.AddStandardItems();
            Items.Add(new ToolStripSeparator());
    
            btnEdit = new ToolStripButton() {
                Image = Properties.Resources.Edit___16x16,
                Name = "bindingNavigatorButtonEdit",
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            Items.Add(btnEdit);
    
            btnExcelExport = new ToolStripButton() {
                Image = Properties.Resources.Excel___16x16,
                Name = "bindingNavigatorButtonExcelExport",
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            Items.Add(btnExcelExport);
        }
    }