שתף באמצעות


change color of one item in listbox

Question

Saturday, September 17, 2011 5:05 AM

 The code below is from your forum and it gives the error: missingmemberexception?

       For x3 = 0 To lstStock.Items.Count - 1
            If lstStock.Items(x3) = LastStockCode1 Then
                lstStock.Items(x3).attributes.add("style", "color:red")
                Exit For
            End If
        Next x3

I have a list box display on a form. I want to change the forground color of one item using item index.

The code "lststock.item(x3).attributes.add("style", "color:red")" does not work.

One solution was two pages of code! This s/b simple. I have the INDEX of the list item. I just want to change the words in that item

from the color at list build time to red to highlite it!

Can any one give me a line or two of code to do this?

james fleming

All replies (4)

Saturday, September 17, 2011 7:36 AM ✅Answered

Hi,

Try to use a listview control. I hope you can do what you want.

Regards,

A.Murugan.

If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!


Saturday, September 17, 2011 5:33 AM

is your ListBox a standard ListBox from the ToolBox, or is it a custom control?

the standard ListBox does not have an attribute property, so what you ask cannot be done in one or two lines of code

you could use another control that allows formatting of individual items, such as a RichTextBox or DataGridView

 


Saturday, September 17, 2011 7:47 AM

Hi Jim I made following code for you

 

try it in your list box I am using windows listBox control here.Set the Draw mode property of your list box to "OwnerDrawFixed"

 

 Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

        Dim myBrush As Brush = Brushes.Black

        Select Case e.Index

            Case 3

                myBrush = Brushes.Red

            Case Else

                myBrush = Brushes.Black

        End Select

        e.DrawBackground()

        e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _

              e.Font, myBrush, e.Bounds, StringFormat.GenericDefault)

    End Sub

 

Please let me know your results.

 

Want to add MVP with my name.


Monday, September 19, 2011 10:57 AM

Hi Jim,

Welcome to MSDN forum.

Have you ever solved your question?  I think Jwavila has given you the correct answer. Would you mind to telling more information about your code? If your work it for practicing List box Control, Bahushekh has provided the sample code. If not, other controls can meet your need more successfully. You can compare them with the list box, and make your choice.

Here is some information about controls.

ListBox: http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx
Contains a list of selectable items. ListBox is an ItemsControl, which means it can contain a collection of objects of any type (such as string, image, or panel). For more information, see the ItemsControl class.

RichTextBox: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx
Represents a Windows rich text box control. With this, you can enter and edit text. The control also provides more advanced formatting features than the standard Textbox control.

DataGridView: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx
Displays data in a customizable grid. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties.

If you have any concerns, please feel free to let me know.

Mark lxf [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.