ListView控件(在ListView失去焦点的情况下仍然保持Item高亮)

 ListView控件(在ListView失去焦点的情况下仍然保持Item高亮)
 
添加列:
在“属性”窗口中,将该控件的 View 属性设置为 Details。

在“属性”窗口中,单击 Columns 属性旁的“省略号”按钮 ()。

出现“ColumnHeader 集合编辑器”。

使用“添加”按钮添加新列。然后可以选择列标题并设置其文本(列的标题文字)、文本对齐方式和宽度。

 

 

ExpandedBlockStart.gif 代码
         private   void  button2_Click( object  sender, EventArgs e)
        {
            
if  (listView1.SelectedItems.Count  >   0 )
                MessageBox.Show(listView1.SelectedItems[
0 ].Text);
            
else
                MessageBox.Show(
" 请选择 " ); 
        }

        
private   void  ListviewDemo_Load( object  sender, EventArgs e)
        {
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " A " " a "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " B " " b "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " C " " c "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " D " " d "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " A " " a "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " B " " b "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " C " " c "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " D " " d "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " A " " a "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " B " " b "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " C " " c "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " D " " d "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " A " " a "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " B " " b "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " C " " c "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " D " " d "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " A " " a "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " B " " b "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " C " " c "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " D " " d "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " A " " a "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " B " " b "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " C " " c "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " D " " d "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " A " " a "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " B " " b "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " C " " c "  }));
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " D " " d "  }));
        }

        
private   void  toolStripMenuItem1_Click( object  sender, EventArgs e)
        {
            listView1.Items.Add(
new  ListViewItem( new   string [] {  " 添加 " " 添加 "  }));
        }

        
private   void  toolStripMenuItem2_Click( object  sender, EventArgs e)
        {
            MessageBox.Show(
" 删除 "   +  listView1.SelectedItems[ 0 ].Text);
            listView1.SelectedItems[
0 ].Remove();
        }

        
#region   在ListView失去焦点的情况下仍然保持Item高亮
        
private   void  listView_Validated( object  sender, EventArgs e)
        {
            
if  (listView1.FocusedItem  !=   null )
            {
                listView1.FocusedItem.BackColor 
=  SystemColors.Highlight;
                listView1.FocusedItem.ForeColor 
=  Color.White;
                listView1.SelectedIndices.Add(listView1.FocusedItem.Index);
// **真正让其选择
            }
        }

        
private   void  listView_ItemSelectionChanged( object  sender, ListViewItemSelectionChangedEventArgs e)
        {
            e.Item.ForeColor 
=  Color.Black;
            e.Item.BackColor 
=  SystemColors.Window;

            
if  (listView1.FocusedItem  !=   null )
            {
                listView1.FocusedItem.Selected 
=   true ;
            }
        }
        
#endregion

 

 

转载于:https://www.cnblogs.com/jdmei520/archive/2010/03/16/1685150.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值