如何在Visual Basic中创建LinearLayout(visual studio 2013)

时间:2014-01-02 18:18:36

标签: vb.net visual-studio visual-studio-2012 visual-studio-2013

(这与VB6无关)

enter image description here

如上所述,我想在分隔的“标签”控件之间进行“文本框”控制。

我使用方法加载这三个控件(两个标签和一个文本框),但我无法精确定位文本框和以下标签。

在应用程序中,将有超过数百个句子具有几乎相同数量的空白(文本框),因此需要完美的衬里。

那么,有什么好的方法可以做到吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以创建一个继承自Panel的类,并包含两个标签和一个自动重新对齐的文本框。我做了类似的事情让你开始。只需将代码粘贴到项目中,重新编译一次,然后就可以从工具箱中将这些代码添加到表单中(或者当然可以动态创建它们)。这是无处可去的。你应该根据你的需要为字体制作属性和其他事件,但是你想到了我的想法。

Public Class ctrlBlankSentence
    Inherits Panel
    Private WithEvents lblLeft As Label 'The left part of the sentence
    Private WithEvents tbBlank As TextBox 'The blank text
    Private WithEvents lblRight As Label 'The right part of the sentence
    Public Event BlankTextChanged() 'Use events to react to user input
    Private _Spacing As Integer
    ''' <summary>
    ''' Determines the distance between the labels and the textbox
    ''' </summary>
    Public Property Spacing As Integer
        Get
            Return _Spacing
        End Get
        Set(value As Integer)
            _Spacing = value
            DoreAlign()
        End Set
    End Property
    Private _TextboxWidth As Integer
    ''' <summary>
    ''' Determines the width of the textbox
    ''' </summary>
    Public Property TextboxWidth As Integer
        Get
            Return _TextboxWidth
        End Get
        Set(value As Integer)
            _TextboxWidth = value
            DoreAlign()
        End Set
    End Property
    Private _AutosizeControl As Boolean
    ''' <summary>
    ''' Determines if the panel is automatically resized.
    ''' </summary>
    Public Property AutosizeControl As Boolean
        Get
            Return _AutosizeControl
        End Get
        Set(value As Boolean)
            _AutosizeControl = value
            DoreAlign()
        End Set
    End Property
    ''' <summary>
    ''' Used to get or set the left part of the sentence
    ''' </summary>
    Public Property LeftText As String
        Get
            Return lblLeft.Text
        End Get
        Set(value As String)
            lblLeft.Text = value
            DoreAlign()
        End Set
    End Property
    ''' <summary>
    ''' Used to set the right part of the sentence
    ''' </summary>
    Public Property RightText As String
        Get
            Return lblRight.Text
        End Get
        Set(value As String)
            lblRight.Text = value
            DoreAlign()
        End Set
    End Property

    ''' <summary>
    ''' Used to get or set the user input
    ''' </summary>
    Public Property BlankText As String
        Get
            Return tbBlank.Text
        End Get
        Set(value As String)
            tbBlank.Text = value
            RaiseEvent BlankTextChanged()
        End Set
    End Property

#Region "Constructor"
    Public Sub New()
        lblLeft = New Label With {.AutoSize = True, .Text = "Left Text"}
        lblRight = New Label With {.AutoSize = True, .Text = "Right Text"}
        tbBlank = New TextBox
        Spacing = 3
        TextboxWidth = 100
        AutosizeControl = True
        Me.Controls.Add(lblLeft)
        Me.Controls.Add(lblRight)
        Me.Controls.Add(tbBlank)
        DoreAlign()
    End Sub
#End Region
#Region "The method the realigns the controls"
    Private Sub DoreAlign()
        lblLeft.Left = 0
        tbBlank.Left = lblLeft.Right + Spacing
        tbBlank.Width = TextboxWidth
        lblRight.Left = tbBlank.Right + Spacing
        If AutosizeControl Then
            Me.Width = lblRight.Right
            Me.Height = tbBlank.Height
        End If
        lblRight.Top = CInt(Me.Height / 2 - lblRight.Height / 2)
        lblLeft.Top = lblRight.Top
    End Sub
#End Region

    Private Sub thisTextChanged() Handles tbBlank.TextChanged
        RaiseEvent BlankTextChanged()
    End Sub
End Class