怎么设置图标(listView)
WinForm,ListView,imagelist
程序如下
//****************************
try
{
int intRow;
SqlDataAdapter objAdapter=new SqlDataAdapter();
DataSet objds=new DataSet();
SqlConnection objCon=new SqlConnection(this.strCon);
objAdapter.SelectCommand=new SqlCommand("select * from db_register order by ID desc",objCon);
objAdapter.Fill(objds,"db_register");
DataTable objTable=null;
objTable=objds.Tables[0];
DataRow objRow=null;
this.lvi.Items.Clear();
for(intRow=0;intRow<objTable.Rows.Count;intRow++)
{
objRow=objTable.Rows[intRow];
ListViewItem lvi=new ListViewItem();
lvi.Text=objRow["UserID"].ToString();
lvi.SubItems.Add(objRow["Name"].ToString());
lvi.SubItems.Add(objRow[2].ToString());
lvi.SubItems.Add(objRow[3].ToString());
lvi.SubItems.Add(objRow[4].ToString());
this.lvi.Items.Add(lvi);
}
this.lvi.EndUpdate();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
怎么分别给listView的4中形式设置图标,添加在程序中? 多谢各位
问题点数:20、回复次数:3Top
1 楼wjcking()回复于 2004-09-04 12:52:33 得分 0
还有一个问题,就是怎么给listview里面的某个项设置图标Top
2 楼herony420(坦荡荡)回复于 2004-09-04 14:15:10 得分 20
下面是msdn的例子很好懂,但是请注意是没有办法给subitem设置图标的,这是因为subItem只是设置为显示,而不是控制的
该示例假定已经将该代码添加到某 Form 中,然后从构造函数或此窗体上的另一个方法调用在该示例中创建的方法。该示例还假定名为 MySmallImage1、MySmallImage2、MyLargeImage1 和 MyLargeImage2 的图像位于驱动器 C 的根目录下。
[Visual Basic]
Private Sub CreateMyListView()
' Create a new ListView control.
Dim listView1 As New ListView()
listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))
' Set the view to show details.
listView1.View = View.Details
' Allow the user to edit item text.
listView1.LabelEdit = True
' Allow the user to rearrange columns.
listView1.AllowColumnReorder = True
' Display check boxes.
listView1.CheckBoxes = True
' Select the item and subitems when selection is made.
listView1.FullRowSelect = True
' Display grid lines.
listView1.GridLines = True
' Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending
' Create three items and three sets of subitems for each item.
Dim item1 As New ListViewItem("item1", 0)
' Place a check mark next to the item.
item1.Checked = True
item1.SubItems.Add("1")
item1.SubItems.Add("2")
item1.SubItems.Add("3")
Dim item2 As New ListViewItem("item2", 1)
item2.SubItems.Add("4")
item2.SubItems.Add("5")
item2.SubItems.Add("6")
Dim item3 As New ListViewItem("item3", 0)
' Place a check mark next to the item.
item3.Checked = True
item3.SubItems.Add("7")
item3.SubItems.Add("8")
item3.SubItems.Add("9")
' Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)
'Add the items to the ListView.
listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
' Create two ImageList objects.
Dim imageListSmall As New ImageList()
Dim imageListLarge As New ImageList()
' Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))
'Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge
listView1.SmallImageList = imageListSmall
' Add the ListView to the control collection.
Me.Controls.Add(listView1)
End Sub 'CreateMyListView
[C#]
private void CreateMyListView()
{
// Create a new ListView control.
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check mark next to the item.
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3",0);
// Place a check mark next to the item.
item3.Checked = true;
item3.SubItems.Add("7");
item3.SubItems.Add("8");
item3.SubItems.Add("9");
// Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);
//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});
// Create two ImageList objects.
ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList();
// Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));
//Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge;
listView1.SmallImageList = imageListSmall;
// Add the ListView to the control collection.
this.Controls.Add(listView1);
}
Top
3 楼ffb(项目急,顾不了结构的)回复于 2004-09-16 17:13:44 得分 0
SubItem的图标怎么加?Top




