分享

IWindowsFormsEditorService 介面 (System.Windows.Forms.Design) | Microsoft Docs

 孙无者 2020-12-14

IWindowsFormsEditorService 介面

定義

命名空間:
System.Windows.Forms.Design
組件:
System.Windows.Forms.dll

在設計模式中提供 UITypeEditor 的介面,以顯示 Windows Form 或從屬性方格控制項的下拉區域中顯示控制項。

VB
Public Interface IWindowsFormsEditorService

範例

下列範例提供的範例 UITypeEditor 會使用 IWindowsFormsEditorService 來顯示 Form 使用者輸入的。

VB
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

' Example UITypeEditor that uses the IWindowsFormsEditorService 
' to display a Form.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> Public Class TestDialogEditor
   Inherits System.Drawing.Design.UITypeEditor
   
   Public Sub New()
    End Sub

    Public Overloads Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
        ' Indicates that this editor can display a Form-based interface.
        Return UITypeEditorEditStyle.Modal
    End Function

    Public Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object

        ' Attempts to obtain an IWindowsFormsEditorService.
        Dim edSvc As IWindowsFormsEditorService = CType(provider.GetService( _
        GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
        If edSvc Is Nothing Then
            Return Nothing
        End If

        ' Displays a StringInputDialog Form to get a user-adjustable 
        ' string value.
        Using form As New StringInputDialog(CStr(value))
            If edSvc.ShowDialog(form) = DialogResult.OK Then
                Return form.inputTextBox.Text
            End If
        End Using

        ' If OK was not pressed, return the original value
        Return value

    End Function
End Class

' Example Form for entering a string.
Friend Class StringInputDialog
    Inherits System.Windows.Forms.Form

    Private ok_button As System.Windows.Forms.Button
    Private cancel_button As System.Windows.Forms.Button
    Public inputTextBox As System.Windows.Forms.TextBox

    Public Sub New(ByVal [text] As String)
        InitializeComponent()
        inputTextBox.Text = [text]
    End Sub

    Private Sub InitializeComponent()
        Me.ok_button = New System.Windows.Forms.Button()
        Me.cancel_button = New System.Windows.Forms.Button()
        Me.inputTextBox = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        Me.ok_button.Anchor = System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right
        Me.ok_button.Location = New System.Drawing.Point(180, 43)
        Me.ok_button.Name = "ok_button"
        Me.ok_button.TabIndex = 1
        Me.ok_button.Text = "OK"
        Me.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.cancel_button.Anchor = System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right
        Me.cancel_button.Location = New System.Drawing.Point(260, 43)
        Me.cancel_button.Name = "cancel_button"
        Me.cancel_button.TabIndex = 2
        Me.cancel_button.Text = "Cancel"
        Me.cancel_button.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.inputTextBox.Location = New System.Drawing.Point(6, 9)
        Me.inputTextBox.Name = "inputTextBox"
        Me.inputTextBox.Size = New System.Drawing.Size(327, 20)
        Me.inputTextBox.TabIndex = 0
        Me.inputTextBox.Text = ""
        Me.inputTextBox.Anchor = System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right
        Me.ClientSize = New System.Drawing.Size(342, 73)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.inputTextBox, Me.cancel_button, Me.ok_button})
        Me.MinimumSize = New System.Drawing.Size(350, 100)
        Me.Name = "StringInputDialog"
        Me.Text = "String Input Dialog"
        Me.ResumeLayout(False)
    End Sub

End Class

' Provides an example control that displays instructions in design mode,
' with which the example UITypeEditor is associated.
Public Class WinFormsEdServiceDialogExampleControl
    Inherits UserControl

    <EditorAttribute(GetType(TestDialogEditor), GetType(UITypeEditor))> _
    Public Property TestDialogString() As String
        Get
            Return localDialogTestString
        End Get
        Set(ByVal Value As String)
            localDialogTestString = Value
        End Set
    End Property
    Private localDialogTestString As String

    Public Sub New()
        localDialogTestString = "Test String"
        Me.Size = New Size(210, 74)
        Me.BackColor = Color.Beige
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If Me.DesignMode Then
            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)
        End If
    End Sub

End Class

下列範例提供的範例 UITypeEditor 會使用 IWindowsFormsEditorService 來顯示使用者輸入的下拉式記錄 UserControl

VB
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

' Example UITypeEditor that uses the IWindowsFormsEditorService to 
' display a drop-down control.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class TestDropDownEditor
   Inherits System.Drawing.Design.UITypeEditor
   
   Public Sub New()
    End Sub
   
    Public Overloads Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle
        ' Indicates that this editor can display a control-based 
        ' drop-down interface.
        Return UITypeEditorEditStyle.DropDown
    End Function
    
    Public Overloads Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object

        ' Attempts to obtain an IWindowsFormsEditorService.
        Dim edSvc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
        If edSvc Is Nothing Then
            Return value
        End If

        ' Displays a drop-down control.
        Dim inputControl As New StringInputControl(CStr(value), edSvc)
        edSvc.DropDownControl(inputControl)
        Return inputControl.inputTextBox.Text
    End Function

End Class

' Example control for entering a string.
Friend Class StringInputControl
    Inherits System.Windows.Forms.UserControl

    Public inputTextBox As System.Windows.Forms.TextBox
    Private WithEvents ok_button As System.Windows.Forms.Button
    Private WithEvents cancel_button As System.Windows.Forms.Button
    Private edSvc As IWindowsFormsEditorService

    Public Sub New(ByVal [text] As String, ByVal edSvc As IWindowsFormsEditorService)
        InitializeComponent()
        inputTextBox.Text = [text]
        ' Stores IWindowsFormsEditorService reference to use to 
        ' close the control.
        Me.edSvc = edSvc
    End Sub

    Private Sub InitializeComponent()
        Me.inputTextBox = New System.Windows.Forms.TextBox()
        Me.ok_button = New System.Windows.Forms.Button()
        Me.cancel_button = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        Me.inputTextBox.Anchor = System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right
        Me.inputTextBox.Location = New System.Drawing.Point(6, 7)
        Me.inputTextBox.Name = "inputTextBox"
        Me.inputTextBox.Size = New System.Drawing.Size(336, 20)
        Me.inputTextBox.TabIndex = 0
        Me.inputTextBox.Text = ""
        Me.ok_button.Anchor = System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right
        Me.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.ok_button.Location = New System.Drawing.Point(186, 38)
        Me.ok_button.Name = "ok_button"
        Me.ok_button.TabIndex = 1
        Me.ok_button.Text = "OK"
        Me.cancel_button.Anchor = System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right
        Me.cancel_button.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.cancel_button.Location = New System.Drawing.Point(267, 38)
        Me.cancel_button.Name = "cancel_button"
        Me.cancel_button.TabIndex = 2
        Me.cancel_button.Text = "Cancel"
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cancel_button, Me.ok_button, Me.inputTextBox})
        Me.Name = "StringInputControl"
        Me.Size = New System.Drawing.Size(350, 70)
        Me.ResumeLayout(False)
    End Sub

    Private Sub CloseControl(ByVal sender As Object, ByVal e As EventArgs) Handles ok_button.Click, cancel_button.Click
        edSvc.CloseDropDown()
    End Sub

End Class

' Provides an example control that displays instructions in design mode,
' with which the example UITypeEditor is associated.
Public Class WinFormsEdServiceDropDownExampleControl
    Inherits UserControl

    <EditorAttribute(GetType(TestDropDownEditor), GetType(UITypeEditor))> _
    Public Property TestDropDownString() As String
        Get
            Return localDropDownTestString
        End Get
        Set(ByVal Value As String)
            localDropDownTestString = Value
        End Set
    End Property
    Private localDropDownTestString As String

    Public Sub New()
        localDropDownTestString = "Test String"
        Me.Size = New Size(210, 74)
        Me.BackColor = Color.Beige
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If Me.DesignMode Then
            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)
        End If
    End Sub

End Class

備註

IWindowsFormsEditorService只有透過控制項的方法才能使用 GetService PropertyGrid

這項服務通常用來從的方法顯示表單 EditValue UITypeEditor 當叫用的 PropertyGrid EditValue 方法來提供用來 UITypeEditor 編輯屬性值的使用者介面時,會將 EditValue 參考傳遞給 IServiceProvider ,通常可以提供的實例 IWindowsFormsEditorService 這項服務的方法可以用來顯示對話方塊和表單,或 Control 在下拉式方塊中顯示,該容器會顯示在屬性方格的頂端,而該值欄位的區域接近其值正在編輯的範圍。

方法

方法
CloseDropDown()

關閉先前開啟的任何下拉控制項區域。

DropDownControl(Control)

在提供這個服務之屬性方格值欄位下方的下拉區域中,顯示指定的控制項。

ShowDialog(Form)

顯示指定的 Form

適用於

資料表 2
產品 推出的版本
.NET 5.0
.NET Core 3.0
.NET Framework 1.1

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多