0

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

4

1 回答 1

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
于 2013-06-29T00:11:56.253 回答