vendredi 13 septembre 2019

Problem inserting a checkbox into a toolstrip in winforms

I have an application created some years ago in Visual Studio 2010. It extensively uses a checkbox in many of the toolstrips in the app. All of them work perfectly.

I have recently started to create a new app in WS 2019 using much of the code from the old app. All is OK except the checkboxes in the toolstrips. When I try to insret a checkbox into any of the form toolstrips the checkbox is not in the drop down. Furthermore there is a spurious item in the list called crtoolstriptectbox which appears below the separator where the checkbox item was expected. This spurious entry actually works inserting a textbox (which is not wanted).

The class that is supposed to insert a checkbox in any of the toolstrips does not. however if I create a new test solution and copy in the class then insert a toolstrip in a test form it works perfectly and the spurious crtoolstriptectbox does not appear.

I have searched the whole app looking for an instance of the spurious item and nothing comes up. I have tried creating a new solution and copied all the files from the non working version but the result is the same. The spurious entry appears in the dropdown list and the checkbox does not appear. I cannot create a checkbox in the toolstrip in any of the forms.

here is the code I am using...

I have searched the whole app looking for an instance of the spurious item and nothing comes up. I have tried creating a new solution and copied all the files from the non working version but the result is the same. The spurious entry appears in the dropdown list and the checkbox does not appear. I cannot create a checkbox in the toolstrip in any of the forms.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;
namespace tt
{
    [System.ComponentModel.DesignerCategory("code")]

    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
    // [ToolboxBitmap(@"..\..\Images\CheckBox.bmp")]
    public class ToolStripCheckBox : ToolStripControlHost
    {

        public ToolStripCheckBox()
            : base(new CheckBox())
        { }

        public CheckBox checkBoxControl
        {
            get
            {
                return Control as CheckBox;
            }
        }

        public bool isChecked
        {
            get
            {
                return checkBoxControl.Checked;
            }
            set
            {
                checkBoxControl.Checked = value;
            }
        }

        protected override void OnSubscribeControlEvents(Control c)
        {
            // Call the base so the base events are connected.

            base.OnSubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.

            CheckBox CheckBoxControl = (CheckBox)c;

            // Add the event.

            CheckBoxControl.CheckedChanged += new EventHandler(OnCheckedChanged);
        }

        protected override void OnUnsubscribeControlEvents(Control c)
        {
            // Call the base method so the basic events are unsubscribed.

            base.OnUnsubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.

            CheckBox CheckBoxControl = (CheckBox)c;




Aucun commentaire:

Enregistrer un commentaire