Afficher en anglais

Partage via


IWindowsFormsEditorService Interface

Définition

Fournit une interface utilisée par un UITypeEditor pour l'affichage de formulaires Windows ou d'un contrôle dans une zone de liste déroulante à partir d'un contrôle de grille des propriétés en mode Design.

C#
public interface IWindowsFormsEditorService

Exemples

L’exemple suivant fournit un exemple UITypeEditor qui utilise un IWindowsFormsEditorService pour afficher une Form entrée utilisateur.

C#
using System;
using System.ComponentModel;
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.
    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);
            }
        }
    }
}

L’exemple suivant fournit un exemple UITypeEditor qui utilise une IWindowsFormsEditorService liste déroulante UserControl pour l’entrée utilisateur.

C#
using System;
using System.ComponentModel;
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.
    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);
            }
        }
    }
}

Remarques

La IWindowsFormsEditorService seule option est disponible par le biais de la GetService méthode du PropertyGrid contrôle.

Ce service est généralement utilisé pour afficher un formulaire à partir de la EditValue méthode d’un UITypeEditor. Lorsqu’un PropertyGrid appelle la EditValue méthode d’un UITypeEditor pour fournir une interface utilisateur pour modifier la valeur d’une propriété, la EditValue méthode est passée à IServiceProvider une référence qui peut généralement fournir une instance du IWindowsFormsEditorService. Les méthodes de ce service peuvent être utilisées pour afficher des boîtes de dialogue et des formulaires, ou pour afficher un Control conteneur déroulant qui s’affiche en haut de la grille de propriétés près de la zone du champ de valeur dont la valeur est modifiée.

Méthodes

CloseDropDown()

Ferme tous les contrôles de liste déroulante ouverts.

DropDownControl(Control)

Affiche le contrôle spécifié dans une zone de liste déroulante, au-dessous d'un champ de valeur de la grille des propriétés qui fournit ce service.

ShowDialog(Form)

Affiche le Form spécifié.

S’applique à

Produit Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7