Let me preface this by saying that I've only taken 1 formal programming class and everything else I've learned is from trial and error or Google (forums, blogs). I have never taken an algorithm class or an advanced math class.

In the process of making my current application, I need to perform multiple checks and need complex if then statements in order to eliminate multiple possibilities (my datagridview, which I'm validating has at minimum of 9 fields to validate) all of which will require a separate function call.

In the process of doing this, I've noticed that I've begun to miss combinations and am looking to write a quick application to tell me the what all the combinations are. Essentially it'll have a listbox for the placeholders I'll be using to replace with the boolean operation in the project. The listbox will be associated with 1 boolean value in a boolean array. As it's looping through the array showing all the possibilities, I'll be lining up the values with the listbox.

The problem I'm having relates to how to output all the possible combinations of True/False values any number of booleans. How would I process through the array and flip the values as they need to be?

The output, for 3 values, I'm looking for would consist of:
F:ListBoxValue1 F:ListBoxValue2 F:ListBoxValue3
T:ListBoxValue1 F:ListBoxValue2 F:ListBoxValue3
T:ListBoxValue1 T:ListBoxValue2 F:ListBoxValue3
T:ListBoxValue1 T:ListBoxValue2 T:ListBoxValue3
F:ListBoxValue1 T:ListBoxValue2 T:ListBoxValue3
F:ListBoxValue1 F:ListBoxValue2 T:ListBoxValue3
F:ListBoxValue1 T:ListBoxValue2 F:ListBoxValue3

I don't need the code for the form or even include the listbox, just how to get all the combinations of the boolean array.

Thanks in advance!

8 Years
Discussion Span
Last Post by Stevoni

with 9 boolean variables that is 512 combinations

If you are not into maths this is going to freak you out a bit but the best approach is to resort to binary. An integer in binary form consists of a series of 1s and 0s that can represent your 9 variables

eg 010101011 = 171

the left most 0 represents your first listbox
second digit (1) represents listbox 2
third digit (0) represents listbox 3
and so on.

this is achieved in .NET with a BitArray. In the example below I have set values 1 & 3 arbitarily. In your code you will set all nine elements of the bit array based on your list boxes.

the function BinToInt converts the bit array to an integer between 0 and 511 which represents to 512 different combinations.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim LineIn As String
        LineIn = ""
        Dim Int1 As Int32
        Dim test As New BitArray(9, False)
        test.Item(1) = True
        test.Item(3) = True
        Int1 = BinToInt(test, 9)

    End Sub
    Public Function BinToInt(ByVal BAin As BitArray, ByVal BAdigits As Int16) As Int16
        Dim i As Int16
        Dim outint As Int16
        outint = 0
        For i = 0 To BAdigits - 1
            If BAin(i) Then
                outint = outint + 2 ^ i
            End If
        Next i
        BinToInt = outint
    End Function

with 9 boolean variables that is 512 combinations

Thanks for the help!

There are restrictions on most of the combinations. For example, the last two can only be used when the first one is true. And even then both the last two can be independent or together with each other.

In your code you will set all nine elements of the bit array based on your list boxes.

I apologize for confusing the problem, the listbox is used as place holder for me to interpret into the code, not to set the values. I.e.: That way "TFFFFTFTT" would turn into "T: Partnumber F:OrderNumber F:LineNumber",etc.

The problem I'm having is determining all the outcomes for the amount of items listed in the list box. If there are 3 items, then there'll be 8 combinations (man I hope my math is right, not enough coffee yet), 9 then 512 combinations. I need the code to take in the n boolean options and loop through and tell me what all the combinations are.

Edited by Stevoni: Damn smilies


I presume these are coming from user input, in which case it may be simpler to restrict your inputs to valid combinations than try and interpret them later.

For example if we stick to 4 inputs for simplicity. If 3 & 4 can only be supplied if 1 is supplied then write some client side script to run on exit from 1. if 1 is Null then disable 3 and 4, otherwise enable 3 & 4. This way you only need to cater for valid combinations in your code.


I finally took the time to attempt to find a solution. From what I can tell it's working properly, but if you see any errors, please let me know.

I figured attaching my finished product would help anyone else who was looking for something similar.

Note: the file uploaded is a Visual Basic file and the extension needs to be changed to "vb" to run it. Compiled in VB .Net 2.0.

Edited by Stevoni: File didn't compile correctly.

Public Class frmBooleanOutCome
#Region "Designer Information"
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
            End If
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.lstItems = New System.Windows.Forms.ListBox
        Me.butGo = New System.Windows.Forms.Button
        Me.butQuit = New System.Windows.Forms.Button
        Me.butAdd = New System.Windows.Forms.Button
        Me.butRemove = New System.Windows.Forms.Button
        Me.butClear = New System.Windows.Forms.Button
        Me.dgvMain = New System.Windows.Forms.DataGridView
        Me.ckOrderColumnsByList = New System.Windows.Forms.CheckBox
        Me.ckSortListBox = New System.Windows.Forms.CheckBox
        Me.ckAddMultipleEntries = New System.Windows.Forms.CheckBox
        CType(Me.dgvMain, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.lstItems.FormattingEnabled = True
        Me.lstItems.Location = New System.Drawing.Point(100, 12)
        Me.lstItems.Name = "lstItems"
        Me.lstItems.Size = New System.Drawing.Size(120, 82)
        Me.lstItems.TabIndex = 0
        Me.butGo.Enabled = False
        Me.butGo.Location = New System.Drawing.Point(13, 100)
        Me.butGo.Name = "butGo"
        Me.butGo.Size = New System.Drawing.Size(93, 23)
        Me.butGo.TabIndex = 1
        Me.butGo.Text = "Get Outcomes"
        Me.butGo.UseVisualStyleBackColor = True
        Me.butQuit.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.butQuit.Location = New System.Drawing.Point(145, 100)
        Me.butQuit.Name = "butQuit"
        Me.butQuit.Size = New System.Drawing.Size(75, 23)
        Me.butQuit.TabIndex = 2
        Me.butQuit.Text = "Quit"
        Me.butQuit.UseVisualStyleBackColor = True
        Me.butAdd.Location = New System.Drawing.Point(13, 12)
        Me.butAdd.Name = "butAdd"
        Me.butAdd.Size = New System.Drawing.Size(75, 23)
        Me.butAdd.TabIndex = 3
        Me.butAdd.Text = "Add"
        Me.butAdd.UseVisualStyleBackColor = True
        Me.butRemove.Enabled = False
        Me.butRemove.Location = New System.Drawing.Point(13, 41)
        Me.butRemove.Name = "butRemove"
        Me.butRemove.Size = New System.Drawing.Size(75, 23)
        Me.butRemove.TabIndex = 4
        Me.butRemove.Text = "Remove"
        Me.butRemove.UseVisualStyleBackColor = True
        Me.butClear.Enabled = False
        Me.butClear.Location = New System.Drawing.Point(13, 70)
        Me.butClear.Name = "butClear"
        Me.butClear.Size = New System.Drawing.Size(75, 23)
        Me.butClear.TabIndex = 5
        Me.butClear.Text = "Clear"
        Me.butClear.UseVisualStyleBackColor = True
        Me.dgvMain.AllowUserToAddRows = False
        Me.dgvMain.AllowUserToDeleteRows = False
        Me.dgvMain.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.dgvMain.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.dgvMain.Location = New System.Drawing.Point(12, 129)
        Me.dgvMain.Name = "dgvMain"
        Me.dgvMain.Size = New System.Drawing.Size(346, 149)
        Me.dgvMain.TabIndex = 6
        Me.ckOrderColumnsByList.AutoSize = True
        Me.ckOrderColumnsByList.Location = New System.Drawing.Point(226, 35)
        Me.ckOrderColumnsByList.Name = "ckOrderColumnsByList"
        Me.ckOrderColumnsByList.Size = New System.Drawing.Size(141, 17)
        Me.ckOrderColumnsByList.TabIndex = 7
        Me.ckOrderColumnsByList.Text = "Order columns by the list"
        Me.ckOrderColumnsByList.UseVisualStyleBackColor = True
        Me.ckSortListBox.AutoSize = True
        Me.ckSortListBox.Location = New System.Drawing.Point(226, 12)
        Me.ckSortListBox.Name = "ckSortListBox"
        Me.ckSortListBox.Size = New System.Drawing.Size(77, 17)
        Me.ckSortListBox.TabIndex = 8
        Me.ckSortListBox.Text = "Sort listbox"
        Me.ckSortListBox.UseVisualStyleBackColor = True
        Me.ckAddMultipleEntries.AutoSize = True
        Me.ckAddMultipleEntries.Checked = True
        Me.ckAddMultipleEntries.CheckState = System.Windows.Forms.CheckState.Checked
        Me.ckAddMultipleEntries.Location = New System.Drawing.Point(226, 58)
        Me.ckAddMultipleEntries.Name = "ckAddMultipleEntries"
        Me.ckAddMultipleEntries.Size = New System.Drawing.Size(110, 17)
        Me.ckAddMultipleEntries.TabIndex = 9
        Me.ckAddMultipleEntries.Text = "Add multiple items"
        Me.ckAddMultipleEntries.UseVisualStyleBackColor = True
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.CancelButton = Me.butQuit
        Me.ClientSize = New System.Drawing.Size(370, 290)
        Me.MinimumSize = New System.Drawing.Size(240, 34)
        Me.Name = "frmBooleanOutCome"
        Me.Text = "Boolean Outcome"
        CType(Me.dgvMain, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub
    Friend WithEvents lstItems As System.Windows.Forms.ListBox
    Friend WithEvents butGo As System.Windows.Forms.Button
    Friend WithEvents butQuit As System.Windows.Forms.Button
    Friend WithEvents butAdd As System.Windows.Forms.Button
    Friend WithEvents butRemove As System.Windows.Forms.Button
    Friend WithEvents butClear As System.Windows.Forms.Button
    Friend WithEvents dgvMain As System.Windows.Forms.DataGridView
    Friend WithEvents ckOrderColumnsByList As System.Windows.Forms.CheckBox
    Friend WithEvents ckSortListBox As System.Windows.Forms.CheckBox
    Friend WithEvents ckAddMultipleEntries As System.Windows.Forms.CheckBox
#End Region

    Private lstItemsAddedOrder As New List(Of String)

    Private Sub butAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAdd.Click
        Dim sUserInput As String = InputBox("Enter the name to associate you'd like to add", "User Defined Fields")

        If sUserInput.Trim IsNot String.Empty AndAlso Not Me.lstItems.Items.Contains(sUserInput.Trim) Then
            If dgvMain.RowCount > 0 Then dgvMain.Rows.Clear()
            ckOrderColumnsByList_Click(sender, e)
            Exit Sub
        End If

        If Me.ckAddMultipleEntries.Checked Then butAdd_Click(sender, e)
        If Not Me.butGo.Enabled Then Me.butGo.Enabled = lstItems.Items.Count > 0
        If Not Me.butClear.Enabled Then Me.butClear.Enabled = lstItems.Items.Count > 0
    End Sub

    Private Sub butClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butClear.Click

        If dgvMain.RowCount > 0 Then dgvMain.Rows.Clear()
        If dgvMain.Columns.Count > 0 Then dgvMain.Columns.Clear()
        If Me.butGo.Enabled Then Me.butGo.Enabled = False
        If Me.butClear.Enabled Then Me.butClear.Enabled = False
        If Me.butRemove.Enabled Then Me.butRemove.Enabled = False
    End Sub

    Private Sub butRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butRemove.Click
        If Me.lstItems.SelectedIndices.Count > 0 Then
            If dgvMain.RowCount > 0 Then dgvMain.Rows.Clear()
            Do While lstItems.SelectedIndices.Count > 0
            ckOrderColumnsByList_Click(sender, e)
        End If
        If Me.butGo.Enabled Then Me.butGo.Enabled = lstItems.Items.Count > 0
        If Me.butClear.Enabled Then Me.butClear.Enabled = lstItems.Items.Count > 0
    End Sub

    Private Sub ckSortListBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckSortListBox.CheckedChanged
        Me.lstItems.Sorted = Me.ckSortListBox.Checked

        If Not Me.lstItems.Sorted Then
                For Each value As String In Me.lstItemsAddedOrder
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.