首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITypeEditor未正确关闭

UITypeEditor未正确关闭
EN

Stack Overflow用户
提问于 2015-07-13 19:53:31
回答 1查看 505关注 0票数 0

我正在使用带有UITypeEditor的UserControl。用户控件具有“确定”和“取消”按钮,这两个按钮除了显示带有“确定”或“取消”的MessageBox,然后隐藏用户控件外,什么也不做。但是当我单击其中一个按钮时,PropertyGrid会显示一个空的框,在我离开之前UserControl一直在那里。然后,该框消失,并显示该对话框。

下面是用户控制代码:

代码语言:javascript
复制
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();
        }
    }
}

下面是UITypeEditor代码:

代码语言:javascript
复制
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;
        }
    }
}

任何想法和/或建议都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2015-07-14 12:20:35

叹息,当你找到它的时候,它总是很容易。

我需要通过一个重载的构造函数传入IWindowsFormsEditorService,缓存它,然后在Button Click事件中调用它的CloseDropdown方法,而不是隐藏用户控件。它现在可以像预期的那样工作。

代码语言:javascript
复制
/// <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();
    }
}

下面是修改后的编辑器调用:

代码语言:javascript
复制
using (var simpleTest = new SimpleTest(editorService))
{
    editorService.DropDownControl(simpleTest);
    MessageBox.Show(simpleTest.Cancelled ? "Cancelled" : "OK");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31382667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档