views:

276

answers:

1

Does anybody know if it's possible to open a pop-up form "Input Mask" which is displayed when you want to change Mask property of MaskedTextBox editor and you click on the details button on the right side of this property in design-time?

I'd like to use the same form in run-time in an application and use its result for mask string.

+1  A: 

The dialog is defined in System.Design.dll, named "MaskDesignerDialog". It is internal so you can't use it directly. Reflection can bypass that. Try it out with a sample form, drop a Button and a MaskedTextBox on the form. Make the form's code look like this:

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;
            }
        }
    }
}
Hans Passant
Great! That works perfectly, thank you.
Leo