我在label中动态加入字符串,我想计算字符串的长度
来动态改变label的长度,不知怎么计算.
我的label是"宋体",常规,10号字,

解决方案 »

  1.   

    把 Label 的 AutoSize 直接设成 True 不就行了吗?
      

  2.   

    是呀,何必那么费事!
    把 Label 的 AutoSize 直接设成 True 
      

  3.   

    Label控件没有提供textwidth函数,想使用textwidth可用一个picturebox作为载体:
    Private Sub Command1_Click()
        Dim temppic As PictureBox
        Set temppic = Controls.Add("vb.picturebox", "temppic")
        temppic.Font = Me.Label1.Font
        Dim i As Long
        Dim s As String
        s = "123456abcqqqqqqqqq"
        i = temppic.TextWidth(s)
        Me.Label1.Caption = s
        Me.Label1.Width = i    
        Controls.Remove temppic
    End Sub
    推荐把Label的 AutoSize直接设成True