Fixed listview to look & behave like a listbox, but with icons

This commit is contained in:
Thorsten Sommer 2022-07-17 20:47:17 +02:00
parent a7cef8d209
commit d36a258392
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 27 additions and 13 deletions

View File

@ -38,6 +38,7 @@
this.labelFilter = new System.Windows.Forms.Label(); this.labelFilter = new System.Windows.Forms.Label();
this.labelSectionPath = new System.Windows.Forms.Label(); this.labelSectionPath = new System.Windows.Forms.Label();
this.listTextElements = new System.Windows.Forms.ListView(); this.listTextElements = new System.Windows.Forms.ListView();
this.column = new System.Windows.Forms.ColumnHeader();
this.toolTip = new System.Windows.Forms.ToolTip(this.components); this.toolTip = new System.Windows.Forms.ToolTip(this.components);
this.tableLayout.SuspendLayout(); this.tableLayout.SuspendLayout();
this.flowLayoutToolbar.SuspendLayout(); this.flowLayoutToolbar.SuspendLayout();
@ -155,19 +156,23 @@
// //
// listTextElements // listTextElements
// //
this.listTextElements.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.column});
this.tableLayout.SetColumnSpan(this.listTextElements, 2); this.tableLayout.SetColumnSpan(this.listTextElements, 2);
this.listTextElements.Dock = System.Windows.Forms.DockStyle.Fill; this.listTextElements.Dock = System.Windows.Forms.DockStyle.Fill;
this.listTextElements.FullRowSelect = true; this.listTextElements.FullRowSelect = true;
this.listTextElements.GridLines = true;
this.listTextElements.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; this.listTextElements.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
this.listTextElements.Location = new System.Drawing.Point(69, 43); this.listTextElements.Location = new System.Drawing.Point(69, 43);
this.listTextElements.MultiSelect = false; this.listTextElements.MultiSelect = false;
this.listTextElements.Name = "listTextElements"; this.listTextElements.Name = "listTextElements";
this.listTextElements.ShowGroups = false;
this.listTextElements.Size = new System.Drawing.Size(634, 115); this.listTextElements.Size = new System.Drawing.Size(634, 115);
this.listTextElements.TabIndex = 5; this.listTextElements.TabIndex = 5;
this.listTextElements.UseCompatibleStateImageBehavior = false; this.listTextElements.UseCompatibleStateImageBehavior = false;
this.listTextElements.View = System.Windows.Forms.View.List; this.listTextElements.View = System.Windows.Forms.View.Details;
//
// column
//
this.column.Width = 194;
// //
// toolTip // toolTip
// //
@ -204,5 +209,6 @@
private Label labelFilter; private Label labelFilter;
private Label labelSectionPath; private Label labelSectionPath;
private ListView listTextElements; private ListView listTextElements;
private ColumnHeader column;
} }
} }

View File

@ -1,6 +1,7 @@
using DataModel.Database; using DataModel.Database;
using Processor; using Processor;
using UI_WinForms.Dialogs; using UI_WinForms.Dialogs;
using UI_WinForms.Resources;
namespace UI_WinForms.Components; namespace UI_WinForms.Components;
@ -16,6 +17,15 @@ public partial class TextElements : UserControl
if(Program.SERVICE_PROVIDER is null) if(Program.SERVICE_PROVIDER is null)
return; return;
// Create an image list from a resource:
var imgList = new ImageList();
imgList.ImageSize = new Size(45, 45);
imgList.ColorDepth = ColorDepth.Depth32Bit;
imgList.Images.Add(Icons.icons8_align_text_left_512);
// Set the image list to the list box:
this.listTextElements.SmallImageList = imgList;
// When the section is changed, update this component: // When the section is changed, update this component:
AppEvents.WhenSectionChanged += async (sender, section) => AppEvents.WhenSectionChanged += async (sender, section) =>
{ {
@ -43,14 +53,12 @@ public partial class TextElements : UserControl
// Update the list: // Update the list:
this.listTextElements.Items.Clear(); this.listTextElements.Items.Clear();
foreach (var textElement in textElements) foreach (var textElement in textElements)
{ this.listTextElements.Items.Add(new ListViewItem(textElement.Name, 0)
var item = new ListViewItem(textElement.Name)
{ {
Tag = textElement.Code, Tag = textElement.Code,
}; });
this.listTextElements.Items.Add(item); this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
}
} }
private async void buttonAdd_Click(object sender, EventArgs e) private async void buttonAdd_Click(object sender, EventArgs e)
@ -76,12 +84,12 @@ public partial class TextElements : UserControl
return; return;
// Add the text element to the list: // Add the text element to the list:
var item = new ListViewItem(newTextElement.Result!.Name) this.listTextElements.Items.Add(new ListViewItem(newTextElement.Result!.Name, 0)
{ {
Tag = newTextElement.Result.Code, Tag = newTextElement.Result.Code,
}; });
this.listTextElements.Items.Add(item); this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
} }
private void buttonRemove_Click(object sender, EventArgs e) private void buttonRemove_Click(object sender, EventArgs e)