+ Reply to Thread
Results 1 to 6 of 6

multiselect listbox backcolor

  1. #1
    Russ
    Guest

    multiselect listbox backcolor

    I have a form with several textboxes, comboboxes and multiselect listboxes.
    I would like to change the backcolor of each without changing the value in
    each. A simple statement like:
    Me.textbox1.backcolor = VbRed changes the backcolor of textboxes without
    changing the textbox.value
    Me.combobox1.backcolor = VbRed changes the backcolor of comboxes without
    changing the combobox.value
    However, Me.listbox1.backcolor = VbRed changes the backcolor of multiselect
    listboxes but also resets the listbox1 to no selections.

    Does anyone know a way to change the color of a multiselect listbox and keep
    its selections without a lot of code to remember the array settings?

    --
    russ

  2. #2
    Dave Peterson
    Guest

    Re: multiselect listbox backcolor

    Not too much code:

    Option Explicit
    Private Sub CommandButton1_Click()
    Dim myArr() As Boolean
    Dim iCtr As Long
    ReDim myArr(0 To Me.ListBox1.ListCount - 1)

    For iCtr = 0 To Me.ListBox1.ListCount - 1
    myArr(iCtr) = Me.ListBox1.Selected(iCtr)
    Next iCtr
    Me.ListBox1.BackColor = vbRed

    For iCtr = 0 To Me.ListBox1.ListCount - 1
    Me.ListBox1.Selected(iCtr) = myArr(iCtr)
    Next iCtr

    End Sub
    'for testing
    Private Sub UserForm_Initialize()
    Dim iCtr As Long
    With Me.ListBox1
    .MultiSelect = fmMultiSelectMulti
    For iCtr = 1 To 10
    .AddItem iCtr
    Next iCtr
    End With
    End Sub



    Russ wrote:
    >
    > I have a form with several textboxes, comboboxes and multiselect listboxes.
    > I would like to change the backcolor of each without changing the value in
    > each. A simple statement like:
    > Me.textbox1.backcolor = VbRed changes the backcolor of textboxes without
    > changing the textbox.value
    > Me.combobox1.backcolor = VbRed changes the backcolor of comboxes without
    > changing the combobox.value
    > However, Me.listbox1.backcolor = VbRed changes the backcolor of multiselect
    > listboxes but also resets the listbox1 to no selections.
    >
    > Does anyone know a way to change the color of a multiselect listbox and keep
    > its selections without a lot of code to remember the array settings?
    >
    > --
    > russ


    --

    Dave Peterson

  3. #3
    Jim Rech
    Guest

    Re: multiselect listbox backcolor

    >>Does anyone know a way to change the color of a multiselect listbox and
    >>keep its selections without a lot of code to remember the array settings?


    No, but it's not that much code to get and set the selected items:

    Dim SelectedArray() As Integer

    Private Sub UserForm_Initialize()
    Dim x As Integer
    For x = 1 To 10
    Me.ListBox1.AddItem x
    Next
    End Sub

    Private Sub CommandButton1_Click()
    SetSelectedArray
    Me.ListBox1.BackColor = vbRed
    ResetListboxSelectedItems
    End Sub

    Sub SetSelectedArray()
    Dim Counter As Integer, ArrayCounter As Integer
    Erase SelectedArray
    For Counter = 1 To Me.ListBox1.ListCount
    If Me.ListBox1.Selected(Counter - 1) Then
    ArrayCounter = ArrayCounter + 1
    ReDim Preserve SelectedArray(1 To ArrayCounter)
    SelectedArray(ArrayCounter) = Counter - 1
    End If
    Next
    End Sub

    Sub ResetListboxSelectedItems()
    Dim Counter As Integer
    For Counter = 1 To UBound(SelectedArray)
    Me.ListBox1.Selected(SelectedArray(Counter)) = True
    Next
    End Sub


    --
    Jim
    "Russ" <Russ@discussions.microsoft.com> wrote in message
    news:447388AA-020A-4AC0-A37B-68C6A8AA99EA@microsoft.com...
    |I have a form with several textboxes, comboboxes and multiselect listboxes.
    | I would like to change the backcolor of each without changing the value in
    | each. A simple statement like:
    | Me.textbox1.backcolor = VbRed changes the backcolor of textboxes without
    | changing the textbox.value
    | Me.combobox1.backcolor = VbRed changes the backcolor of comboxes without
    | changing the combobox.value
    | However, Me.listbox1.backcolor = VbRed changes the backcolor of
    multiselect
    | listboxes but also resets the listbox1 to no selections.
    |
    | Does anyone know a way to change the color of a multiselect listbox and
    keep
    | its selections without a lot of code to remember the array settings?
    |
    | --
    | russ



  4. #4
    Russ
    Guest

    Re: multiselect listbox backcolor

    Thanks Jim - works fine and not too much code as you said
    --
    russ


    "Jim Rech" wrote:

    > >>Does anyone know a way to change the color of a multiselect listbox and
    > >>keep its selections without a lot of code to remember the array settings?

    >
    > No, but it's not that much code to get and set the selected items:
    >
    > Dim SelectedArray() As Integer
    >
    > Private Sub UserForm_Initialize()
    > Dim x As Integer
    > For x = 1 To 10
    > Me.ListBox1.AddItem x
    > Next
    > End Sub
    >
    > Private Sub CommandButton1_Click()
    > SetSelectedArray
    > Me.ListBox1.BackColor = vbRed
    > ResetListboxSelectedItems
    > End Sub
    >
    > Sub SetSelectedArray()
    > Dim Counter As Integer, ArrayCounter As Integer
    > Erase SelectedArray
    > For Counter = 1 To Me.ListBox1.ListCount
    > If Me.ListBox1.Selected(Counter - 1) Then
    > ArrayCounter = ArrayCounter + 1
    > ReDim Preserve SelectedArray(1 To ArrayCounter)
    > SelectedArray(ArrayCounter) = Counter - 1
    > End If
    > Next
    > End Sub
    >
    > Sub ResetListboxSelectedItems()
    > Dim Counter As Integer
    > For Counter = 1 To UBound(SelectedArray)
    > Me.ListBox1.Selected(SelectedArray(Counter)) = True
    > Next
    > End Sub
    >
    >
    > --
    > Jim
    > "Russ" <Russ@discussions.microsoft.com> wrote in message
    > news:447388AA-020A-4AC0-A37B-68C6A8AA99EA@microsoft.com...
    > |I have a form with several textboxes, comboboxes and multiselect listboxes.
    > | I would like to change the backcolor of each without changing the value in
    > | each. A simple statement like:
    > | Me.textbox1.backcolor = VbRed changes the backcolor of textboxes without
    > | changing the textbox.value
    > | Me.combobox1.backcolor = VbRed changes the backcolor of comboxes without
    > | changing the combobox.value
    > | However, Me.listbox1.backcolor = VbRed changes the backcolor of
    > multiselect
    > | listboxes but also resets the listbox1 to no selections.
    > |
    > | Does anyone know a way to change the color of a multiselect listbox and
    > keep
    > | its selections without a lot of code to remember the array settings?
    > |
    > | --
    > | russ
    >
    >
    >


  5. #5
    Russ
    Guest

    Re: multiselect listbox backcolor

    Thanks Dave - that does the trick.
    --
    russ


    "Dave Peterson" wrote:

    > Not too much code:
    >
    > Option Explicit
    > Private Sub CommandButton1_Click()
    > Dim myArr() As Boolean
    > Dim iCtr As Long
    > ReDim myArr(0 To Me.ListBox1.ListCount - 1)
    >
    > For iCtr = 0 To Me.ListBox1.ListCount - 1
    > myArr(iCtr) = Me.ListBox1.Selected(iCtr)
    > Next iCtr
    > Me.ListBox1.BackColor = vbRed
    >
    > For iCtr = 0 To Me.ListBox1.ListCount - 1
    > Me.ListBox1.Selected(iCtr) = myArr(iCtr)
    > Next iCtr
    >
    > End Sub
    > 'for testing
    > Private Sub UserForm_Initialize()
    > Dim iCtr As Long
    > With Me.ListBox1
    > .MultiSelect = fmMultiSelectMulti
    > For iCtr = 1 To 10
    > .AddItem iCtr
    > Next iCtr
    > End With
    > End Sub
    >
    >
    >
    > Russ wrote:
    > >
    > > I have a form with several textboxes, comboboxes and multiselect listboxes.
    > > I would like to change the backcolor of each without changing the value in
    > > each. A simple statement like:
    > > Me.textbox1.backcolor = VbRed changes the backcolor of textboxes without
    > > changing the textbox.value
    > > Me.combobox1.backcolor = VbRed changes the backcolor of comboxes without
    > > changing the combobox.value
    > > However, Me.listbox1.backcolor = VbRed changes the backcolor of multiselect
    > > listboxes but also resets the listbox1 to no selections.
    > >
    > > Does anyone know a way to change the color of a multiselect listbox and keep
    > > its selections without a lot of code to remember the array settings?
    > >
    > > --
    > > russ

    >
    > --
    >
    > Dave Peterson
    >


  6. #6
    Russ
    Guest

    Re: multiselect listbox backcolor

    Thanks Dave - and not too much code. Works like a charm.
    --
    russ


    "Dave Peterson" wrote:

    > Not too much code:
    >
    > Option Explicit
    > Private Sub CommandButton1_Click()
    > Dim myArr() As Boolean
    > Dim iCtr As Long
    > ReDim myArr(0 To Me.ListBox1.ListCount - 1)
    >
    > For iCtr = 0 To Me.ListBox1.ListCount - 1
    > myArr(iCtr) = Me.ListBox1.Selected(iCtr)
    > Next iCtr
    > Me.ListBox1.BackColor = vbRed
    >
    > For iCtr = 0 To Me.ListBox1.ListCount - 1
    > Me.ListBox1.Selected(iCtr) = myArr(iCtr)
    > Next iCtr
    >
    > End Sub
    > 'for testing
    > Private Sub UserForm_Initialize()
    > Dim iCtr As Long
    > With Me.ListBox1
    > .MultiSelect = fmMultiSelectMulti
    > For iCtr = 1 To 10
    > .AddItem iCtr
    > Next iCtr
    > End With
    > End Sub
    >
    >
    >
    > Russ wrote:
    > >
    > > I have a form with several textboxes, comboboxes and multiselect listboxes.
    > > I would like to change the backcolor of each without changing the value in
    > > each. A simple statement like:
    > > Me.textbox1.backcolor = VbRed changes the backcolor of textboxes without
    > > changing the textbox.value
    > > Me.combobox1.backcolor = VbRed changes the backcolor of comboxes without
    > > changing the combobox.value
    > > However, Me.listbox1.backcolor = VbRed changes the backcolor of multiselect
    > > listboxes but also resets the listbox1 to no selections.
    > >
    > > Does anyone know a way to change the color of a multiselect listbox and keep
    > > its selections without a lot of code to remember the array settings?
    > >
    > > --
    > > russ

    >
    > --
    >
    > Dave Peterson
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1