当表单状态更改时,如何在不使用 Select Case 语句的情况下处理启用/禁用工具条按钮?
我们都知道,当表单状态发生变化时,可以使用 Select Case
语句 (VB.NET) 或 C# 中的 switch
case 语句来处理启用/禁用 ToolStrip 按钮。
但我记得我的老师说过“当你使用 OOP 开发软件时,使用这些语句并不是正确的方法”。
Private Sub SetToolStripButtons()
Select Case formState
Case FormStates.Normal
btnSave.Enabled = False
btnCancel.Enabled = False
btnNew.Enabled = True
btnEdit.Enabled = True
Case FormStates.Edit
btnSave.Enabled = True
btnCancel.Enabled = True
btnNew.Enabled = False
btnEdit.Enabled = False
'.....
'.....
End Select
End Sub
编辑:我在上面放了一个简单的代码片段
那么你会推荐什么?
We all know that it's possible to handle enabling/disabling ToolStrip buttons when a form state changes by using Select Case
statements (VB.NET) or switch
case statement in C#.
But I remember my teacher said "using these statements is not a correct way when you are developing a software using OOP".
Private Sub SetToolStripButtons()
Select Case formState
Case FormStates.Normal
btnSave.Enabled = False
btnCancel.Enabled = False
btnNew.Enabled = True
btnEdit.Enabled = True
Case FormStates.Edit
btnSave.Enabled = True
btnCancel.Enabled = True
btnNew.Enabled = False
btnEdit.Enabled = False
'.....
'.....
End Select
End Sub
EDIT: I put a simple code snippet above
So what would you recommend instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,代码看起来是有效的。我认为使用 Select Case 没有问题。
但是,如果您只有两种状态(正常和编辑),您可以像这样改进代码:
Well, the code looks like valid. I see no problem in using Select Case.
However, if you have only two states (Normal and Edit), you can improve your code like this:
您看过状态设计模式吗?
您将有一个抽象类来表示状态
FormState
,其中包含一个虚拟SetToolStripButtons()
方法:然后该类的每个派生实现负责了解如何设置每个按钮的可见性(对于给定状态)。例如,
FormStateNormal
类将重写SetToolsStripButtons
以包含类似以下内容:它的含义远不止这些,但希望这应该是一个很好的起点。
Have you looked at the state design pattern?
You would have an abstract class to represent the state
FormState
that would contain a virtualSetToolStripButtons()
method:Then each derived implementation of that class is responsible for knowing how to set the visibility of each button (for the given state). For example the
FormStateNormal
class would override theSetToolsStripButtons
to contain something like this:There is more to it than just that, but hopefully this should be a good starting point.
您可以将编辑模式存储在布尔值中,然后设置enable =此布尔值
例如:(将编辑模式设置为“True”,否则设置为“False”,然后调用该函数)
you can store edit mode in Boolean then set enable= this Boolean
eg: (Set Edit Mode as "True", else set to "False", then call the function)
以下是我对此的解决方案:
lockcode 字符串传递给方法 lockToolStripItems()
执行for循环来锁定或解锁控件,请注意,1或0是我使用的字符1=Enabled=true,0=.Enable=false。如果未知字符则返回错误消息框,注意方法中的 x 变量有 3 个用途,A. for 语句中的计数器,B. char 数组索引,C. 工具条项索引
就这么简单分三步,希望对您有所帮助。
Below is my solution to this:
lockcode string is passed to the method lockToolStripItems()
For loop is executed to lock or unlock the controls, Do note that 1 or 0 are the characters I have used 1= Enabled=true, 0 = .Enable=false. if Unknown character then it returns an error message box, Note that x variable in the in the method has 3 uses, A. a counter in the for statement, B.char array index, C. an tool strip item index
As easy as that in 3 steps, hope this helps.