1

有谁知道是否可以打开一个弹出表单“输入掩码”,当您想更改 MaskedTextBox 编辑器的 Mask 属性并在设计时单击该属性右侧的详细信息按钮时显示该表单?

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

4

1 回答 1

1

该对话框在 System.Design.dll 中定义,名为“MaskDesignerDialog”。它是内部的,因此您不能直接使用它。反射可以绕过它。尝试使用示例表单,在表单上放置一个 Button 和一个 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;
            }
        }
    }
}
于 2009-11-16T21:49:48.803 回答