方法一:
1.ListView的HideSelection属性设置为True。
2.ListView的Validated事件处理
/// <summary>
/// 失去焦点事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lvSeries_Validated(object sender, EventArgs e)
{
try
{
if (lvSeries.FocusedItem != null)
{
lvSeries.FocusedItem.BackColor = SystemColors.Highlight;
lvSeries.FocusedItem.ForeColor = Color.White;
lvSeries.SelectedIndices.Add(lvSeries.FocusedItem.Index);
}
}
catch (Exception eEx)
{
MessageBox.Show(eEx.Message);
}
}
3.ListView的ItemSelectionChanged事件处理
/// <summary>
/// 选择变化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lvSeries_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
try
{
e.Item.ForeColor = Color.Black;
e.Item.BackColor = SystemColors.Window;
if (lvSeries.FocusedItem != null)
{
lvSeries.FocusedItem.Selected = true;
}
}
catch (Exception eEx)
{
MessageBox.Show(eEx.Message);
}
}
方法二:只能单选行功能,并排除方法一中的不易发现的Bug
this.listview1.Validated += new System.EventHandler(this.lvSeries_Validated);
this.listview1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lvSeries_MouseDown);
/// <summary>
/// 失去焦点事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lvSeries_Validated(object sender, EventArgs e)
{
try
{
if (lvSeries.FocusedItem != null)
{
lvSeries.FocusedItem.BackColor = SystemColors.Highlight;
lvSeries.FocusedItem.ForeColor = Color.White;
}
}
catch (Exception eEx)
{
MessageBox.Show(eEx.Message);
}
}
/// <summary>
/// 重新选择行事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lvSeries_MouseDown(object sender, MouseEventArgs e)
{
try
{
ListViewItem _curItem = this.lvSeries.GetItemAt(e.X, e.Y);
foreach (ListViewItem item in lvSeries.Items)
{
item.ForeColor = Color.Black;
item.BackColor = Color.White;
}
if (_curItem != null && _curItem.Index > -1)
{
_curItem.BackColor = SystemColors.Highlight;
_curItem.ForeColor = Color.White;
}
else
{
if (lvSeries.FocusedItem != null)
{
lvSeries.FocusedItem.BackColor = SystemColors.Highlight;
lvSeries.FocusedItem.ForeColor = Color.White;
}
}
}
catch (Exception eEx)
{
MessageBox.Show(eEx.Message);
}
}
粘自:http://www.cnblogs.com/fanyf/archive/2011/12/07/2278957.html