写一个小程序, 用VB6随意做下界面,离奇的发现弹出菜单居然没有效果,过程如下
创建一个窗体Form1,用菜单编辑器创建一个弹出菜单
弹出菜单子菜单项在这里名称为mPopup1,事件代码为:
Option Explicit Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) PopupMenu mnuPopup1 End Sub Private Sub mPopup1_Click() Dim f As New Form2 f.Show vbModal End Sub
Option Explicit Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) PopupMenu mnuPopup2 End Sub
查了下MSDN说明有一句:
在显示弹出式菜单时,调用 PopupMenu 方法后面的代码直到用户或者从菜单中选择了命令(这时,该命令的 Click 事件的代码比 PopupMenu 语句后面的代码先执行)或者取消该菜单时才能执行。此外,每次只能显示一个弹出式菜单,因此,如果已经显示了一个弹出式菜单或打开了一个下拉式菜单时,该方法的其它调用将被忽略。
实践证明,PopupMenu后面的代码是继续执行的,但是多次弹出菜单无效,即弹出一个菜单后的对话框不能再继续弹出对话框
解决的办法也出来了: 不要以模态显示对话框即可.
调整后Form1的代码为:
Option Explicit Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) PopupMenu mnuPopup1 End Sub Private Sub mPopup1_Click() Dim f As New Form2 'f.Show vbModal f.Show End Sub
这样mPopup1_Click()响应过程能够返回, 再调用PopupMenu也就能弹出菜单了,当然了还有其他的解决办法,比如:
Option Explicit Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) PopupMenu mnuPopup1 End Sub Private Sub mPopup1_Click() 'Dim f As New Form2 ''f.Show vbModal 'f.Show Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Dim f As New Form2 f.Show vbModal End Sub
应该就这么多了