问题描述
我有一个表单,可以通过单击三个按钮(添加,修改或删除)中的任何一个来加载.加载表单后,会出现一个确认"按钮,该按钮将根据最初用于显示表单的按钮来执行任务.
I have a form that loads from a click of any of three buttons (Add, Modify or Delete). When the form loads there is a 'Confirm' button which will perform a task depending on which button was originally used to show the form.
是否有一种简单的方法来确定最初单击了哪个按钮,以便可以执行正确的代码?
Is there an easy way to determine which button was originally clicked so that the right code can be executed?
谢谢
推荐答案
应该在全局级别定义这样的枚举
Well suppose to define at the global level an enum like this
Public Enum CommandAction
Create
Modify
Delete
End Enum
现在在用于启动第二个窗体以执行Add命令的代码中,您可以编写这样的代码(当然,您重复相同的操作,但是在其他按钮中更改了CommandAction).
Now in the code used to launch your second form to execute the Add command, you could write code like this (of course you repeat the same but varying the CommandAction in the other buttons).
Dim myFormInstance = new MyForm(CommandAction.Create)
myFormInstance.ShowDialog()
最后,为您的第二种形式添加特殊的构造函数(在本示例中为MyForm
).构造函数,该构造函数接收并保存CommandAction以供将来使用
Finally, add a specfic constructor for your second form (MyForm
in this example).A constructor that receives and saves for future usage a CommandAction
Public Class MyForm
Dim cmd as CommandAction
Public Sub New(command as CommandAction )
InitializeComponent()
cmd = command
End Sub
Public Sub New()
InitializeComponent()
cmd = CommandAction.Create ' Set a default'
End Sub
End Class
现在在代码中,您需要确定要执行哪种操作,只需查看全局cmd
变量的值并执行相应的代码块
Now in the code where you need to decide which kind of action to execute, just look at the value of the global cmd
variable and execute the appropriate block of code
注意:向表单类添加特定的构造函数需要标准空构造函数的显式存在.
NOTE Adding a specific constructor to a form class requires the explicit presence of the standard empty constructor.
这篇关于单击哪个按钮打开表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!