环境:VS 2008,.NET 2.0,WinForms
我在Tile模式下有一个listview。某些项目具有关联的图像。有些人没有。
当用户点击某些控件时,内容(列表视图项)通常会更新。发生这种情况时,有时似乎没有新项目有图像。在这种情况下,我想摆脱为图像保留的项目左侧的空白区域。我尝试了以下伪代码来暂时删除图像列表
list.Items.Clear();
FillList();
list.LargeImageList= (none of the items has image) ? null : MyImageList;
但它不起作用:空的空间仍然存在。 我也尝试重新绘制控件,但无济于事。
alt text http://apptranslator.com/_so/so_list1.jpg alt text http://apptranslator.com/_so/so_list2.jpg alt text http://apptranslator.com/_so/so_list3.jpg
左:列出图像。
中:没有图像的列表。图像空间可见。
正确:当没有图像时,我希望如何。
编辑: 我也做了这个测试:不要在设计器中分配图像列表。 如果第一个显示器不包含任何图像,那么我会得到预期的结果。 然后我点击显示图像(我得到它们)。我再次点击返回无图像选择:图像空间不会消失。
另外,哈斯,不,我不使用小图像或状态图像。只有大图片。
我该怎么办? TIA。
答案 0 :(得分:1)
using System;
using System.Windows.Forms;
public class MainForm : Form
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.ListView listView;
private System.Windows.Forms.ImageList emptySmallImageList;
private System.Windows.Forms.ImageList largeImageList;
private System.Windows.Forms.Button imageListSmallButton;
private System.Windows.Forms.Button imageListLargeButton;
public MainForm()
{
InitializeComponent();
}
private void OnImageListSmallButtonClick(object sender, EventArgs e)
{
this.listView.LargeImageList = emptySmallImageList;
}
private void OnImageListLargeButtonClick(object sender, EventArgs e)
{
this.listView.LargeImageList = largeImageList;
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("fgsdfg");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("sdfgsdfg");
System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem("sdfgsdfgsdfg");
System.Windows.Forms.ListViewItem listViewItem8 = new System.Windows.Forms.ListViewItem("sdfgsdfg");
this.listView = new System.Windows.Forms.ListView();
this.largeImageList = new System.Windows.Forms.ImageList(this.components);
this.emptySmallImageList = new System.Windows.Forms.ImageList(this.components);
this.imageListSmallButton = new System.Windows.Forms.Button();
this.imageListLargeButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listView
//
this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem5,
listViewItem6,
listViewItem7,
listViewItem8});
this.listView.LargeImageList = this.largeImageList;
this.listView.Location = new System.Drawing.Point(0, 0);
this.listView.Name = "listView";
this.listView.Size = new System.Drawing.Size(292, 266);
this.listView.TabIndex = 0;
this.listView.UseCompatibleStateImageBehavior = false;
this.listView.View = System.Windows.Forms.View.Tile;
//
// largeImageList
//
this.largeImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.largeImageList.ImageSize = new System.Drawing.Size(32, 32);
this.largeImageList.TransparentColor = System.Drawing.Color.Transparent;
//
// emptySmallImageList
//
this.emptySmallImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.emptySmallImageList.ImageSize = new System.Drawing.Size(1, 1);
this.emptySmallImageList.TransparentColor = System.Drawing.Color.Transparent;
//
// imageListSmallButton
//
this.imageListSmallButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.imageListSmallButton.Location = new System.Drawing.Point(175, 12);
this.imageListSmallButton.Name = "imageListSmallButton";
this.imageListSmallButton.Size = new System.Drawing.Size(95, 23);
this.imageListSmallButton.TabIndex = 1;
this.imageListSmallButton.Text = "ImageList 1x1";
this.imageListSmallButton.UseVisualStyleBackColor = true;
this.imageListSmallButton.Click += new System.EventHandler(this.OnImageListSmallButtonClick);
//
// imageListLargeButton
//
this.imageListLargeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.imageListLargeButton.Location = new System.Drawing.Point(175, 53);
this.imageListLargeButton.Name = "imageListLargeButton";
this.imageListLargeButton.Size = new System.Drawing.Size(95, 23);
this.imageListLargeButton.TabIndex = 2;
this.imageListLargeButton.Text = "ImageList 32x32";
this.imageListLargeButton.UseVisualStyleBackColor = true;
this.imageListLargeButton.Click += new System.EventHandler(this.OnImageListLargeButtonClick);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.imageListLargeButton);
this.Controls.Add(this.imageListSmallButton);
this.Controls.Add(this.listView);
this.Name = "MainForm";
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
我已经做到了你现在所看到的,我无法理解为什么它没有进行调整...我想知道如果做一个SendMessage会做的伎俩,但我duno它会是什么消息。
如果你这样做,我可以看到最好的工作:imageList1.ImageSize = new Size(1,1);
我创建了一个测试版,除非我设置StateImageList
,否则它似乎有效。你还要清除StateImageList
吗?
答案 1 :(得分:0)
如果您想要完全控制,请使用listview.drawitem事件。
答案 2 :(得分:0)
我最近也遇到了这个问题。我注意到的是,无论LargeImageList ImageSize设置为什么(或者即使LargeImageList为NULL!),ListView也总是表现为LargeImageList.ImageSize仍然设置为它的最后一个值。
在我的程序中,我根本没有使用SmallImageList,所以它一直都是NULL。在我的表单构造函数中,我只是将它设置为ListView的新ImageList()并将大小锁定在(1,1)。问题消失了!看起来就像一个愚蠢的虫子。
public frmMain()
{
InitializeComponent();
this.Text = Program.AppName;
lstSightings.SmallImageList = new ImageList();
lstSightings.SmallImageList.ImageSize = new Size(1, 1);
RefreshItems();
}