调用与内部调用的不同功能相同的功能

问题描述:

我希望我的标题清楚。让我解释。我有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()

我不知道如何做一个子中,我可以使这种差异。希望我很清楚。提前致谢。

+1

你应该尝试写一个做一个简单的事情方法的唯一的地方。尽我所知,点击事件确实是5.如果每个块都是自己的子块,可以根据需要将“通用”块与更精确的块混合匹配:[单个责任原则](https:// en。 wikipedia.org/wiki/Single_responsibility_principle)。简单性是可重用性的核心.BTW似乎有大量的标签是从未使用或处理过的。 – Plutonix

+1

您还应该打开Option Strict,以便IDE可以警告您可疑的类型转换 – Plutonix

+0

感谢您的建议,我正在为我的公司做一个程序,我不是程序员,但我知道一些基本的东西,并且很喜欢编程,所以我为此编写的第一个代码程序是一次又一次的重复代码,当我想要添加一些新的程序时,我意识到我需要使代码更简单,否则每次尝试添加内容时都会挣扎。 0再一次,感谢您的建议,因为我认为我的代码很简单。我想你们在我的一个问题中已经帮助过我了! 编辑:所有的标签都使用btw :) –

创建功能,并在其发送的值,所以你可以改变x功能

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

在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 由于它在那里我看你使用的功能之一,你提到

+0

我认为这可能工作正确!感谢这个想法。那正是我所期待的。 –