我有一个3个单选按钮,可以控制几个文本框所处的状态,我正在为所有3个按钮使用一个事件,但是控制文本框的部分看起来可能更流畅......必须有一个更好的方法来处理这个事件然后我有什么
Private Sub rdo_SkipBitLockerY_CheckedChanged(sender As RadioButton, e As EventArgs) Handles rdo_SkipBitLockerY.CheckedChanged, rdo_SkipBitLockerN.CheckedChanged, rdo_SkipBitLockerU.CheckedChanged
Select Case sender.Name
Case "rdo_SkipBitLockerY"
txt_BDEDriveLetter.Enabled = True
txt_BDEDriveSize.Enabled = True
txt_BDEInstall.Enabled = True
txt_BDEInstallSuppress.Enabled = True
txt_BDERecoveryKey.Enabled = True
txt_TPMOwnerPassword.Enabled = True
txt_OSDBitLockerStartupKeyDrive.Enabled = True
txt_OSDBitLockerWaitForEncryption.Enabled = True
AttributeRDOTable("SkipBitLocker") = "YES"
Case "rdo_SkipBitLockerN"
txt_BDEDriveLetter.Enabled = False
txt_BDEDriveSize.Enabled = False
txt_BDEInstall.Enabled = False
txt_BDEInstallSuppress.Enabled = False
txt_BDERecoveryKey.Enabled = False
txt_TPMOwnerPassword.Enabled = False
txt_OSDBitLockerStartupKeyDrive.Enabled = False
txt_OSDBitLockerWaitForEncryption.Enabled = False
AttributeRDOTable("SkipBitLocker") = "NO"
Case Else
txt_BDEDriveLetter.Enabled = False
txt_BDEDriveSize.Enabled = False
txt_BDEInstall.Enabled = False
txt_BDEInstallSuppress.Enabled = False
txt_BDERecoveryKey.Enabled = False
txt_TPMOwnerPassword.Enabled = False
txt_OSDBitLockerStartupKeyDrive.Enabled = False
txt_OSDBitLockerWaitForEncryption.Enabled = False
AttributeRDOTable.Remove("SkipBitLocker")
End Select
我可以做些什么来减少重复次数?
答案 0 :(得分:0)
抱歉,我不知道.Net平台,我只是尝试按照我在Java中的方式进行操作,可能你会发现它很有用。
txt_BDEDriveLetter.Enabled
txt_BDEDriveSize.Enabled
txt_BDEInstall.Enabled
txt_BDEInstallSuppress.Enabled
txt_BDERecoveryKey.Enabled
txt_TPMOwnerPassword.Enabled
txt_OSDBitLockerStartupKeyDrive
txt_OSDBitLockerWaitForEncryption
FALSE
每个案例操作应包含不超过1-2个语句。 这就是为什么如果你想遵守这个规则,你应该只进行提取方法重构。
请勿向方法发送超出其需要知道的更多信息。
您的方法无需了解 sender 对象。只需发送发件人对象的名称。
我会按如下方式重构您的代码(抱歉,我不知道.Net,我会尝试编写每个示例):
Private Sub rdo_SkipBitLockerY_CheckedChanged(Name As String, e As EventArgs) Handles rdo_SkipBitLockerY.CheckedChanged, rdo_SkipBitLockerN.CheckedChanged, rdo_SkipBitLockerU.CheckedChanged
Select Case Name
Case "rdo_SkipBitLockerY"
doRdoSkipBitLockerY();
Case "rdo_SkipBitLockerN"
doRdoSkipBitLockerN()
Case Else
doElse();
End Select
Private void doRdoSkipBitLockerY()
txt_BDEDriveLetter.Enabled = True
txt_BDEDriveSize.Enabled = True
txt_BDEInstall.Enabled = True
txt_BDEInstallSuppress.Enabled = True
txt_BDERecoveryKey.Enabled = True
txt_TPMOwnerPassword.Enabled = True
txt_OSDBitLockerStartupKeyDrive.Enabled = True
txt_OSDBitLockerWaitForEncryption.Enabled = True
AttributeRDOTable("SkipBitLocker") = "YES"
Private void doRdoSkipBitLockerN()
AttributeRDOTable("SkipBitLocker") = "NO"
Private void doElse()
AttributeRDOTable.Remove("SkipBitLocker");
如果我想以OOP方式(在Java中)我将提取所有3个案例作为枚举成员并将其作为参数发送到公共方法setVariables()
enum MY_CASES{
private boolean txt_BDEDriveLetter;
private boolean txt_BDEDriveSize;
...
MY_CASES(boolean txt_BDEDriveLetter, boolean txt_BDEDriveSize , ..){
this.txt_BDEDriveLetter = txt_BDEDriveLetter;
this.txt_BDEDriveSize = txt_BDEDriveSize;
...
}
CASE1(true, true, ...), CASE2(false, false, ...), CASE3(false, false, ...), ...
}
private Sub rdo_SkipBitLockerY_CheckedChanged(Name String, e EventArgs)
setVariables(MY_CASES.valueOf(NAME));
private void setVariables(MY_CASES case){
this.txt_BDEDriveLetter = case.getTxt_BDEDriveLetter;
this.txt_BDEDriveSize = case.getTxt_BDEDriveSize;
...
}
此版本允许您添加其他案例而无需重构您的rdo_SkipBitLockerY_CheckedChanged方法。