Search code examples
c#winformslistviewlistviewitem

Set Listviewitem's width


I have a Listview and it has one column. The view is set to List and I can see each Listviewitem but I can't select the item's row, I have to select the item's text. Is it possible to make it so that the Listviewitem's width is the same size as the Listview itself so that the user can click anywhere on the Listviewitem to select the item?

I tried searching but could only find how to change the column width and how to fix it in XAML, but this is for a WinForm.

Edit - As requested, this is the code that is generated by the Visual Studio designer. It is the only code involved with the ListviewItem.

System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("2");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("3");
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("4");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("5");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("6");
System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem("7");
this.listView1 = new System.Windows.Forms.ListView();

this.listView1.Activation = System.Windows.Forms.ItemActivation.OneClick;
            this.listView1.AllowDrop = true;
            this.listView1.AutoArrange = false;          
            this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.listView1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
            this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            listViewItem1.StateImageIndex = 0;
            this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem1,
            listViewItem2,
            listViewItem3,
            listViewItem4,
            listViewItem5,
            listViewItem6,
            listViewItem7});
            this.listView1.Location = new System.Drawing.Point(105, 129);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(157, 475);
            this.listView1.TabIndex = 4;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.List;

Solution

  • A simple solution will be using a ListBox instead of List View

    In case of simple text, it is better to use ListBox.

    Update:

    Simplest hack for getting this functionality in ListView will be adding dummy spaces at the end of each item string to fill.