c# listview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace installfirstproject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//labeledit指標子項標籤是否可以編輯
listView1.LabelEdit = true;
listView1.Items.Add("a");
listView1.Items.Add("b");
}
//新增子項
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text =="")
{
MessageBox.Show("要新增到LISTVIEW的項為空");
}
else
{
listView1.Items.Add(textBox1.Text.Trim());
//listview各個子項的顯示方式,經測試僅為list;每個項左邊才會完整顯示checkbox核取方塊
//經測試如果子項顯示方式為list且子項左側為核取方塊時,button2_click選中子項進行刪除某子項不起作用,註解掉對比測試下
//listView1.View = View.List;
//listView1.CheckBoxes = true;
try
{
//判斷子項個數
if (listView1.Items.Count < 2)
{
listView1.Items.Add("給力");
}
else
{
//selected指標是否選中某個子項
listView1.Items[2].Selected = true;
}
}
catch(Exception ec)
{
MessageBox.Show(ec.Message);
}
textBox1.Clear();
}
}
private void button2_Click(object sender, EventArgs e)
{
//刪除前判斷是否有子項,否則刪除會報異常;在listview中,類似於資源管理器右視窗
if (listView1.Items.Count == 0)
{
MessageBox.Show("不用刪除,沒有子項");
return;
}
//else if (listView1.SelectedItems.Count == 0)
//{
// MessageBox.Show("有子項,但當前沒有選中子項");
//}
else
{
//選中了子項
//經測試selecteditems.clear方法失效
// listView1.SelectedItems.Clear();
// MessageBox.Show("已成功刪除子項"+listView1.SelectedItems[listView1.SelectedItems[0].Index]);
//移除所有子項
//listView1.Items.Clear();
//下列方法是取消選擇吧
listView1.SelectedItems.Clear();
//remove為刪除指定子項,方法引數listviewitem
// listView1.Items.Remove();
listView1.Items.RemoveAt(0);
//int index = listView1.SelectedItems.IndexOf(listView1.SelectedItems);
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-717257/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C# generate listview and fill data no xamlC#View
- C# ListView用法詳解 很完整C#View
- Listview 取選中的值 C#ViewC#
- c# listview 刪除選中子項C#View
- ListView選中selectedItem上下移動 C#ViewC#
- C#如何獲取ListView控制元件選中項的值C#View控制元件
- ListViewView
- 安卓 listview安卓View
- QML::ListViewView
- ListView優化View優化
- Android ListViewAndroidView
- [譯]Flutter - 掌握ListViewFlutterView
- Flutter - Listview 詳解FlutterView
- ListView簡單使用View
- Android之ListViewAndroidView
- Android的ListViewAndroidView
- Listview禁止滑動View
- 8ListView4View
- 7ListView3View
- 6ListView2View
- 5ListView1View
- ListView 可編輯View
- ScrollView與ListView合用問題(正確計算Listview的高度)View
- android中的ListViewAndroidView
- Flutter ListView 用法詳解FlutterView
- Flutter ListView 原始碼分析FlutterView原始碼
- Flutter 中 ListView 的使用FlutterView
- RecyclerView與ListView比較View
- fragment中listview的onitemclicklistenerFragmentView
- android listview and scrollviewAndroidView
- RecycleView:再見前任(Listview)View
- ListView非同步建立ViewView非同步
- android:ListView bbs DemoAndroidView
- ListView+介面卡View
- listview屬性總結View
- getView()不復用convertView,ListView即毫無複用!(ListView回收機制)View
- 重寫ListView解決ListView內部ViewPaper滑動事件衝突問題View事件
- ListView與ScrollView衝突,計算ListView的高度Item,然後重繪View