Which button was clicked to open form

不打扰是莪最后的温柔 提交于 2019-12-20 05:24:25

问题


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?

Thanks


回答1:


Well suppose to define at the global level an enum like this

Public Enum CommandAction
    Create
    Modify 
    Delete
End Enum

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()

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

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.




回答2:


first put a hidden field with some name like "clicked". Make a javascript function with one parameter to put values in to the hidden field. Call the function on click of every button but set some specific value for each button. In this way you will be able to run right code for each button on submit. If not understand then do tell me, i'll help you in the code.



来源:https://stackoverflow.com/questions/25892602/which-button-was-clicked-to-open-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!