Help shortening code

957

New member
Joined
Mar 23, 2011
Messages
1
Programming Experience
Beginner
Hi, I was hoping I could receive some help shortening my code.

VB.NET:
Private Sub NP1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP1.Click, NP1.MouseDown
        If NP1fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(0))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(0)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(0))
                process.Start(NPLBfp.Items(0))
            End If
        End If
    End Sub

    Private Sub NP2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP2.Click
        If NP2fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(1))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(1)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(1))
                process.Start(NPLBfp.Items(1))
            End If
        End If
    End Sub

    Private Sub NP3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP3.Click
        If NP3fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(2))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(2)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(2))
                process.Start(NPLBfp.Items(2))
            End If
        End If
    End Sub

    Private Sub NP4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP4.Click
        If NP4fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(3))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(3)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(3))
                process.Start(NPLBfp.Items(3))
            End If
        End If
    End Sub

    Private Sub NP5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP5.Click
        If NP5fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(4))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(4)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(4))
                process.Start(NPLBfp.Items(4))
            End If
        End If
    End Sub

    Private Sub NP6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP6.Click
        If NP6fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(5))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(5)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(5))
                process.Start(NPLBfp.Items(5))
            End If
        End If
    End Sub

    Private Sub NP7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP7.Click
        If NP7fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(6))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(6)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(6))
                process.Start(NPLBfp.Items(4))
            End If
        End If
    End Sub

    Private Sub NP8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP8.Click
        If NP8fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(7))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(7)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(7))
                process.Start(NPLBfp.Items(7))
            End If
        End If
    End Sub

    Private Sub NP9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP9.Click
        If NP9fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(8))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(8)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(8))
                process.Start(NPLBfp.Items(8))
            End If
        End If
    End Sub

    Private Sub NP10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP10.Click
        If NP10fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(9))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(9)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(9))
                process.Start(NPLBfp.Items(9))
            End If
        End If
    End Sub

    Private Sub NP11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP11.Click
        If NP11fp.Text = "11" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(10))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(10)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(10))
                process.Start(NPLBfp.Items(10))
            End If
        End If
    End Sub

    Private Sub NP12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP12.Click
        If NP12fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(11))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(11)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(11))
                process.Start(NPLBfp.Items(11))
            End If
        End If
    End Sub

    Private Sub NP13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP13.Click
        If NP13fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(12))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(12)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(12))
                process.Start(NPLBfp.Items(12))
            End If
        End If
    End Sub

    Private Sub NP14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP14.Click
        If NP14fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(13))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(13)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(13))
                process.Start(NPLBfp.Items(13))
            End If
        End If
    End Sub

    Private Sub NP15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP15.Click
        If NP15fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(14))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(14)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(14))
                process.Start(NPLBfp.Items(14))
            End If
        End If
    End Sub

    Private Sub NP16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP16.Click
        If NP16fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(15))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(15)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(15))
                process.Start(NPLBfp.Items(15))
            End If
        End If
    End Sub

    Private Sub NP17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP17.Click
        If NP17fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(16))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(16)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(16))
                process.Start(NPLBfp.Items(16))
            End If
        End If
    End Sub

    Private Sub NP18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP18.Click
        If NP18fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(17))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(17)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(17))
                process.Start(NPLBfp.Items(17))
            End If
        End If
    End Sub

    Private Sub NP19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP19.Click
        If NP19fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(18))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(18)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(18))
                process.Start(NPLBfp.Items(18))
            End If
        End If
    End Sub

    Private Sub NP20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP20.Click
        If NP20fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(19))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(19)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(19))
                process.Start(NPLBfp.Items(19))
            End If
        End If
    End Sub

    Private Sub NP21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP21.Click
        If NP21fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(20))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(20)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(20))
                process.Start(NPLBfp.Items(20))
            End If
        End If
    End Sub

    Private Sub NP22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP22.Click
        If NP22fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(21))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(21)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(21))
                process.Start(NPLBfp.Items(21))
            End If
        End If
    End Sub

    Private Sub NP23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP22.Click
        If NP23fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(22))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(22)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(22))
                process.Start(NPLBfp.Items(22))
            End If
        End If
    End Sub

    Private Sub NP24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP24.Click
        If NP24fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(23))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(23)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(23))
                process.Start(NPLBfp.Items(23))
            End If
        End If
    End Sub

    Private Sub NP25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP25.Click
        If NP25fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(24))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(24)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(24))
                process.Start(NPLBfp.Items(24))
            End If
        End If
    End Sub

    Private Sub NP26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP26.Click
        If NP26fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(25))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(25)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(25))
                process.Start(NPLBfp.Items(25))
            End If
        End If
    End Sub

    Private Sub NP27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP27.Click
        If NP27fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(26))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(26)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(26))
                process.Start(NPLBfp.Items(26))
            End If
        End If
    End Sub

    Private Sub NP28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP28.Click
        If NP28fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(27))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(27)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(27))
                process.Start(NPLBfp.Items(27))
            End If
        End If
    End Sub

    Private Sub NP29_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP29.Click
        If NP29fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(28))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(28)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(28))
                process.Start(NPLBfp.Items(28))
            End If
        End If
    End Sub

    Private Sub NP30_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NP30.Click
        If NP30fp.Text = "0" Then
            Exit Sub
        Else
            If Remove = 1 Then
                NPLBfp.Items.Remove(NPLBfp.Items(29))
                II = II - 1
                Remove = 0
            Else
                Dim process As New Process()
                process.StartInfo.UseShellExecute = False
                process.StartInfo.FileName = NPLBfp.Items(29)
                process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(NPLBfp.Items(29))
                process.Start(NPLBfp.Items(29))
            End If
        End If
    End Sub

As you can see, I have a lot of buttons a way too much code to show for it. is there any way to shorten this?
 
You can use a single method to handle multiple events and then use the 'sender' parameter to determine which object raised the event. Amongst other options, you can use concurrent arrays or a Dictionary to determine what to do for each object. E.g.
VB.NET:
Public Class Form1

    Private buttons As Button()
    Private greetings As String() = {"Hello", "Yo, whazup?!"}

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.buttons = {Me.Button1, Me.Button2}
    End Sub

    Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button2.Click, Button1.Click
        Dim index = Array.IndexOf(Me.buttons, sender)
        Dim greeting = Me.greetings(index)

        MessageBox.Show(greeting)
    End Sub

End Class
 
Back
Top