您的位置:首页 > 编程语言 > C#

C#中ListView和ImageList配合使用

2008-01-11 13:59 399 查看
http://www.programfan.com/club/post-218751-1.html
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ListView lv = new ListView();
ImageList il = new ImageList();

public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

Controls.Add(lv);
lv.Dock = DockStyle.Fill;
lv.View = View.LargeIcon;
lv.LargeImageList = il;

if (System.IO.File.Exists(@"C:/Documents and Settings/Administrator/桌面/game.txt"))
{
face();
}
else
{
MessageBox.Show("file don't exists");
}
}

private void face()
{

System.IO.StreamReader sr = new System.IO.StreamReader(@"C:/Documents and Settings/Administrator/桌面/game.txt");
string sLine = "";
ArrayList arrText = new ArrayList();

while (sLine != null)
{
sLine = sr.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
sr.Close();

string[][] tmpArr = new string[arrText.Count][];

for (int i = 1; i < arrText.Count; i++)
{
tmpArr[i] = new string[5];

string[] tmpArrB = arrText[i].ToString().Split(',');
tmpArr[i][0] = tmpArrB[0]; //序号
tmpArr[i][1] = tmpArrB[1]; //图片
tmpArr[i][2] = tmpArrB[2]; //名称
tmpArr[i][3] = tmpArrB[3]; //路径
tmpArr[i][4] = tmpArrB[4]; //信息

il.ImageSize = new Size(24, 24);
il.Images.Add(new Bitmap(tmpArr[i][1]));

lv.Items.Add(tmpArr[i][2],i-1);
}
}
}
}

// 图片取自qq的face

// 读取的文本文件格式为

No.,gamePic,gameName,gamePath,gameInfo
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: