Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Listview Control

Status
Not open for further replies.

TotalInsanity

Programmer
Oct 18, 2006
20
0
0
GB
I have created a listview in my C# form and when I add an item through my code I change the background colour of my cells/rows. However, when I move over them at runtime with my mouse, the colour defaults back to white and then they stay white (when they should be red!).

The highlighted one is ok however (when clicking with mouse), it's just the background colour rows I have changed that are a problem. They are displayed with a red background colour when the form is displayed and when I move my mouse over the form they are OK. They vanish when I move my mouse over the listview control???

Is this simply a glitch in VS? I have no idea how to fix it, Can anyone help me out please??

When I enter items into the listview I am using the following code.

Code:
//get messages
            DataTable dTable = this.m_mainToolbarForm.AllMessagesDataSet.Tables["Table"];
            // Clear the ListView control
            listBox1.Items.Clear();

            // Attach Subitems to the ListView
            listBox1.Columns.Add("Priority", 50, HorizontalAlignment.Center);
            listBox1.Columns.Add("Message Title", 220, HorizontalAlignment.Left);
            listBox1.Columns.Add("Date", 120, HorizontalAlignment.Left);

            // Display items in the ListView control
            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                DataRow drow = dTable.Rows[i];

                // Only row that have not been deleted
                if (drow.RowState != DataRowState.Deleted)
                {
                    // Add a ListItem object to the ListView.
                    ListViewItem messageItem = listBox1.Items.Add(drow["Message_Priority"].ToString());

                    messageItem.UseItemStyleForSubItems = false;
                    // Add the message title subitem.
                    ListViewItem.ListViewSubItem messageTitle = messageItem.SubItems.Add(drow["Message_Title"].ToString());
                    // Add the message date subitem.
                    ListViewItem.ListViewSubItem messageDate = messageItem.SubItems.Add(drow["Message_Added"].ToString());
                    //check if message high priority or not and highlight accordingly
                    if (drow["Message_Priority"].ToString() == "1")
                    {
                        messageItem.BackColor = Color.Red;
                        messageTitle.BackColor = Color.Red;
                        messageDate.BackColor = Color.Red; 
                    }
                    else
                    {
                        messageItem.BackColor = Color.White;
                        messageTitle.BackColor = Color.White;
                        messageDate.BackColor = Color.White;                   
                    }

                }
            }

any help at all would be vastly appreciated!
 
try commenting out the 3 lines that set the item to white. Its possible this could be fired after the item is initially added. And the second time around, since you are comparing a number as a string, the string could come out like " 1" or "1 ", etc.
 
I have now sorted this out. It is a bug with the control.

The only workaround is to use the Windows XP visual styles to fix the problem.

first define:

Code:
using System.Windows.Forms.VisualStyles;

then use
Code:
Application.EnableVisualStyles();

before calling the standard
Code:
Application.Run();

The workaround is good for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top