Toolstripmenu添加项目

时间:2011-01-07 21:41:14

标签: .net vb.net toolstrip

我知道如何动态地向工具条添加项目(复选框),但我想添加一个表单中存在的复选框。我尝试过使用代码

Dim chkboxhost As ToolStripControlHost
chkboxhost = New ToolStripControlHost(CheckBox1)
toolStrip1.Items.Add(chkboxhost)

但是这会生成已存在的复选框,转到屏幕的左上角,当工具条点击时,它会出现。所以我想在菜单中添加复选框,而不是左上角,任何想法?

1 个答案:

答案 0 :(得分:0)

BlueRaja的回答是答案,你可以通过多种方式做到,这里有两个:

首先:

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
    ToolStripButton2.Checked = ToolStripButton1.Checked
    'Do whatever you want with your buttons
End Sub

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
    ToolStripButton1.Checked = ToolStripButton2.Checked
    'Do whatever you want with your buttons
End Sub

另一种方法:

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
    'Do whatever you want with your buttons
End Sub

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
    'Do whatever you want with your buttons
End Sub

Private Sub ToolStripButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.CheckedChanged
    ToolStripButton2.Checked = ToolStripButton1.Checked
End Sub  

Private Sub ToolStripButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.CheckedChanged
    ToolStripButton1.Checked = ToolStripButton2.Checked
End Sub

我显然更喜欢第一个。