c# 有控件能显示下图的效果吗? 关键是哪个复选框怎么加上啊?listview和imagelist能显示图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用checkbox、PictureBox和Label组合一个自定义控件。然后循环创建,放入FlowLayoutPanel组合到界面上。 private void button1_Click(object sender, EventArgs e) { imageList1.ColorDepth = ColorDepth.Depth24Bit; imageList1.ImageSize = new Size(120, 120); listView1.View = View.LargeIcon; for (int i = 0; i < 12; i++) { int k = 160 * i + 8; imageList1.Images.Add(Image.FromFile(Application.StartupPath + "\\"+(i+1).ToString()+".jpg")); listView1.LargeImageList = imageList1; listView1.Items.Add("pic"+(i+1).ToString()); listView1.Items[i].ImageIndex = i; CheckBox chk2 = new CheckBox(); //chk2.Location =new Point(k, 130); chk2.Top = 130; chk2.Left = k; chk2.Width = 13; chk2.Text = i.ToString(); chk2.Checked = (i % 1 == 0); chk2.Visible = true; listView1.Controls.Add(chk2); } }帮忙看看这样行吗? checkbox的top值不好确定,主要是窗口的大小可以改变,所以一行的图片数就不确定了上面的checkbox的位置好像是相对Form的,怎样才能改成相对于listview呢 不要用listview,否则很麻烦。listview还要滚动,还得计算滚动客户区。我让你装入flowlayoutpanel就一了百了了。 listview本来就带checkbox属性 WPF的话可以自定义样式模板,然后将view跟后台数据绑定WF的话如果要实现比较美观的效果,可以重写ListView或者ListBox,实现Item WinForm就自定义控件就好了,如果是WPF自定义模板就可以轻松实现的 首先定义一个用户控件ImageCell,在设计界面设计好它的布局using System;using System.Windows.Forms;using System.Drawing;namespace Demo{ /// <summary> /// 图片格子用户控件 /// </summary> public partial class ImageCell : UserControl { public ImageCell() { InitializeComponent(); SetupControls(); } private void SetupControls() { //图像按比例缩放 pbImage.SizeMode = PictureBoxSizeMode.StretchImage; //设置标签居中显示 lblTitle.AutoSize = false; lblTitle.TextAlign = ContentAlignment.TopCenter; } //复选框 public bool ImageCellCheck { get { return ckbCheck.Checked; } set { ckbCheck.Checked = value; } } //图像 public Image ImageCellImage { get { return pbImage.Image; } set { pbImage.Image = value; } } //标题 public string ImageCellTitle { get { return lblTitle.Text; } set { lblTitle.Text = value; } } }}定义一个容器ImageCellContainer:using System;using System.Windows.Forms;using System.IO;using System.Drawing;namespace Demo{ public class ImageCellContainer : FlowLayoutPanel { private const string FILE_PATH = "Cards/"; private ImageCell[] imageCells=null; public ImageCellContainer() { LoadImage(); } //或者从其他数据源获取数据进行绑定 private void LoadImage() { try { string[] files = Directory.GetFiles(FILE_PATH); imageCells = new ImageCell[files.Length]; for (int i = 0; i < files.Length; i++) { Image image = Image.FromFile(string.Format("{0}", files[i])); ImageCell cell = new ImageCell(); cell.ImageCellCheck = i % 2 == 0 ? true : false; cell.ImageCellImage = image; cell.ImageCellTitle = (i + 1) + "." + files[i]; imageCells[i]=cell; } this.Controls.AddRange(imageCells); } catch (Exception ex) { throw ex; } } }}使用该自定义控件:using System;using System.Windows.Forms;using System.Drawing;namespace Demo{ public partial class Form1 : Form { ImageCellContainer imageCellContainer; public Form1() { InitializeComponent(); this.BackColor = Color.LightGreen; imageCellContainer = new ImageCellContainer(); imageCellContainer.Dock = DockStyle.Fill; imageCellContainer.AutoScroll = true; this.Controls.Add(imageCellContainer); } }}效果:这个实现很粗糙,有待你的改善 如果你就这么几张数的清的图片FlowLayoutPanel最适合你了。如果有几千张图什么的,还是要用Listview 请教,自定义组件后如何通过一个checkbox设定全部自定义组件中的checkbox的全选或反选,如何获取自定义组件中所有选定的checkbox的text文本? 网络通信缓冲区大小问题 如何把PPT转换成图片 安装时候遇到大问题了,紧急求救啊! 怎么让C写的消息队列让C#读出来 树形输出~ 请遇到过类似水晶报表导出问题的高手们帮忙看看,谢谢啦 智能设备连SQL server WebBrowser控件,如何只显示一部分? 微软的例子中ApplicationConfiguration.ReadSetting的ApplicationConfiguration是什么? 谁用过以色列的一个编程工具magic呀> c++ builder 封装了一个dll,c#调用报错,求助!! C#连接sqlsever出现的问题,求高手解答。急急
{
imageList1.ColorDepth = ColorDepth.Depth24Bit;
imageList1.ImageSize = new Size(120, 120);
listView1.View = View.LargeIcon;
for (int i = 0; i < 12; i++)
{
int k = 160 * i + 8;
imageList1.Images.Add(Image.FromFile(Application.StartupPath + "\\"+(i+1).ToString()+".jpg"));
listView1.LargeImageList = imageList1;
listView1.Items.Add("pic"+(i+1).ToString());
listView1.Items[i].ImageIndex = i;
CheckBox chk2 = new CheckBox();
//chk2.Location =new Point(k, 130);
chk2.Top = 130;
chk2.Left = k;
chk2.Width = 13;
chk2.Text = i.ToString(); chk2.Checked = (i % 1 == 0); chk2.Visible = true;
listView1.Controls.Add(chk2);
}
}
帮忙看看这样行吗? checkbox的top值不好确定,主要是窗口的大小可以改变,所以一行的图片数就不确定了
上面的checkbox的位置好像是相对Form的,怎样才能改成相对于listview呢
WF的话如果要实现比较美观的效果,可以重写ListView或者ListBox,实现Item
using System;
using System.Windows.Forms;
using System.Drawing;
namespace Demo
{
/// <summary>
/// 图片格子用户控件
/// </summary>
public partial class ImageCell : UserControl
{
public ImageCell()
{
InitializeComponent();
SetupControls();
}
private void SetupControls()
{
//图像按比例缩放
pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
//设置标签居中显示
lblTitle.AutoSize = false;
lblTitle.TextAlign = ContentAlignment.TopCenter;
} //复选框
public bool ImageCellCheck
{
get { return ckbCheck.Checked; }
set { ckbCheck.Checked = value; }
}
//图像
public Image ImageCellImage
{
get { return pbImage.Image; }
set { pbImage.Image = value; }
}
//标题
public string ImageCellTitle
{
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
}
}定义一个容器ImageCellContainer:using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
namespace Demo
{
public class ImageCellContainer : FlowLayoutPanel
{
private const string FILE_PATH = "Cards/";
private ImageCell[] imageCells=null; public ImageCellContainer()
{
LoadImage();
}
//或者从其他数据源获取数据进行绑定
private void LoadImage()
{
try
{
string[] files = Directory.GetFiles(FILE_PATH);
imageCells = new ImageCell[files.Length];
for (int i = 0; i < files.Length; i++)
{
Image image = Image.FromFile(string.Format("{0}", files[i]));
ImageCell cell = new ImageCell();
cell.ImageCellCheck = i % 2 == 0 ? true : false;
cell.ImageCellImage = image;
cell.ImageCellTitle = (i + 1) + "." + files[i];
imageCells[i]=cell;
}
this.Controls.AddRange(imageCells);
}
catch (Exception ex)
{
throw ex;
}
}
}
}使用该自定义控件:using System;
using System.Windows.Forms;
using System.Drawing;
namespace Demo
{
public partial class Form1 : Form
{
ImageCellContainer imageCellContainer;
public Form1()
{
InitializeComponent(); this.BackColor = Color.LightGreen;
imageCellContainer = new ImageCellContainer();
imageCellContainer.Dock = DockStyle.Fill;
imageCellContainer.AutoScroll = true;
this.Controls.Add(imageCellContainer);
}
}
}效果:这个实现很粗糙,有待你的改善
如何获取自定义组件中所有选定的checkbox的text文本?