我有一个菜单条,可以添加/删除按钮。我希望能够得到每个按钮的高度,并将它们相加。这可能吗?
我使用了以下示例代码作为指南: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,然后在括号中提供ToolStripItems的高度。消息框的底部包含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