listview中选择一个item获得该item名字的问题,可能bug?

红烧蹄膀饭 2008-05-09 11:41:19
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
treeView1.ImageList = imageList1;
TreeNode tn = new TreeNode("我的电脑", 2, 2);
treeView1.Nodes.Add(tn);
string[] dname = Directory.GetLogicalDrives();

foreach(string x in dname)
{
TreeNode tn1 = new TreeNode(x,1,2);
tn.Nodes.Add(tn1);
//ListViewItem lvi = new ListViewItem(x.Substring(0,2),1);
listView1.Items.Add(x,1);

}
tn.Expand();

}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
string path = treeView1.SelectedNode.FullPath;
if (path.Length > 4)
{
path = path.Substring(5);
DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo[] Moudle1 = dir.GetDirectories();
foreach(DirectoryInfo y in Moudle1)
{
TreeNode tn2 = new TreeNode(y.Name,0,3);
treeView1.SelectedNode.Nodes.Add(tn2);

}
}
}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
listView1.Items.Clear();
// string[] mo = Directory.GetLogicalDrives();
// foreach(string m in mo)
string p_str = this.listView1.SelectedItems[0].Text.ToString();//--这里报错:InvalidArgument=“0”的值对于“index”无效。
参数名: index
DirectoryInfo dir = new DirectoryInfo(p_str);
DirectoryInfo[] moudle = dir.GetDirectories();
foreach(DirectoryInfo z in moudle)
{
ListViewItem lvi = new ListViewItem(z.Name,0);
listView1.Items.Add(lvi);
}

}


}
}
...全文
148 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
红烧蹄膀饭 2008-05-09
  • 打赏
  • 举报
回复
thank you !
jinjazz 2008-05-09
  • 打赏
  • 举报
回复
listView1.Items.Clear();

都被你删了,哪还有东西

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧