如何覆盖列表视图中所选行的前景色 [英] How to override the forecolor of a selected row in a listview
问题描述
我有一个列表视图,其中一些前景颜色是红色的,具体取决于某些标准。我注意到,当选择一行时,前景色为黑色,无论未选择时前景是否为红色。当选择与未选择时相同的颜色时,我如何才能使行的前景颜色为
?我在selectedindexchanged事件处理程序中尝试了类似的东西,它没有工作
I have a listview in which some of the rows forecolor are red depending on certain criteria. I noticed that while a row is selected the forecolor is black regardless if the forecolor is red when not selected. How would I be able to make the row's forecolor when selected the same color as when not selected? I tried something like this in the selectedindexchanged event handler and it did not work
if (listView1.SelectedItems.Count > 0)
{
listView1.SelectedItems[0].ForeColor = System.Drawing.Color.Blue;
}
我调试了它并且它通过了代码,但它仍然没有在选择之前将forecolor更改为颜色。如果未选中,我怎样才能使所选行的前景色与行相同?
I debugged it and it went through the code but it still did not change the forecolor to color it was before selecting. How would I be able to make the forecolor of the selected row be the same forecolor as the row when not selected?
Debra有问题
推荐答案
Hello Debra,
Hello Debra,
我对此问题进行了测试, ForeColor按预期更改。怎么样通过一些代码或图像向我展示你的结果?
I have a test about this issue and the ForeColor is changed as expected. What about show me your result by some more code or image?
这是我的测试样本:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TextboxTest
{
public partial class ListViewcs : Form
{
ListView listView1 = new ListView();
public ListViewcs()
{
InitializeComponent();
// Create a new ListView control.
listView1.Bounds = new Rectangle(new Point(10, 10), new Size(300, 200));
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1", 0);
// Place a check mark next to the item.
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2", 1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3", 0);
// Place a check mark next to the item.
item3.Checked = true;
item3.SubItems.Add("7");
item3.SubItems.Add("8");
item3.SubItems.Add("9");
// Create columns for the items and subitems.
// Width of -2 indicates auto-size.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);
//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
this.Controls.Add(listView1);
}
private void button1_Click(object sender, EventArgs e)
{
listView1.SelectedItems[0].ForeColor = System.Drawing.Color.Red;
}
}
}
这是我的结果:
那时候你没有选择任何项目,所以颜色没有变化吗?
Will it be that you haven't select any item at that time so the color doesnot changed?
问候,
这篇关于如何覆盖列表视图中所选行的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!