使用内部调用的不同函数调用相同的函数

时间:2016-04-27 15:56:30

标签: vb.net visual-studio-2015

我希望我的头衔清楚。让我解释。我有4个按钮,几乎一样。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    LimiteGlobalSeparador = InputBox("Introduzca la distancia máxima entre huecos en el tubo", "Cuadro de Datos, Separaciones", "")
    Dim a, b, c, d, f, g As New Label
    Dim o, p, q, r, s, t As New Label
    a.Text = "Medida del Tubo"
    b.Text = "Espacio 1"
    c.Text = "Espacio 2"
    d.Text = "Espacio 3"
    f.Text = "Espacio 4"
    g.Text = "Espacio 5"
    For i = 0 To 5
        TableLayoutPanel1.Controls.Add(a, 0, i)
    Next
    If TextBox1.Text <> 0 Then
        For j = 1 To Int(TextBox1.Text)
            Dim x As New List(Of Decimal)
            x = MedTuboFuncPCT()
            For i = 0 To x.Count - 1
                Dim lbl As New Label
                lbl.Text = Math.Round(x(i), 2)
                TableLayoutPanel1.Controls.Add(lbl, j, i)
            Next
            ContadorGlobal = ContadorGlobal + 1
        Next
    Else
    End If

    ContadorGlobal = 0

    Dim h As Integer
    h = Int(TextBox2.Text - TextBox1.Text)
    If TextBox2.Text <> 0 Then
        If h = Int(TextBox2.Text) Then
            For j = 1 To Int(TextBox2.Text)
                Dim x As New List(Of Decimal)
                x = MedTuboFunCTCT()
                For i = 0 To x.Count - 1
                    Dim lbl As New Label
                    lbl.Text = Math.Round(x(i), 2)
                    TableLayoutPanel1.Controls.Add(lbl, j, i)
                Next
                ContadorGlobal = ContadorGlobal + 1
            Next
        Else
            ContadorGlobal = 0
            For j = Int(1) + Int(TextBox1.Text) To (Int(TextBox1.Text) + Int(TextBox2.Text))
                Dim x As New List(Of Decimal)
                x = MedTuboFunCTCT()
                For i = 0 To x.Count - 1
                    Dim lbl As New Label
                    lbl.Text = Math.Round(x(i), 2)
                    TableLayoutPanel1.Controls.Add(lbl, j, i)
                Next
                ContadorGlobal = ContadorGlobal + 1
            Next
        End If
    End If

    ContadorGlobal = 0
    If TextBox3.Text <> 0 Then
        Dim x As New List(Of Decimal)
        x = MedTuboFuncPP()
        For i = 0 To x.Count - 1
            Dim lbl As New Label
            lbl.Text = Math.Round(x(i), 2)
            TableLayoutPanel1.Controls.Add(lbl, 1, i)
        Next
    End If
    If Te180 > 0 Then
        o.Text = "Medida del Tubo"
        p.Text = "Espacio 1"
        q.Text = "Espacio 2"
        r.Text = "Espacio 3"
        s.Text = "Espacio 4"
        t.Text = "Espacio 5"
        For i = 6 To 11
            TableLayoutPanel1.Controls.Add(o, 0, i)
        Next
    End If
End Sub

所以我正在尝试创建一个Sub,然后每个按钮调用该子,所以我没有像现在这样在每个按钮中重复代码。唯一的变化是:

一个按钮调用此功能。 x = MedTuboFuncPCT().

另一个人打电话给x = MedTuboFuncPCTVid()

另一个人打电话给x = MedTuboFuncPCTVidBB()

我不知道如何制作一个可以让我发挥作用的Sub。希望我足够清楚。提前谢谢。

1 个答案:

答案 0 :(得分:1)

创建您的函数并在其中发送一个值,以便您可以更改x函数

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  VFunction = 1
  RepeatedCode(VFunction)
End Sub

On Button2 VFunction = 2等等。然后创建你的功能。

Public Sub RepeatedCode (ByVal VFunction as integer)
    If VFunction = 1 Then
        x = MedTuboFuncPCT()
    ElseIf VFunction = 2 Then
       x = MedTuboFuncPCTVid()
    ElseIf VFunction = 3 Then
       x = MedTuboFuncPCT()
    End If

   'The Rest of your code

我相信If条件必须放在If TextBox1.Text <> 0内 因为它是我看到你使用过你提到的功能之一的唯一地方