Baseline snaplines in custom Winforms controls

后端 未结 5 1633
余生分开走
余生分开走 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:36

    You're on the right track. You will need to override the SnapLines property in your designr and do something like this:

    Public Overrides ReadOnly Property SnapLines() As System.Collections.IList
        Get
            Dim snapLinesList As ArrayList = TryCast(MyBase.SnapLines, ArrayList)
    
            Dim offset As Integer
            Dim ctrl As MyControl = TryCast(Me.Control, MyControl)
            If ctrl IsNot Nothing AndAlso ctrl.TextBox1 IsNot Nothing Then
                offset = ctrl.TextBox1.Bottom - 5
            End If
    
            snapLinesList.Add(New SnapLine(SnapLineType.Baseline, offset, SnapLinePriority.Medium))
    
            Return snapLinesList
    
        End Get
    End Property
    

    In this example the usercontrol contains a textbox. The code adds a new snapline that represents the baseline for the textbox. The important thing is to calculate the offset correctly.

提交回复
热议问题