Am I thinking too complicated? How can I simplify this piece of code?
Well sometimes, I believe am thinking toooooo complicated or just got tired of thinking too much or something else that you could point.
I am trying to copy the pager found in YouTube for a Windows Forms application of mine.
And here is wh开发者_如何学运维at I have done so far:
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btnNext.Click
If sender.text = "previous" Then
SelIndex -= 1
ElseIf sender.text = "next" Then
SelIndex += 1
Else
If sender.text > SelIndex Then
If sender.name = "btn5" OrElse sender.name = "btn6" OrElse sender.name = "btn7" Then
btn1.Text += 1
btn2.Text += 1
btn3.Text += 1
btn4.Text += 1
btn5.Text += 1
btn6.Text += 1
btn7.Text += 1
SelIndex = btn4.Text
Else
SelIndex = sender.text
End If
ElseIf sender.text < SelIndex Then
If sender.name = "btn1" OrElse sender.name = "btn2" OrElse sender.name = "btn3" Then
If btn1.Text <> "1" Then
btn1.Text -= 1
btn2.Text -= 1
btn3.Text -= 1
btn4.Text -= 1
btn5.Text -= 1
btn6.Text -= 1
btn7.Text -= 1
SelIndex = btn4.Text
Else
SelIndex = sender.text
End If
End If
Else
SelIndex = sender.text
End If
End If
Select Case SelIndex
Case 1
btnPrevious.Visible = False
Case Else
btnPrevious.Visible = True
End Select
Label1.Text = SelIndex
End Sub
The first problem seems to be that I am making circles in the sand. I do not like the way I wrote the code AND is not working as it supposed to.
So the question is: how could I simplify this one?
Determine the new page first, then set the text on the buttons and such. Something like:
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btnNext.Click
If sender.text = "previous" Then
SelIndex -= 1
ElseIf sender.text = "next" Then
SelIndex += 1
Else
SelIndex = Int32.Parse(sender.text)
End If
Dim buttons As Button() = { btn1, btn2, btn3, btn4, btn5, btn6, btn7 }
For i As Integer = 0 to buttons.Length - 1
Dim page As Integer = SelIndex + i - 3
If page >= 1 Then
buttons(i).Visible = True
buttons(i).Text = page.ToString
Else
buttons(i).Visible = Flase
End If
Next
btnPrevious.Visible = SelIndex > 1
Label1.Text = SelIndex
End Sub
精彩评论