Toolstripmenu添加项目

问题描述:

我知道如何将一个项目(复选框)动态添加到工具条,但是我想添加一个存在于表单中的复选框。我使用的代码Toolstripmenu添加项目

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

试过但是这使得现有的复选框,转到屏幕的左上角时,工具条是点击它出现。所以我想将复选框添加到菜单中,而不是去左上角,有什么想法?

+0

通常这是通过一个`ToolStripMenuItem`来完成的,它设置了`CheckOnClick = true`。这不符合你的需求吗? – 2011-01-07 23:37:43

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 

我喜欢显然是第一个。