简体   繁体   English

UITypeEditor无法正确关闭

[英]UITypeEditor is not closing properly

I am using a UserControl with a UITypeEditor. 我正在使用带有UITypeEditor的UserControl。 The user control has OK and Cancel buttons that do nothing except display a MessageBox with either OK or Cancel and then hide the user control. 用户控件具有“确定”和“取消”按钮,除了显示“确定”或“取消”的MessageBox,然后隐藏用户控件外,其他任何操作都没有。 But when I click one of the buttons, the PropertyGrid displays an empty box where the UserControl was until I click away. 但是,当我单击其中一个按钮时,PropertyGrid将显示一个空框,直到单击为止,UserControl才位于其中。 Then the box disappears and the dialog is displayed. 然后该框消失,并显示对话框。

在此处输入图片说明

Here is the user control code: 这是用户控制代码:

using System;
using System.Windows.Forms;

namespace j2associates.Tools.Winforms.Controls.DesignTimeSupport.SupportingClasses
{
    public partial class SimpleTest : UserControl
    {
        public bool Cancelled { get; set; }

        public SimpleTest()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Cancelled = false;
            this.Hide();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancelled = true;
            this.Hide();
        }
    }
}

Here is the UITypeEditor code: 这是UITypeEditor代码:

using System;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

using j2associates.Tools.Winforms.Controls.DesignTimeSupport.SupportingClasses;

namespace j2associates.Tools.Winforms.Controls.DesignTimeSupport.Editors
{
    internal class TimeElementsEditor : UITypeEditor // PropertyEditorBase<TimeElementsUserControl>
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (value.GetType() == typeof(j2aTimePicker.TimeElementOptions))
            {
                var editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
                if (editorService != null)
                {
                    using (var st = new SimpleTest())
                    {
                        editorService.DropDownControl(st);
                        if (st.Cancelled)
                        {
                            MessageBox.Show("Cancel");
                        }
                        else
                        {
                            MessageBox.Show("OK");
                        }
                        editorService.CloseDropDown();
                    }
                }
            }

            return value;
        }
    }
}

Any ideas and/or suggestions would be appreciated. 任何想法和/或建议将不胜感激。

Sigh, it's always easy when you find it. igh,找到它总是很容易的。

I needed to pass the IWindowsFormsEditorService in via an overloaded constructor, cache it and then call it's CloseDropdown method in the Button Click events instead of hiding the user control. 我需要通过重载的构造函数传递IWindowsFormsEditorService,对其进行缓存,然后在Button Click事件中调用它的CloseDropdown方法,而不是隐藏用户控件。 It now works as expected. 现在可以正常工作了。

/// <summary>
/// Displays an OK and Cancel button. When one is pressed, 
/// the dialog is closed and a message box is displayed.
/// The actual value of the property is unchanged throughout.
/// </summary>
/// <remarks>The ToolboxItem attribute prevents the control from being displayed in the ToolKit.</remarks>
[ToolboxItem(false)]
public partial class SimpleTest : UserControl
{
    public bool Cancelled { get; set; }

    private IWindowsFormsEditorService m_EditorService;

    // Require the use of the desired overloaded constructor.
    private SimpleTest()
    {
        InitializeComponent();
    }

    internal SimpleTest(IWindowsFormsEditorService editorService)
        : this()
    {
        // Cache the editor service.
        m_EditorService = editorService;
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        Cancelled = false;
        m_EditorService.CloseDropDown();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        Cancelled = true;
        m_EditorService.CloseDropDown();
    }
}

And here is the modified editor call: 这是修改后的编辑器调用:

using (var simpleTest = new SimpleTest(editorService))
{
    editorService.DropDownControl(simpleTest);
    MessageBox.Show(simpleTest.Cancelled ? "Cancelled" : "OK");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM