如何将Ellipsis添加到MenuStrip项文本

时间:2014-11-24 13:24:42

标签: vb.net text menuitem ellipsis menustrip

使用MenuStrip时,最好让项目和子项具有一定的宽度,宽度不应该改变。假设宽度确实变为很大的长度,而不是改变MenuStrip项的整个大小以适应文本,那么应该只有一种方式来显示一些文本,例如省略号。

之前:

enter sdescription here

After

显然,我编辑了这个MenuStrip MenuItem的文本,以证明我的意思。这可能吗?如果是这样我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

Public Class Form1

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim strPrograms(1, 4) '(0,*) = EXE name; (1,*) = screen name; (*,n) = Program Index

    strPrograms(0, 0) = "ONE.EXE" : strPrograms(1, 0) = "First"
    strPrograms(0, 1) = "TWO.EXE" : strPrograms(1, 1) = "Second"
    strPrograms(0, 2) = "THREE.EXE" : strPrograms(1, 2) = "Third program description is too long"
    strPrograms(0, 3) = "FOUR.EXE" : strPrograms(1, 3) = "Fourth"
    strPrograms(0, 4) = "FIVE.EXE" : strPrograms(1, 4) = "Fifth"

    Dim intMaxLength As Integer = 10
    Dim tsmParent As ToolStripMenuItem = MenuStrip1.Items.Add("Programs")
    For i As Integer = 0 To strPrograms.GetUpperBound(1)
      Dim strScreenName As String = strPrograms(1, i)
      If strScreenName.Length > intMaxLength Then
        strScreenName = strScreenName.Substring(0, intMaxLength - 1) & "…"
      End If
      Dim tsm As New ToolStripMenuItem(strScreenName)
      tsm.Tag = strPrograms(0, i)
      AddHandler tsm.Click, AddressOf tsm_Click
      tsmParent.DropDownItems.Add(tsm)
    Next i

  End Sub

  Private Sub tsm_Click(sender As Object, e As EventArgs)
    Dim strEXEName As String = DirectCast(sender, ToolStripMenuItem).Tag
    MsgBox(strEXEName)

  End Sub
End Class