Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Controls in VB.NET Listview [closed]

Tags:

vb.net

Hello I have a Listview in VB.NET and I want to insert inside it a name and a button as following: The name should be displayed in the left of the listview and the button in the right (just like if I have a row inside the listeview and the two controls placed in the left and right of that row) so, is it possible in VB.NET and if it's the case how? I hope my question was clear. Thanks.

like image 296
OussamaLord Avatar asked Feb 26 '26 02:02

OussamaLord


2 Answers

Not easily. However a DataGridView could handle this easily:

enter image description here

    DataGridView1.Columns.AddRange(New DataGridViewColumn(1) _
                                    {New DataGridViewTextBoxColumn() With _
                                     {.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, .HeaderText = "Text"},
                                     New DataGridViewButtonColumn() With _
                                     {.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, .HeaderText = "Button"}})
    DataGridView1.Rows.Add({"row 1 text", "row 1 button"})
    DataGridView1.Rows.Add({"row 2 text", "row 2 button"})

These properties can be set from the designer, and you would probably bind the datagridview rather than add the rows manually.

EDIT to remove a row on button click, use the CellContentClick event:

Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    'check it the button column being clicked, and check they are not clicking the column heading
    If e.ColumnIndex = 1 And e.RowIndex >= 0 Then
        DataGridView1.Rows.RemoveAt(e.RowIndex)
    End If
End Sub

Note if you do use databinding, you should remove the row from the data object and not the datagridview

EDIT Extracting all column on items to a list:

 Dim columnOneValues As New List(Of String)

 For Each row As DataGridViewRow In DataGridView1.Rows
        columnOneValues.Add(row.Cells(0).Value.ToString)
 Next
like image 146
Steve Avatar answered Feb 27 '26 20:02

Steve


Short answer: no.

Longer answer: yes, but this is nontrivial, and you can't do it with the stock ListView.

You'd have to extend the class, handle the drawing of the controls yourself, calculating their positions and dimensions according to the size of the ListViewItem. Furthermore, if a user reorders the ListView columns, there is no trivial way to get the new column order.

See this codeproject page for an example of a custom ListView.

like image 38
alldayremix Avatar answered Feb 27 '26 18:02

alldayremix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!