當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET ControlPersister類代碼示例

本文整理匯總了VB.NET中System.Web.UI.Design.ControlPersister的典型用法代碼示例。如果您正苦於以下問題:VB.NET ControlPersister類的具體用法?VB.NET ControlPersister怎麽用?VB.NET ControlPersister使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ControlPersister類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: New

' 導入命名空間
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Namespace ControlParserPersisterDesignerControl_VB

    ' Web control designer provides an interface 
    '   to use the ControlPersister and ControlParser.
    ' The DesignerActionListCollection must run under "FullTrust",
    '   so you must also require your designer to run under "FullTrust".
    <System.Security.Permissions.PermissionSetAttribute( _
        System.Security.Permissions.SecurityAction.InheritanceDemand, _
        Name:="FullTrust")> _
    <System.Security.Permissions.PermissionSetAttribute( _
         Security.Permissions.SecurityAction.Demand, _
         Name:="FullTrust")> _
    Public Class ControlParserPersisterDesigner
        Inherits System.Web.UI.Design.ControlDesigner

        Private _actLists As DesignerActionListCollection

        Public Sub New()
        End Sub

        ' Creates designer menu commands to persist 
        ' a control and to load a persisted control.
        Public Overrides ReadOnly Property ActionLists() As System.ComponentModel.Design.DesignerActionListCollection
            Get
                If IsNothing(_actLists) Then
                    _actLists = New DesignerActionListCollection()
                    _actLists.AddRange(MyBase.ActionLists)
                    _actLists.Add(New ParserActionList(Me))
                End If

                Return _actLists
            End Get
        End Property
        'Public Overrides ReadOnly Property Verbs() _
        'As System.ComponentModel.Design.DesignerVerbCollection
        '    Get
        '        Dim dvc As New DesignerVerbCollection()
        '        dvc.Add(New DesignerVerb("Load Persisted Control...", _
        '            New EventHandler(AddressOf Me.loadPersistedControl)))
        '        dvc.Add(New DesignerVerb("Parse and Save Control...", _
        '            New EventHandler(AddressOf Me.saveControl)))
        '        Return dvc
        '    End Get
        'End Property

        ' Displays a textbox form to receive an HTML 
        ' string that represents a control, and creates
        ' a toolbox item for the control, if not already
        ' present in the selected toolbox category.

        Private Class ParserActionList
            Inherits DesignerActionList

            Private _parent As ControlParserPersisterDesigner
            Private _items As DesignerActionItemCollection

            Public Sub New(ByVal parent As ControlParserPersisterDesigner)
                MyBase.New(parent.Component)
                _parent = parent
            End Sub

            Public Overrides Function GetSortedActionItems() As System.ComponentModel.Design.DesignerActionItemCollection
                If IsNothing(_items) Then
                    _items = New DesignerActionItemCollection()
                    _items.Add(New DesignerActionMethodItem(Me, "saveControl", "Parse and Save Control...", True))
                    _items.Add(New DesignerActionMethodItem(Me, "loadPersistedControl", "Load Persisted Control...", True))
                End If
                Return _items
            End Function

            ' Displays a list of controls in the project, if any,
            ' and displays the HTML markup for the selected control.       
            Private Sub saveControl()
                ' Get the collection of components in the current 
                ' design mode document.
                Dim documentComponents As ComponentCollection = _
                    Me.Component.Site.Container.Components

                ' Filter the components to those that derive from the 
                ' System.Web.UI.Control class.

                ' Create an IComponent array of the maximum possible length needed.
                Dim filterArray(documentComponents.Count) As IComponent

                ' Count the total qualifying components.
                Dim total As Integer = 0
                Dim i As Integer
                For i = 0 To documentComponents.Count - 1
                    ' If the component derives from System.Web.UI.Control, 
                    ' copy a reference to the control to the filterArray 
                    ' array and increment the control count.
                    If GetType(System.Web.UI.Control).IsAssignableFrom(CType(documentComponents(i), Object).GetType()) Then
                        If Not (CType(documentComponents(i), System.Web.UI.Control).UniqueID Is Nothing) Then
                            filterArray(total) = documentComponents(i)
                            total += 1
                        End If
                    End If
                Next i

                If total = 0 Then
                    Throw New Exception( _
                        "Document contains no System.Web.UI.Control components.")
                End If

                ' Move the components that derive from System.Web.UI.Control to a 
                ' new array of the correct size.
                Dim controlArray(total - 1) As System.Web.UI.Control

                For i = 0 To total - 1
                    controlArray(i) = CType(filterArray(i), System.Web.UI.Control)
                Next i

                ' Create a ControlSelectionForm to provide a persistence 
                ' configuration interface.
                Dim selectionForm As New ControlSelectionForm(controlArray)

                ' Display the form.
                If selectionForm.ShowDialog() <> DialogResult.OK Then
                    Return
                End If

                ' Validate the selection.
                If selectionForm.controlList.SelectedIndex = -1 Then
                    Throw New Exception("You must select a control to persist.")
                End If

                ' Parse the selected control.
                Dim persistedData As String = ControlPersister.PersistControl( _
                    controlArray(selectionForm.controlList.SelectedIndex))

                ' Display the control persistence string in a text box.
                Dim displayForm As New StringDisplayForm(persistedData)
                displayForm.ShowDialog()
            End Sub

            ' Displays a textbox form to receive an HTML 
            ' string that represents a control, and creates
            ' a toolbox item for the control, if not already
            ' present in the selected toolbox category.
            Private Sub loadPersistedControl()
                ' Display a StringInputForm to obtain a persisted control string.
                Dim inputForm As New StringInputForm()

                If inputForm.ShowDialog() <> DialogResult.OK Then
                    Return
                End If
                If inputForm.TxBox.Text.Length < 2 Then
                    Return
                End If
                ' Obtain an IDesignerHost for the design-mode document.
                Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)

                ' Create a Web control from the persisted control string.
                Dim ctrl As System.Web.UI.Control = ControlParser.ParseControl(host, inputForm.TxBox.Text.Trim())

                ' Create a Web control toolbox item for the type of the control.
                Dim item As New System.Web.UI.Design.WebControlToolboxItem(ctrl.GetType())

                ' Add the Web control toolbox item to the toolbox.
                Dim toolService As IToolboxService = CType(Me.Component.Site.GetService(GetType(IToolboxService)), IToolboxService)
                If Not (toolService Is Nothing) Then
                    toolService.AddToolboxItem(item)
                Else
                    Throw New Exception("IToolboxService was not found.")
                End If
            End Sub
        End Class
    End Class

    ' Simple text display control hosts the ControlParserPersisterDesigner.
    <DesignerAttribute(GetType(ControlParserPersisterDesigner), _
        GetType(IDesigner))> _
    Public Class ControlParserPersisterDesignerControl
        Inherits System.Web.UI.WebControls.WebControl

        Private _text As String

        <Bindable(True), _
            Category("Appearance"), _
            DefaultValue("")> _
        Public Property [Text]() As String
            Get
                Return _text
            End Get
            Set(ByVal Value As String)
                _text = Value
            End Set
        End Property

        Public Sub New()
            [Text] = "Right-click here to access control persistence " & _
                        "methods in design mode"
        End Sub

        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            output.Write([Text])
        End Sub
    End Class

    ' Provides a form with a list for selection.
    Friend Class ControlSelectionForm
        Inherits System.Windows.Forms.Form
        Private controlArray() As System.Web.UI.Control
        Public controlList As System.Windows.Forms.ListBox

        Public Sub New(ByVal controlArray() As System.Web.UI.Control)
            Me.controlArray = controlArray

            Me.Size = New Size(400, 500)
            Me.Text = "Control Persister Selection Form"

            Me.SuspendLayout()

            Dim label1 As New System.Windows.Forms.Label()
            label1.Text = "Select the control to persist:"
            label1.Size = New Size(240, 20)
            label1.Location = New Point(10, 10)
            Me.Controls.Add(label1)

            controlList = New System.Windows.Forms.ListBox()
            controlList.Size = New Size(370, 400)
            controlList.Location = New Point(10, 30)
            controlList.Anchor = AnchorStyles.Left Or AnchorStyles.Top _
                    Or AnchorStyles.Bottom Or AnchorStyles.Right
            Me.Controls.Add(controlList)

            Dim okButton As New System.Windows.Forms.Button()
            okButton.Text = "OK"
            okButton.Size = New Size(80, 24)
            okButton.Location = New Point(Me.Width - 100, Me.Height - 60)
            okButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
            okButton.DialogResult = DialogResult.OK
            Me.Controls.Add(okButton)

            Dim cancelButton As New System.Windows.Forms.Button()
            cancelButton.Text = "Cancel"
            cancelButton.Size = New Size(80, 24)
            cancelButton.Location = New Point(Me.Width - 200, Me.Height - 60)
            cancelButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
            cancelButton.DialogResult = DialogResult.Cancel
            Me.Controls.Add(cancelButton)

            Dim i As Integer
            For i = 0 To controlArray.Length - 1
                controlList.Items.Add(controlArray(i).UniqueID)
            Next i
            Me.ResumeLayout()
        End Sub
    End Class

    ' Provides a form with a multiline text box display.
    Friend Class StringDisplayForm
        Inherits System.Windows.Forms.Form

        Private tbox As New System.Windows.Forms.TextBox()

        Public Property TxBox() As System.Windows.Forms.TextBox
            Get
                Return tbox
            End Get
            Set(ByVal value As System.Windows.Forms.TextBox)
                tbox = value
            End Set
        End Property

        Public Sub New(ByVal displayText As String)
            Me.Size = New Size(400, 300)
            Me.Text = "Control Persistence String"

            Me.SuspendLayout()
            Dim tbox As New System.Windows.Forms.TextBox()
            tbox.Multiline = True
            tbox.Size = New Size(Me.Width - 40, Me.Height - 90)
            tbox.Text = displayText
            Me.Controls.Add(tbox)

            Dim okButton As New System.Windows.Forms.Button()
            okButton.Text = "OK"
            okButton.Size = New Size(80, 24)
            okButton.Location = New Point(Me.Width - 100, Me.Height - 60)
            okButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
            okButton.DialogResult = DialogResult.OK
            Me.Controls.Add(okButton)

            Me.ResumeLayout()
        End Sub
    End Class

    ' Provides a form with a multiline text box for input.
    Friend Class StringInputForm
        Inherits System.Windows.Forms.Form
        Private tbox As New System.Windows.Forms.TextBox()

        Public Property TxBox() As System.Windows.Forms.TextBox
            Get
                Return tbox
            End Get
            Set(ByVal value As System.Windows.Forms.TextBox)
                tbox = value
            End Set
        End Property

        Public Sub New()
            Me.Size = New Size(400, 400)
            Me.Text = "Input Control Persistence String"

            Me.SuspendLayout()
            tbox = New System.Windows.Forms.TextBox()
            tbox.Multiline = True
            tbox.Size = New Size(Me.Width - 40, Me.Height - 90)
            tbox.Text = ""
            Me.Controls.Add(tbox)

            Dim okButton As New System.Windows.Forms.Button()
            okButton.Text = "OK"
            okButton.Size = New Size(80, 24)
            okButton.Location = New Point(Me.Width - 100, Me.Height - 60)
            okButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
            okButton.DialogResult = DialogResult.OK
            Me.Controls.Add(okButton)

            Dim cancelButton As New System.Windows.Forms.Button()
            cancelButton.Text = "Cancel"
            cancelButton.Size = New Size(80, 24)
            cancelButton.Location = New Point(Me.Width - 200, Me.Height - 60)
            cancelButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
            cancelButton.DialogResult = DialogResult.Cancel
            Me.Controls.Add(cancelButton)

            Me.ResumeLayout()
        End Sub
    End Class
End Namespace
開發者ID:VB.NET開發者,項目名稱:System.Web.UI.Design,代碼行數:344,代碼來源:ControlPersister


注:本文中的System.Web.UI.Design.ControlPersister類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。