如何在VB.Net中的MenuStrip中获取各个按钮的高度

时间:2013-06-28 21:19:38

标签: vb.net height menustrip

我有一个菜单条,可以添加/删除按钮。我希望能够获得每个按钮的高度,并将它们添加起来。这可能吗?

1 个答案:

答案 0 :(得分:0)

我使用此示例代码作为指南:http://social.msdn.microsoft.com/Forums/vstudio/en-US/1a7f655b-fd14-4af9-b843-28432d8ce7fb/iterate-through-all-menu-options-in-a-menustrip

消息框为MenuStrip1中的每个ToolStripItem提供Text,后跟括号中ToolStripItem的高度。消息框的底部包含MenuStrip1中所有控件的总高度。

这是你在找什么?

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim total As Integer
    Dim menues As New List(Of ToolStripItem)

    For Each t As ToolStripItem In MenuStrip1.Items
        GetMenues(t, menues)
    Next

    Dim msg As New StringBuilder
    For Each t As ToolStripItem In menues
        msg.Append(t.Text)
        msg.Append(" (")
        msg.Append(t.Height)
        msg.AppendLine(")")

        total += t.Height
    Next
    msg.AppendLine("")
    msg.AppendLine("")
    msg.Append("Total Height: ")
    msg.Append(total)

    MessageBox.Show(msg.ToString)
End Sub

Public Sub GetMenues(ByVal Current As ToolStripItem, ByRef menues As List(Of ToolStripItem))
    menues.Add(Current)
    If TypeOf (Current) Is ToolStripMenuItem Then
        For Each menu As ToolStripItem In DirectCast(Current, ToolStripMenuItem).DropDownItems
            GetMenues(menu, menues)
        Next
    End If
End Sub