2013-03-08 100 views
0

我設計了一些軟件來幫助賣家計算客戶購買的產品。 我把它編碼得太原始了,我知道這段代碼可以縮短,但我不知道該怎麼做。Visual Basic:有沒有辦法縮短這個原始代碼

這是我的代碼:

Public Class Form1 

Private Strg1, Strg2, Strg3, Strg4, Strg5, Strg6, Strg7, Strg8, Strg9, Strg10, Strg11, Strg12, Strg13, Strg14, Strg15, Strg16, Strg17, Strg18, Strg19, Strg20, Strg21, Strg22, Strg23, Strg24, Strg25, Strg26, Strg27, Strg28, Strg29, Strg30 As Double 
Private p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, MedPrc1, MedPrc2, TotMedPrc As Double 
Private Function QntCalc(ByVal x As Double, ByVal y As Double) As Double 
    Return x * y 
End Function 
Private Sub Form1_Load(ByVal sder As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    MedPrc1 = 0 
    MedPrc2 = 0 
    TotMedPrc = 0 
End Sub 

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged 
    If TextBox1.Enabled = False Then 
     TextBox1.Enabled = True 
     p1 = Val(Label1.Text) 
    Else 
     TextBox1.Enabled = False 
     TextBox1.Text = "" 
     p1 = 0 
    End If 
End Sub 

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged 
    If TextBox2.Enabled = False Then 
     TextBox2.Enabled = True 
     p2 = Val(Label2.Text) 
    Else 
     TextBox2.Enabled = False 
     TextBox2.Text = "" 
     p2 = 0 
    End If 
End Sub 

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged 
    If TextBox3.Enabled = False Then 
     TextBox3.Enabled = True 
     p3 = Val(Label3.Text) 
    Else 
     TextBox3.Enabled = False 
     TextBox3.Text = "" 
     p3 = 0 
    End If 
End Sub 

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged 
    If TextBox4.Enabled = False Then 
     TextBox4.Enabled = True 
     p4 = Val(Label4.Text) 
    Else 
     TextBox4.Enabled = False 
     TextBox4.Text = "" 
     p4 = 0 
    End If 
End Sub 

Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged 
    If TextBox5.Enabled = False Then 
     TextBox5.Enabled = True 
     p5 = Val(Label5.Text) 
    Else 
     TextBox5.Enabled = False 
     TextBox5.Text = "" 
     p5 = 0 
    End If 
End Sub 

Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged 
    If TextBox6.Enabled = False Then 
     TextBox6.Enabled = True 
     p6 = Val(Label6.Text) 
    Else 
     TextBox6.Enabled = False 
     TextBox6.Text = "" 
     p6 = 0 
    End If 
End Sub 

Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged 
    If TextBox7.Enabled = False Then 
     TextBox7.Enabled = True 
     p7 = Val(Label7.Text) 
    Else 
     TextBox7.Enabled = False 
     TextBox7.Text = "" 
     p7 = 0 
    End If 
End Sub 

Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged 
    If TextBox8.Enabled = False Then 
     TextBox8.Enabled = True 
     p8 = Val(Label8.Text) 
    Else 
     TextBox8.Enabled = False 
     TextBox8.Text = "" 
     p8 = 0 
    End If 
End Sub 

Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged 
    If TextBox9.Enabled = False Then 
     TextBox9.Enabled = True 
     p9 = Val(Label9.Text) 
    Else 
     TextBox9.Enabled = False 
     TextBox9.Text = "" 
     p9 = 0 
    End If 
End Sub 

Private Sub CheckBox10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox10.CheckedChanged 
    If TextBox10.Enabled = False Then 
     TextBox10.Enabled = True 
     p10 = Val(Label10.Text) 
    Else 
     TextBox10.Enabled = False 
     TextBox10.Text = "" 
     p10 = 0 
    End If 
End Sub 

Private Sub CheckBox11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged 
    If TextBox11.Enabled = False Then 
     TextBox11.Enabled = True 
    Else 
     TextBox11.Enabled = False 
     TextBox11.Text = "" 
     p1 = 0 
    End If 
End Sub 

Private Sub CheckBox12_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox12.CheckedChanged 
    If TextBox12.Enabled = False Then 
     TextBox12.Enabled = True 
    Else 
     TextBox12.Enabled = False 
     TextBox12.Text = "" 
    End If 
End Sub 

Private Sub CheckBox13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox13.CheckedChanged 
    If TextBox13.Enabled = False Then 
     TextBox13.Enabled = True 
    Else 
     TextBox13.Enabled = False 
     TextBox13.Text = "" 
     p1 = 0 
    End If 
End Sub 

Private Sub CheckBox14_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox14.CheckedChanged 
    If TextBox14.Enabled = False Then 
     TextBox14.Enabled = True 
    Else 
     TextBox14.Enabled = False 
     TextBox14.Text = "" 
    End If 
End Sub 

Private Sub CheckBox15_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox15.CheckedChanged 
    If TextBox15.Enabled = False Then 
     TextBox15.Enabled = True 
    Else 
     TextBox15.Enabled = False 
     TextBox15.Text = "" 
    End If 
End Sub 

Private Sub CheckBox16_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox16.CheckedChanged 
    If TextBox16.Enabled = False Then 
     TextBox16.Enabled = True 
    Else 
     TextBox16.Enabled = False 
     TextBox16.Text = "" 
    End If 
End Sub 

Private Sub CheckBox17_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox17.CheckedChanged 
    If TextBox17.Enabled = False Then 
     TextBox17.Enabled = True 
    Else 
     TextBox17.Enabled = False 
     TextBox17.Text = "" 
    End If 
End Sub 

Private Sub CheckBox18_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox18.CheckedChanged 
    If TextBox18.Enabled = False Then 
     TextBox18.Enabled = True 
    Else 
     TextBox18.Enabled = False 
     TextBox18.Text = "" 
    End If 
End Sub 

Private Sub CheckBox19_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox19.CheckedChanged 
    If TextBox19.Enabled = False Then 
     TextBox19.Enabled = True 
    Else 
     TextBox19.Enabled = False 
     TextBox19.Text = "" 
    End If 
End Sub 

Private Sub CheckBox20_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox20.CheckedChanged 
    If TextBox20.Enabled = False Then 
     TextBox20.Enabled = True 
    Else 
     TextBox20.Enabled = False 
     TextBox20.Text = "" 
    End If 
End Sub 

Private Sub CheckBox21_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox21.CheckedChanged 
    If TextBox21.Enabled = False Then 
     TextBox21.Enabled = True 
    Else 
     TextBox21.Enabled = False 
     TextBox21.Text = "" 
    End If 
End Sub 

Private Sub CheckBox22_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox22.CheckedChanged 
    If TextBox22.Enabled = False Then 
     TextBox22.Enabled = True 
    Else 
     TextBox22.Enabled = False 
     TextBox22.Text = "" 
    End If 
End Sub 

Private Sub CheckBox23_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox23.CheckedChanged 
    If TextBox23.Enabled = False Then 
     TextBox23.Enabled = True 
    Else 
     TextBox23.Enabled = False 
     TextBox23.Text = "" 
    End If 
End Sub 

Private Sub CheckBox24_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox24.CheckedChanged 
    If TextBox24.Enabled = False Then 
     TextBox24.Enabled = True 
    Else 
     TextBox24.Enabled = False 
     TextBox24.Text = "" 
    End If 
End Sub 

Private Sub CheckBox25_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox25.CheckedChanged 
    If TextBox25.Enabled = False Then 
     TextBox25.Enabled = True 
    Else 
     TextBox25.Enabled = False 
     TextBox25.Text = "" 
    End If 
End Sub 

Private Sub CheckBox26_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox26.CheckedChanged 
    If TextBox26.Enabled = False Then 
     TextBox26.Enabled = True 
    Else 
     TextBox26.Enabled = False 
     TextBox26.Text = "" 
    End If 
End Sub 

Private Sub CheckBox27_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox27.CheckedChanged 
    If TextBox27.Enabled = False Then 
     TextBox27.Enabled = True 
    Else 
     TextBox27.Enabled = False 
     TextBox27.Text = "" 
    End If 
End Sub 

Private Sub CheckBox28_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox28.CheckedChanged 
    If TextBox28.Enabled = False Then 
     TextBox28.Enabled = True 
    Else 
     TextBox28.Enabled = False 
     TextBox28.Text = "" 
    End If 
End Sub 

Private Sub CheckBox29_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox29.CheckedChanged 
    If TextBox29.Enabled = False Then 
     TextBox29.Enabled = True 
    Else 
     TextBox29.Enabled = False 
     TextBox29.Text = "" 
    End If 
End Sub 

Private Sub CheckBox30_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox30.CheckedChanged 
    If TextBox30.Enabled = False Then 
     TextBox30.Enabled = True 
    Else 
     TextBox30.Enabled = False 
     TextBox30.Text = "" 
    End If 
End Sub 

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 
    Strg1 = Str(QntCalc(p1, Val(TextBox1.Text))) 
End Sub 

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged 
    Strg2 = Str(QntCalc(p2, Val(TextBox2.Text))) 
End Sub 

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged 
    Strg3 = Str(QntCalc(p3, Val(TextBox3.Text))) 
End Sub 

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged 
    Strg4 = Str(QntCalc(p4, Val(TextBox4.Text))) 
End Sub 

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged 
    Strg5 = Str(QntCalc(p5, Val(TextBox5.Text))) 
End Sub 

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged 
    Strg6 = Str(QntCalc(p6, Val(TextBox6.Text))) 
End Sub 

Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged 
    Strg7 = Str(QntCalc(p7, Val(TextBox7.Text))) 
End Sub 

Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged 
    Strg8 = Str(QntCalc(p8, Val(TextBox8.Text))) 
End Sub 

Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged 
    Strg9 = Str(QntCalc(p9, Val(TextBox9.Text))) 
End Sub 

Private Sub TextBox10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged 
    Strg10 = Str(QntCalc(p10, Val(TextBox10.Text))) 
End Sub 

Private Sub MedPrc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MedPrc.TextChanged 
End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    TotMedPrc = Strg1 + Strg2 + Strg3 + Strg4 + Strg5 + Strg6 + Strg7 + Strg8 + Strg9 + Strg10 
    MedPrc.Text = Val(TotMedPrc) 
End Sub 
End Class 

有沒有辦法縮短這個?

+0

您需要在此處使用數組。我建議你拿起一本關於VB編程的書,因爲這是一個非常基礎的東西,它不能在編程論壇上教好。 – 2013-03-08 08:06:12

+0

這隻會縮短變量的聲明。但是,我也會嘗試這樣做 – conquistador 2013-03-08 08:11:13

+0

不,它會將整個代碼縮短到當前大小的十分之一,因爲所有其餘的代碼都可以通過處理數組來實現。 – 2013-03-08 09:46:20

回答

0

你的代碼看起來像VB.NET嗎?在VBA中,你會寫一些不同的東西。示例:

Option Explicit 

Private Strg1 As String 
Private p1 As Double 

Private Sub CheckBox1_Change() 
    OnCheckBoxChange Me.TextBox1, Me.Label1, p1 
End Sub 

Private Sub TextBox1_Change() 
    OnTextBoxChanged Me.TextBox1, p1, Strg1 
End Sub 

Private Sub OnTextBoxChanged(ByRef textBoxControl As Variant, ByRef p As Double, Strg As String) 
    Strg = Str(QntCalc(p, Val(textBoxControl.Text))) 
End Sub 

Private Sub OnCheckBoxChange(ByRef textBoxControl As Variant, ByRef labelControl As Variant, ByRef p As Double) 
    If textBoxControl.Enabled = False Then 
     textBoxControl.Enabled = True 
     p = Val(labelControl.Caption) 
    Else 
     textBoxControl.Enabled = False 
     textBoxControl.Text = "" 
     p = 0 
    End If 
End Sub 

Private Function QntCalc(ByVal x As Double, ByVal y As Double) As Double 
    QntCalc = x * y 
End Function 
+0

是這個VB.NET嗎? http://i45.tinypic.com/fx45jb.jpg – conquistador 2013-03-08 07:49:25

+0

是的,它是VB.NET。 – dee 2013-03-08 07:57:38

+0

也許你在你的問題中加了錯誤的標籤?你想VB.NET,但添加VBA instaed? – dee 2013-03-08 07:59:01

相關問題