0

我希望能够动态地创建一个上面带有标签的面板,但是标签的作用也不如我所料,它切断了大部分。

当我创建一个面板然后在面板内创建一个标签时,文本显示不正确。谁知道怎么修它?

应该做的是使用 newpanel() 子创建一个带有文本的面板

Dim timetable(5, 5) As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.Width = (74 * 5) - 3
    Me.Height = My.Computer.Screen.Bounds.Size.Height
    Me.Top = My.Computer.Screen.Bounds.Top
    Me.Left = My.Computer.Screen.Bounds.Right - Me.Width
    GetTimetable()
End Sub





Private Sub newpanel(colour As Color, textT As String)
    Dim Npan As New Panel
    Npan.Top = 0
    Npan.Left = 0
    Npan.Width = Me.Width
    Npan.Height = 64
    Npan.BackColor = colour
    Dim NpanT As New Label
    NpanT.Parent = Npan

    NpanT.Text = textT

    Npan.Controls.Add(NpanT)

    Me.Controls.Add(Npan)


End Sub

Private Sub GetTimetable()
    'Dim path As String = My.Computer.FileSystem.SpecialDirectories.Desktop + "\Timetable"
    newpanel(Color.Aqua, "this is a test! test testtesttest test test test")
End Sub
4

1 回答 1

0

查看该Label.AutoSize属性的 MSDN 页面,如上所述,标签 AutoSize 属性在设计器中默认为 true,在代码中创建时为false 。

从上面的链接:

当使用设计器添加到表单时,默认值为 true。从代码实例化时,默认值为 false。

因此,您需要将newpanel方法更改为:

Private Sub newpanel(colour As Color, textT As String)
    Dim Npan As New Panel
    Npan.Top = 0
    Npan.Left = 0
    Npan.Width = Me.Width
    Npan.Height = 64
    Npan.BackColor = colour
    Dim NpanT As New Label
    NpanT.Parent = Npan

    NpanT.Text = textT
    NpanT.AutoSize = True  'Enables Auto sizing
    Npan.Controls.Add(NpanT)
    Me.Controls.Add(Npan)
End Sub
于 2013-10-14T03:21:03.893 回答