代码之家  ›  专栏  ›  技术社区  ›  Tomaz Tekavec

在运行时显示maskedtextbox.mask属性的设计时弹出窗体

  •  1
  • Tomaz Tekavec  · 技术社区  · 16 年前

    有人知道是否可以打开一个弹出窗体“input mask”,当您想更改maskedtextbox编辑器的mask属性并在设计时单击该属性右侧的详细信息按钮时,会显示该窗体?

    我希望在应用程序的运行时使用相同的表单,并将其结果用于掩码字符串。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Hans Passant    16 年前

    该对话框在system.design.dll中定义,名为“maskdesignerdialog”。它是内部的,所以不能直接使用。反射可以绕过这一点。用一个示例表单来尝试,在表单上放置一个按钮和一个maskedtextbox。使窗体的代码如下所示:

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Reflection;
    
    namespace WindowsFormsApplication1 {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e) {
                Assembly asm = Assembly.Load("System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                Type editor = asm.GetType("System.Windows.Forms.Design.MaskDesignerDialog");
                ConstructorInfo ci = editor.GetConstructor(new Type[] { typeof(MaskedTextBox), typeof(System.ComponentModel.Design.IHelpService) });
                Form dlg = ci.Invoke(new object[] { maskedTextBox1, null }) as Form;
                if (DialogResult.OK == dlg.ShowDialog(this)) {
                    PropertyInfo pi = editor.GetProperty("Mask");
                    maskedTextBox1.Mask = pi.GetValue(dlg, null) as string;
                }
            }
        }
    }