C# WinForm中Listview的使用

来源:转载

在WinForm中,有时候需要在画面上显示图标以及详情之类的,还有的更多功能需要大中小图标的切换显示,这时候可以考虑Listview控件。代码如下:


private void BoardDisplay(int kbn)
{
this.listView1.Clear();if (kbn == 0)
{
ImageLoad(1);
if (board_nums == 0)
{
return;
}
this.listView1.View = View.List;
this.listView1.SmallImageList = this.imageList1;
this.listView1.BeginUpdate();
for (int i = 1; i <= board_nums; i++)
{
ListViewItem boardList = new ListViewItem();
boardList.ImageIndex = i - 1;
boardList.Text = board_names[i - 1];
this.listView1.Items.Add(boardList);
}
this.listView1.EndUpdate();
}
else if (kbn == 1)
{
ImageLoad(2);
if (board_nums == 0)
{
return;
}
this.listView1.View = View.SmallIcon;
this.listView1.SmallImageList = this.imageList2;
this.listView1.BeginUpdate();
for (int i = 1; i <= board_nums; i++)
{
ListViewItem boardList = new ListViewItem();
boardList.ImageIndex = i - 1;
boardList.Text = board_names[i - 1];
this.listView1.Items.Add(boardList);
}
this.listView1.EndUpdate();
}
else
{
ImageLoad(3);
if (board_nums == 0)
{
return;
}
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = this.imageList3;
this.listView1.BeginUpdate();
for (int i = 1; i <= board_nums; i++)
{
ListViewItem boardList = new ListViewItem();
boardList.ImageIndex = i - 1;
boardList.Text = board_names[i - 1];
this.listView1.Items.Add(boardList);
}
this.listView1.EndUpdate();
}
} 以上代码中的kbn是用来区别大中小图标的显示的,三种情况下Listview关联三个不同的Imagelist,三个imagelist中分别存放不同尺寸的图标。关联imagelist之前,要先将图片加载到imagelist中,即ImageLoad()方法(如下),再将此时的显示形式设置为相应的形式,即listview1.View = View.LargeIcon;这句话就是显示设置为大图标显示,这里显示共有5种属性。然后关联相应的imagelist。BeginUpdate()和EndUpdate()用于在向listview中加载显示项的过程中将listview置于挂起状态,令显示更加稳定。


private void ImageLoad(int kbn)
{
board_nums = 0;
DirectoryInfo di = new DirectoryInfo(kojiFolderPC);
if (!di.Exists)
{
//MessageBox.Show("画像のパスが存在しません");
return;
}
if (di.GetFiles("*" + Para.iconType).Length != 0)
{
switch (kbn)
{
case 1:
imageList1.Images.Clear();
board_names = new string[di.GetFiles("*" + Para.iconType).Length];
foreach (FileInfo fi in di.GetFiles("*" + Para.iconType))
{
imageList1.Images.Add(Image.FromFile(fi.FullName));
board_names[board_nums] = fi.Name.Substring(0, fi.Name.Length - 4);
board_nums++;
}
break;
case 2:
imageList2.Images.Clear();
board_names = new string[di.GetFiles("*" + Para.iconType).Length];
foreach (FileInfo fi in di.GetFiles("*" + Para.iconType))
{
imageList2.Images.Add(Image.FromFile(fi.FullName));
board_names[board_nums] = fi.Name.Substring(0, fi.Name.Length - 4);
board_nums++;
}
break;
case 3:
imageList3.Images.Clear();
board_names = new string[di.GetFiles("*" + Para.iconType).Length];
foreach (FileInfo fi in di.GetFiles("*" + Para.iconType))
{
imageList3.Images.Add(Image.FromFile(fi.FullName));
board_names[board_nums] = fi.Name.Substring(0, fi.Name.Length - 4);
board_nums++;
}
break;
}
}
}

分享给朋友:
您可能感兴趣的文章:
随机阅读: