I want to get this simple windows application to call a class and update a listbox

Daniel McElhinney 120 Reputation points
2023-02-20T06:51:16.49+00:00

Hi I’m fairly new to programming and I am having trouble trying to get a simple application to run which I consist of a windows form app and a class. I was hoping someone would be good enough to help me.

The windows form app consist of two controls:

A listbox And a button

The idea is that when I click on the button it calls a class and populates the listbox

Here is the code for the class definition.

        
Public Class Class1
    Sub main()
        Dim arr As Integer() = {1, 5, 7, 2, 11, 13, 16, 21, 36, 29}
        Dim r = 7
        Dim n = arr.Length
        printcombination(arr, n, r)
    End Sub

    Private Shared Sub printcombination(ByVal arr As Integer(), ByVal n As Integer, ByVal r As Integer)
        Dim data = New Integer(r - 1) {}
        combinationUtil(arr, data, 0, n - 1, 0, r)
    End Sub

    Private Shared Sub combinationUtil(ByVal arr As Integer(), ByVal data As Integer(), ByVal start As Integer, ByVal [end] As Integer, ByVal index As Integer, ByVal r As Integer)
        If index = r Then
            For j = 0 To r - 1
                'Console.Write(data(j) & " ")
                Form1.ListBox1.Items.Add(data(j) & " ")
            Next
            'Console.WriteLine()
            Form1.ListBox1.Items.Add(" ")
            Return
        End If

        Dim i = start
        While i <= [end] AndAlso [end] - i + 1 >= r - index
            data(index) = arr(i)
            combinationUtil(arr, data, i + 1, [end], index = 1, r)
            i += 1
        End While
    End Sub
End Class


I’v converted this from a C# console application, hence the commenting out or the Console.Write(data(j) & " ") and Console.WriteLine()

The code for the Form button1_click event is:
Dim x As Class1
        x = New Class1
        x.main()

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,924 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,801 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 120.4K Reputation points
    2023-02-20T07:56:17.1266667+00:00

    Replace index = 1 with index + 1 and maybe improve the part related to listbox. For example:

    Private Shared Sub combinationUtil(arr As Integer(), data As Integer(), start As Integer, [end] As Integer, index As Integer, r As Integer)
        If index = r Then
            Form1.ListBox1.Items.Add(String.Join(" ", data))
            Return
        End If
    
        Dim i = start
        While i <= [end] AndAlso [end] - i + 1 >= r - index
            data(index) = arr(i)
            combinationUtil(arr, data, i + 1, [end], index + 1, r)
            i += 1
        End While
    End Sub
    

    You can also make a class that does not depend on specific form and control.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.