Partial Public Class SelectorBoxCategory
    Inherits System.Web.UI.UserControl
    Private objChoices As System.Web.UI.WebControls.ListItemCollection
    Private objSelected As System.Web.UI.WebControls.ListItemCollection
    Private strTheSelectedValues As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not objChoices Is Nothing Then
            For Each selItem In objChoices
                lstChoices.Items.Add(selItem)
            Next
        End If
        If Not objSelected Is Nothing Then
            For Each selItem In objSelected
                lstSelected.Items.Add(selItem)
            Next
        End If
    End Sub
    Public Sub btnAdd_Click()
        Dim parentFlag As New Integer

        For intCount = lstChoices.Items.Count - 1 To 0 Step -1
            Dim selItem As System.Web.UI.WebControls.ListItem
            selItem = lstChoices.Items(intCount)
            Dim li = lstChoices.Items.FindByValue(selItem.Value)
            If li.Selected = True Then
                Dim liSelected = lstSelected.Items.FindByValue(selItem.Value)
                If liSelected Is Nothing Then
                    Dim selItemAdd As New System.Web.UI.WebControls.ListItem
                    selItemAdd.Value = li.Value
                    ' Chr(187) is the double >> symbol
                    ' Remove the leading >> symbol in the Selected listbox
                    selItemAdd.Text = Replace(li.Text, Chr(187), "")
                    selItemAdd.Attributes.CssStyle.Add("padding-left", "10px")
                    'if it has a parent category add the parent in the menu listfirst
                    Dim selParentItemAdd As New System.Web.UI.WebControls.ListItem
                    Dim parentID As String
                    Dim valueArray As Array
                    valueArray = Split(selItem.Value, ",")
                    parentID = Convert.ToInt32(valueArray(1))
                    If parentID > 0 Then
                        If parentFlag <> parentID Then
                            Dim objCategory As New DAL.dbMainCategory
                            Dim parentCategory As New Entities.entmainCategory
                            parentCategory = objCategory.GetItem(parentID)
                            Dim selItemAddParent As New System.Web.UI.WebControls.ListItem
                            selItemAddParent.Value = parentCategory.CategoryID & "," & parentCategory.ParentCategoryID
                            selItemAddParent.Text = Replace(parentCategory.CategoryName, Chr(187), "")
                            selItemAddParent.Attributes.CssStyle.Add("font-style", "italic")
                            selItemAddParent.Attributes.CssStyle.Add("font-weight", "bold")
                            lstSelected.Items.Add(selItemAddParent)
                            parentFlag = parentID
                        End If
                    ElseIf Convert.ToInt32(valueArray(1)) = 0 Then
                        selItemAdd.Attributes.CssStyle.Clear()
                        selItemAdd.Attributes.CssStyle.Add("font-style", "italic")
                        selItemAdd.Attributes.CssStyle.Add("font-weight", "bold")
                        lstChoices.Items.Remove(li)
                    End If
                    lstSelected.Items.Add(selItemAdd)
                    If parentID <> 0 Then
                        lstChoices.Items.Remove(li)
                    End If
                End If
            End If
            Dim parentArray As Array
            Dim parentIDFormat As New Integer
            parentArray = Split(li.Value, ",")
            parentIDFormat = Convert.ToInt32(parentArray(1))
            If parentIDFormat = 0 Then
                li.Attributes.CssStyle.Add("font-style", "italic")
                li.Attributes.CssStyle.Add("font-weight", "bold")
            Else
                li.Attributes.CssStyle.Add("padding-left", "10px")
            End If
        Next intCount

        lstChoices.ClearSelection()


       
       
    End Sub
    Public Sub btnDel_Click()
        For intCount = lstSelected.Items.Count - 1 To 0 Step -1
            Dim selItem As System.Web.UI.WebControls.ListItem
            selItem = lstSelected.Items(intCount)
            Dim li = lstSelected.Items.FindByValue(selItem.Value)
            Dim parentArray As Array
            Dim parentIDFormat As New Integer
            parentArray = Split(li.Value, ",")
            parentIDFormat = Convert.ToInt32(parentArray(1))
            If li.Selected = True Then
                If parentIDFormat <> 0 Then
                    lstChoices.Items.Add(li)
                End If


                lstSelected.Items.Remove(li)
            End If

            If parentIDFormat = 0 Then
                li.Attributes.CssStyle.Add("font-style", "italic")
                li.Attributes.CssStyle.Add("font-weight", "bold")
            Else
                li.Attributes.CssStyle.Add("padding-left", "10px")
            End If
        Next intCount
        For Each selItem In lstChoices.Items
            selItem.Selected = False
        Next
        lstSelected.ClearSelection()
        For intCount = lstChoices.Items.Count - 1 To 0 Step -1
            Dim selItem As System.Web.UI.WebControls.ListItem
            selItem = lstChoices.Items(intCount)
            Dim li = lstChoices.Items.FindByValue(selItem.Value)

            Dim parentArray As Array
            Dim parentIDFormat As New Integer
            parentArray = Split(li.Value, ",")
            parentIDFormat = Convert.ToInt32(parentArray(1))
            If parentIDFormat = 0 Then
                li.Attributes.CssStyle.Add("font-style", "italic")
                li.Attributes.CssStyle.Add("font-weight", "bold")
            Else
                li.Attributes.CssStyle.Add("padding-left", "10px")
            End If
        Next intCount
    End Sub

#Region "Public properties"
    Public Property TheChoices() As System.Web.UI.WebControls.ListItemCollection
        Get
            Return objChoices
        End Get
        Set(ByVal value As System.Web.UI.WebControls.ListItemCollection)
            objChoices = value
        End Set
    End Property
    Public Property TheSelected() As System.Web.UI.WebControls.ListItemCollection
        Get
            Dim objListCollection As New System.Web.UI.WebControls.ListItemCollection
            Dim selItem As System.Web.UI.WebControls.ListItem
            For Each selItem In lstSelected.Items
                objListCollection.Add(selItem)
            Next
            Return objListCollection
        End Get
        Set(ByVal value As System.Web.UI.WebControls.ListItemCollection)
            objSelected = value
        End Set
    End Property
    Public Property TheSelectedValuesCSV() As String
        Get
            Dim strOutput As String = ""
            For Each selItem In TheSelected
                ' Save it items stored in the the Selected listbox
                strOutput += selItem.Value & Chr(167)
            Next
            If Right(strOutput, 1) = Chr(167) Then
                strOutput = Left(strOutput, (Len(strOutput) - 1))
            End If
            Return strOutput
        End Get
        Set(ByVal value As String)
            strTheSelectedValues = value
        End Set
    End Property
#End Region
End Class