Click here to Skip to main content
16,016,562 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So what i am trying to achieve, is when i select a full row in a listview and i press a button the text of that row i have selected changes to a colour i have specifed.

i have this code at current, to change the colour of a specific row, which is the first row, but i want to try and applly it to a if loop to say if a specific row is selected then change the colour. The code i have is below.

Please help me

C#
BookingsListView.Items[0].BackColor = Color.Red;
Posted
Updated 16-Dec-13 0:23am
v2

Good point Bill,

I had a little play around.

If you declare a global ListViewRow variable and assign it to the selected row in the ItemSelectionChanged Event
C#
ListViewItem selectedItem;
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
    selectedItem = e.item;
}


Then you can change the ForeColor from your button_Click Event by getting the index of the selected row

C#
private void button1_Click(object sender, EventArgs e)
 {

     int index = listView1.Items.IndexOf(selectedItem);
     listView1.Items[index].ForeColor = Color.Red;

 }


Sorry should be ListViewItem not ListViewRow
 
Share this answer
 
v4
Comments
Member 9665875 16-Dec-13 6:15am    
what i am after is, the user to select a row in the listview and when that row is selected channge the colour of that row using a button :/
The standard MS ListView Control for WinForms will use the default color set by your Windows Theme to display selected and unselected rows' BackColor. And, the ListView color behavior used will vary depending on whether the ListView Control has focus.

There's a good analysis of this issue here: [^].

So, as you may have noted, your custom BackColor setting for a ListView row may be ignored.

Your choices are to creates your own custom ListView and do all the painting yourself (prepare to be busy for a long time), or, you could use the excellent custom ListView here on CodeProject by Philip Piper, which is now up to version 2.6 as of July. 2012 [^].

Philip has been developing this ListView since 2006, and is still actively responding to CP members' questions !
 
Share this answer
 
v2
Comments
Member 9665875 16-Dec-13 6:30am    
so theres no easy way, to select any row and that row changes colour within a listview
read the model and modelless class
dialogue result its easy and write if condition of dailogue result on your button event
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900