Baseline snaplines in custom Winforms controls

后端 未结 5 1624
余生分开走
余生分开走 2020-12-08 07:19

I have a custom user control with a textbox on it and I\'d like to expose the baseline (of the text in the textbox) snapline outside of the custom control. I know that you

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 07:32

    VB.Net Version:
    Note: you have to change the txtDescription to the Textbox or another internal control name that you use. and ctlUserControl to your usercontrol name

     _
    Partial Public Class ctlUserControl
       '... 
       'Your Usercontrol class specific code
       '... 
        Class MyCustomDesigner
            Inherits ControlDesigner
            Public Overloads Overrides ReadOnly Property SnapLines() As IList
                Get
                    ' Code from above 
    
                    Dim lines As IList = MyBase.SnapLines
    
                    ' *** This will need to be modified to match your user control
                    Dim control__1 As ctlUserControl = TryCast(Me.Control, ctlUserControl)
                    If control__1 Is Nothing Then Return lines
    
                    ' *** This will need to be modified to match the item in your user control
                    ' This is the control in your UC that you want SnapLines for the entire UC
                    Dim designer As IDesigner = TypeDescriptor.CreateDesigner(control__1.txtDescription, GetType(IDesigner))
                    If designer Is Nothing Then
                        Return lines
                    End If
    
                    ' *** This will need to be modified to match the item in your user control
                    designer.Initialize(control__1.txtDescription)
    
                    Using designer
                        Dim boxDesigner As ControlDesigner = TryCast(designer, ControlDesigner)
                        If boxDesigner Is Nothing Then
                            Return lines
                        End If
    
                        For Each line As SnapLine In boxDesigner.SnapLines
                            If line.SnapLineType = SnapLineType.Baseline Then
                                ' *** This will need to be modified to match the item in your user control
                                lines.Add(New SnapLine(SnapLineType.Baseline, line.Offset + control__1.txtDescription.Top, line.Filter, line.Priority))
                                Exit For
                            End If
                        Next
                    End Using
    
                    Return lines
                End Get
            End Property
        End Class
    
    End Class
    

提交回复
热议问题