Export (0) Print
Expand All

IWindowsFormsEditorService Interface

Provides an interface for a UITypeEditor to display Windows Forms or to display a control in a drop-down area from a property grid control in design mode.

Namespace:  System.Windows.Forms.Design
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public interface IWindowsFormsEditorService

The IWindowsFormsEditorService type exposes the following members.

  NameDescription
Public methodCloseDropDownCloses any previously opened drop down control area.
Public methodDropDownControlDisplays the specified control in a drop down area below a value field of the property grid that provides this service.
Public methodShowDialogShows the specified Form.
Top

The IWindowsFormsEditorService is only available through the GetService method of the PropertyGrid control.

This service is typically used to display a form from the EditValue method of a UITypeEditor. When a PropertyGrid invokes the EditValue method of a UITypeEditor to provide a user interface for editing the value of a property, the EditValue method is passed a reference to an IServiceProvider that can typically provide an instance of the IWindowsFormsEditorService. The methods of this service can be used to display dialog boxes and forms, or to show a Control within a drop down container that is shown on top of the property grid near the area of the value field whose value is being edited.

The following example provides an example UITypeEditor that uses an IWindowsFormsEditorService to display a Form for user input.

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

namespace IWindowsFormsEditorServiceExample
{	
    // Example UITypeEditor that uses the IWindowsFormsEditorService  
    // to display a Form.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
    public class TestDialogEditor : UITypeEditor
    {
        public TestDialogEditor()
        {
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            // Indicates that this editor can display a Form-based interface. 
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(
            ITypeDescriptorContext context, 
            IServiceProvider provider, 
            object value)
        {
            // Attempts to obtain an IWindowsFormsEditorService.
            IWindowsFormsEditorService edSvc = 
                (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc == null)
            {
                return null;
            }

            // Displays a StringInputDialog Form to get a user-adjustable  
            // string value. 
            using (StringInputDialog form = new StringInputDialog((string)value))
            {
                if (edSvc.ShowDialog(form) == DialogResult.OK)
                {
                    return form.inputTextBox.Text;
                }
            }

            // If OK was not pressed, return the original value 
            return value;
        }        
    }

    // Example Form for entering a string. 
    internal class StringInputDialog : Form
    {
        private System.Windows.Forms.Button ok_button;
        private System.Windows.Forms.Button cancel_button;
        public System.Windows.Forms.TextBox inputTextBox;

        public StringInputDialog(string text)
        {
            InitializeComponent();
            inputTextBox.Text = text;
        }

        private void InitializeComponent()
        {
            this.ok_button = new System.Windows.Forms.Button();
            this.cancel_button = new System.Windows.Forms.Button();
            this.inputTextBox = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            this.ok_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.ok_button.Location = new System.Drawing.Point(180, 43);
            this.ok_button.Name = "ok_button";
            this.ok_button.TabIndex = 1;
            this.ok_button.Text = "OK";      
            this.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK;            
            this.cancel_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.cancel_button.Location = new System.Drawing.Point(260, 43);
            this.cancel_button.Name = "cancel_button";
            this.cancel_button.TabIndex = 2;
            this.cancel_button.Text = "Cancel";            
            this.cancel_button.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.inputTextBox.Location = new System.Drawing.Point(6, 9);
            this.inputTextBox.Name = "inputTextBox";
            this.inputTextBox.Size = new System.Drawing.Size(327, 20);
            this.inputTextBox.TabIndex = 0;
            this.inputTextBox.Text = "";            
            this.inputTextBox.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.ClientSize = new System.Drawing.Size(342, 73);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.inputTextBox,
                                                                          this.cancel_button,
                                                                          this.ok_button});
            this.MinimumSize = new System.Drawing.Size(350, 100);
            this.Name = "StringInputDialog";
            this.Text = "String Input Dialog";
            this.ResumeLayout(false);
        }
    }

    // Provides an example control that displays instructions in design mode, 
    // with which the example UITypeEditor is associated. 
    public class WinFormsEdServiceDialogExampleControl : UserControl
    {
        [EditorAttribute(typeof(TestDialogEditor), typeof(UITypeEditor))]
        public string TestDialogString
        {
            get
            {
                return localDialogTestString;
            }
            set
            {
                localDialogTestString = value;
            }
        }
        private string localDialogTestString;

        public WinFormsEdServiceDialogExampleControl()
        {
            localDialogTestString = "Test String"; 
            this.Size = new Size(210, 74);
            this.BackColor = Color.Beige;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if( this.DesignMode )
            {
                e.Graphics.DrawString("Use the Properties window to show", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 5);
                e.Graphics.DrawString("a Form dialog box, using the", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 17);
                e.Graphics.DrawString("IWindowsFormsEditorService, for", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 29);
                e.Graphics.DrawString("configuring this control's", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 41);
                e.Graphics.DrawString("TestDialogString property.", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 53);
            }
            else
            {
                e.Graphics.DrawString("This example requires design mode.", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 5);
            }
        }
    }
}

The following example provides an example UITypeEditor that uses an IWindowsFormsEditorService to display a drop-down UserControl for user input.

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

namespace IWindowsFormsEditorServiceExample
{	
    // Example UITypeEditor that uses the IWindowsFormsEditorService to  
    // display a drop-down control.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    public class TestDropDownEditor : System.Drawing.Design.UITypeEditor
    {
        public TestDropDownEditor()
        {
        }
	
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            // Indicates that this editor can display a control-based  
            // drop-down interface. 
            return UITypeEditorEditStyle.DropDown;
        }
	
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            // Attempts to obtain an IWindowsFormsEditorService.
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if( edSvc == null )
                return value;       

            // Displays a drop-down control.
            StringInputControl inputControl = new StringInputControl((string)value, edSvc);
            edSvc.DropDownControl(inputControl);
            return inputControl.inputTextBox.Text;
        }
    }

    // Example control for entering a string. 
    internal class StringInputControl : System.Windows.Forms.UserControl
    {
        public System.Windows.Forms.TextBox inputTextBox;
        private System.Windows.Forms.Button ok_button;
        private System.Windows.Forms.Button cancel_button;
        private IWindowsFormsEditorService edSvc;

        public StringInputControl(string text, IWindowsFormsEditorService edSvc)
        {
            InitializeComponent();
            inputTextBox.Text = text;
            // Stores IWindowsFormsEditorService reference to use to  
            // close the control. 
            this.edSvc = edSvc;
        }

        private void InitializeComponent()
        {
            this.inputTextBox = new System.Windows.Forms.TextBox();
            this.ok_button = new System.Windows.Forms.Button();
            this.cancel_button = new System.Windows.Forms.Button();
            this.SuspendLayout();
            this.inputTextBox.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.inputTextBox.Location = new System.Drawing.Point(6, 7);
            this.inputTextBox.Name = "inputTextBox";
            this.inputTextBox.Size = new System.Drawing.Size(336, 20);
            this.inputTextBox.TabIndex = 0;
            this.inputTextBox.Text = "";
            this.ok_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.ok_button.Location = new System.Drawing.Point(186, 38);
            this.ok_button.Name = "ok_button";
            this.ok_button.TabIndex = 1;
            this.ok_button.Text = "OK";
            this.ok_button.Click += new EventHandler(this.CloseControl);
            this.cancel_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.cancel_button.DialogResult = System.Windows.Forms.DialogResult.Cancel;            
            this.cancel_button.Location = new System.Drawing.Point(267, 38);
            this.cancel_button.Name = "cancel_button";
            this.cancel_button.TabIndex = 2;
            this.cancel_button.Text = "Cancel";
            this.cancel_button.Click += new EventHandler(this.CloseControl);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.cancel_button,
                                                                          this.ok_button,
                                                                          this.inputTextBox});
            this.Name = "StringInputControl";
            this.Size = new System.Drawing.Size(350, 70);
            this.ResumeLayout(false);
        }

        private void CloseControl(object sender, EventArgs e)
        {
            edSvc.CloseDropDown();
        }
    }

    // Provides an example control that displays instructions in design mode, 
    // with which the example UITypeEditor is associated. 
    public class WinFormsEdServiceDropDownExampleControl : UserControl
    {
        [EditorAttribute(typeof(TestDropDownEditor), typeof(UITypeEditor))]
        public string TestDropDownString
        {
            get
            {
                return localDropDownTestString;
            }
            set
            {       
                localDropDownTestString = value;
            }
        }

        private string localDropDownTestString;

        public WinFormsEdServiceDropDownExampleControl()
        {
            localDropDownTestString = "Test String";
            this.Size = new Size(210, 74);
            this.BackColor = Color.Beige;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if( this.DesignMode )
            {
                e.Graphics.DrawString("Use the Properties window to show", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 5);
                e.Graphics.DrawString("a drop-down control, using the", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 17);
                e.Graphics.DrawString("IWindowsFormsEditorService, for", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 29);
                e.Graphics.DrawString("configuring this control's", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 41);
                e.Graphics.DrawString("TestDropDownString property.", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 53);
            }
            else
            {
                e.Graphics.DrawString("This example requires design mode.", new Font("Arial", 8), new SolidBrush(Color.Black), 5, 5);
            }
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft