Ray Gudgeon Ответов: 1

Проблема с флажком в toolstrip


У меня есть приложение, созданное несколько лет назад в Visual Studio 2010. Он широко использует флажок во многих полосах инструментов в приложении. Все они прекрасно работают.

Недавно я начал создавать новое приложение в WS 2019, используя большую часть кода из старого приложения. Все в порядке, кроме флажков в полосах инструментов. Когда я пытаюсь вставить флажок в любую из полосок инструментов формы, флажок недоступен в раскрывающемся списке. Кроме того, в списке есть ложный элемент под названием crtoolstriptectbox, который появляется под разделителем, где ожидался элемент checkbox. Эта ложная запись на самом деле работает, вставляя текстовое поле (которое не требуется).

Класс, который должен вставить флажок в любую из полос инструментов во время разработки, этого не делает. однако если я создам новое тестовое решение и скопирую его в класс, а затем вставлю toolstrip в тестовую форму, то он будет работать идеально, и ложное crtoolstriptectbox не появится.

Я искал все приложение в поисках экземпляра поддельного элемента, и notheing появляется. Я попытался создать новое решение и скопировал все файлы из нерабочей версии, но результат тот же. Ложная запись появляется в раскрывающемся списке, а флажок не появляется. Я не могу создать флажок в полосе инструментов ни в одной из форм.

вот этот код...

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;

            // Remove the event.

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

        // Declare the DateChanged event.

        public event EventHandler CheckedChanged;

        // Raise the DateChanged event.

        private void OnCheckedChanged(object sender, EventArgs e)
        {
            if (CheckedChanged != null)
            {
                this.CheckedChanged(this, e);
            }
        }

        void OnMouseHover(object sender, EventArgs e)
        {
            this.OnMouseHover(e);
        }
    }
}


Что я уже пробовал:

Я попытался воссоздать приложение в новом решении и попробовал тесты на приведенном выше коде. Все тесты и все исходное приложение работают, но новое приложение не создаст флажок в полосе инструментов

Gerry Schmitz

Вы создали свой собственный флажок? Придерживайтесь того, что дано в Windows Forms, если вы хотите иметь жизнь.

1 Ответов

Рейтинг:
1

Ray Gudgeon

I solved this problem. I was implementing the use of a checkbox in a toolstrip while there were still many errors in the solution brought over from the original application. Thus at that time the new app would not compile.
I was thus running a build that failed (I knew it would). I was assuming that the class that was managing the creation of the required checkbox and adding it into the toolstrip would just ‘save’ as all the files do in a compile and all would be good.
I found the result when I cleaned up the 50 or so errors sitting in the solution. I was then able to run a successful compile and ‘hey presto’ the system worked exactly as required. The dropdown list on the toolstrip now displays the ‘checkbox’ and clicking it installs a checkbox correctly. 
Thanks to those that responded to this question.