所以目前我正在学习VB,我有一个项目,我必须根据以下内容显示一个图片框:
选中了哪个单选框
和
如果选中了使图片框可见的复选框。作为一个重视清洁,良好代码的人,这是我的代码,它让我感到恐惧。我的问题,是否有某种方法可以使用案例或其他一些我在VB.net中不知道的构造来压缩以下内容?
If CheckBox1.Checked = False Then
BooksPictureBox.Visible = False
MusicPictureBox.Visible = False
PeriodicalsPictureBox.Visible = False
CoffeeBarPictureBox.Visible = False
End If
If RadioButton1.Checked And CheckBox1.Checked = True Then
BooksPictureBox.Visible = True
MusicPictureBox.Visible = False
PeriodicalsPictureBox.Visible = False
CoffeeBarPictureBox.Visible = False
End If
If RadioButton2.Checked And CheckBox1.Checked = True Then
BooksPictureBox.Visible = False
MusicPictureBox.Visible = True
PeriodicalsPictureBox.Visible = False
CoffeeBarPictureBox.Visible = False
End If
If RadioButton3.Checked And CheckBox1.Checked = True Then
BooksPictureBox.Visible = False
MusicPictureBox.Visible = False
PeriodicalsPictureBox.Visible = True
CoffeeBarPictureBox.Visible = False
End If
If RadioButton4.Checked And CheckBox1.Checked = True Then
BooksPictureBox.Visible = False
MusicPictureBox.Visible = False
PeriodicalsPictureBox.Visible = False
CoffeeBarPictureBox.Visible = True
End If
注意 - 所有图像彼此堆叠,并且所有图像都在不可见的情况下开始。
答案 0 :(得分:1)
我会创建一个将所有picBox设置为不可见的SUb
Private Sub setInvisible
BooksPictureBox.Visible = False
MusicPictureBox.Visible = False
PeriodicalsPictureBox.Visible = False
CoffeeBarPictureBox.Visible = False
End sub
然后另一个例程
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
setInvisible
if checkbox1.checked = false then exit sub
if radiobutton1.checked = true then bookspicturebox.visible = true
if radiobutton2.checked = true then musicpicturebox.visible = true
etc...
End Sub
当然,更简单的方法是根据单选按钮选择动态加载图像。
答案 1 :(得分:1)
也许这个?
' Set first condition, the check box must be checked
BooksPictureBox.Visible = CheckBox1.Checked
MusicPictureBox.Visible = CheckBox1.Checked
PeriodicalsPictureBox.Visible = CheckBox1.Checked
CoffeeBarPictureBox.Visible = CheckBox1.Checked
' Set individual conditions, the radio button must be checked
BooksPictureBox.Visible = RadioButton1.Checked
MusicPictureBox.Visible = RadioButton2.Checked
PeriodicalsPictureBox.Visible = RadioButton3.Checked
CoffeeBarPictureBox.Visible = RadioButton4.Checked
修改:或者,您可以采用不同的Clean Code方法:)
ShowOrHidePictureBoxes()
...
Private Sub ShowOrHidePictureBoxes
ShowOrHideBooks()
ShowOrHideMusic()
ShowOrHidePeriodicals()
ShowOrHideCoffeeBar()
End Sub
Private Sub ShowOrHideBooks
BooksPictureBox.Visible = RadioButton1.Checked And CheckBox1.Checked
End Sub
Private Sub ShowOrHideMusic
BooksPictureBox.Visible = RadioButton2.Checked And CheckBox1.Checked
End Sub
Private Sub ShowOrHidePeriodicals
BooksPictureBox.Visible = RadioButton3.Checked And CheckBox1.Checked
End Sub
Private Sub ShowOrHideCoffeeBar
BooksPictureBox.Visible = RadioButton4.Checked And CheckBox1.Checked
End Sub
你可以通过两种方式进一步发展:
CheckBox1
和RadioButton1
之类的内容比If
语句更多地污染代码:)答案 2 :(得分:0)
你可以把它归结为:
Dim fBooksVisible As Boolean
Dim fMusicVisible As Boolean
Dim fPeriodicalsVisible As Boolean
Dim fCoffeeBarVisible As Boolean
If CheckBox1.Checked Then
fBooksVisible = RadioButton1.Checked
fMusicVisible = RadioButton2.Checked
fPeriodicalsVisible = RadioButton3.Checked
fCoffeeBarVisible = RadioButton4.Checked
End If
BooksPictureBox.Visible = fBooksVisible
MusicPictureBox.Visible = fMusicVisible
PeriodicalsPictureBox.Visible = fPeriodicalsVisible
CoffeeBarPictureBox.Visible = fCoffeeBarVisible
甚至:
BooksPictureBox.Visible = RadioButton1.Checked AndAlso CheckBox1.Checked
MusicPictureBox.Visible = RadioButton2.Checked AndAlso CheckBox1.Checked
PeriodicalsPictureBox.Visible = RadioButton3.Checked AndAlso CheckBox1.Checked
CoffeeBarPictureBox.Visible = RadioButton4.Checked AndAlso CheckBox1.Checked