在学习C#的winform开发时,想在一个ListView控件中添加一些自定义的按钮、输入框、进度条等,发现和Qt开发比想来真的很吃力。
解决方法:
然而自己简单实现了,把它打包为一个nupkg包,可以直接调用;
效果:
使用部分的代码:
using Jues.CustomListItemView;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using static System.Windows.Forms.ListViewItem;
namespace Demo
{
public partial class Form1 : Form
{
private CustomControl customControl;
public Form1()
{
InitializeComponent();
customControl = new CustomControl(this.listView1);
toolStripButtonAdd_Click(null, null);
}
private void toolStripButtonAdd_Click(object sender, EventArgs e)
{
ListViewItem listViewItem = new ListViewItem();
// name
listViewItem.Text = String.Format("jues {0}",this.listView1.Items.Count+1);
this.listView1.Items.Add(listViewItem);
// TextBox
TextBox textBox = new TextBox();
textBox.Text = String.Format("textBox {0}", this.listView1.Items.Count + 1);
customControl.Insert( this.columnHeaderTextBox.Index, listViewItem, textBox);
// ComboBox
ComboBox comboBox = new ComboBox();
comboBox.Items.Add("Baby");
comboBox.Items.Add("Young");
comboBox.Items.Add("Old");
customControl.Add(listViewItem, comboBox);
comboBox.SelectedIndex = this.listView1.Items.Count % comboBox.Items.Count;
// progressBar
ProgressBar progressBar = new ProgressBar();
progressBar.Maximum = 10;
progressBar.Value = this.listView1.Items.Count % progressBar.Maximum;
customControl.Add(listViewItem, progressBar);
// Button
Button button = new Button();
button.Text = "Delete";
ListViewSubItem buttonSubItem = customControl.Add(listViewItem, button);
button.Click += delegate (object sender1, EventArgs e1)
{
customControl.Remove(listViewItem);
// this.listView1.Items.Remove(listViewItem);
};
}
}
}
nupkg包: