2010-05-20 108 views
1

有時候,我認爲我想太複雜了,或者只是厭倦了想太多或者其他你可以指出的東西。我是不是覺得太複雜?我怎樣才能簡化這段代碼?

我正在嘗試複製在YouTube中找到的尋呼機,以獲取我的Windows窗體應用程序。

這裏是我迄今所做的:

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 

第一個問題似乎是,我在沙決策圈。我不喜歡我編寫代碼的方式,並且不像預期的那樣工作。

所以問題是:我怎麼能簡化這個?

+0

是的,你確實需要一個假期 – Raj 2010-05-20 09:46:12

+0

對於初學者,你應該遍歷一系列按鈕。另外,我不太瞭解VB.Net,但是不能寫'btnPrevious.Visible =(SelIndex <> 1)',或者類似的東西? – 2010-05-20 09:47:40

+0

爲什麼你在一個子處理所有按鈕點擊?我認爲分開處理所有點擊會更容易。 – 2010-05-20 10:02:18

回答

0

先確定新的頁面,然後在按鈕等上設置文本。例如:

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 
相關問題