Link to home
Start Free TrialLog in
Avatar of anusdesai
anusdesai

asked on

How to find a column number in listview which is clicked

Hi,
How to find a column number in listview which is clicked


Regards.
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Try this

        Dim colstart As Integer = 0
        Dim colend As Integer = 0
        Dim x As Integer

        For x = 0 To (ListView1.Columns.Count - 1)
            colend = colend + ListView1.Columns(x).Width
            If colstart <= e.X And e.X <= colend Then
                Label1.Text = "Column clicked: " & x + 1
                Exit For
            End If
            colstart = colstart + ListView1.Columns(x).Width
        Next

The code from jpaulino needs to be placed in the MouseClick-event !
Avatar of anusdesai
anusdesai

ASKER

Hi,
its pointing to wrong columns....i have 13 columns but maximum i get is 6 from the function
Are all your columns on the screen ? I guess you have a scrollbar in your listbox !
Hi,
If i place
The code from jpaulino needs to be placed in the MouseClick-event

i get e.X
X is not a member of system.eventargs.
Thanks Dhaest I forgot to tell it :-)

Anusdesai Its works correctly with the listview
anusdesai in the mouse down event.
But if you have a scrollbar (like Dhaest already post it) it does not work.
Stilll no luck...it counts max 6 columns....are any settings needed
 hover over....
full row select ?
This will only work if you click on the columnheader (title)

Private Sub listView1_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs)
      Dim colIndex As Integer = e.Column
      MessageBox.Show(listView1.Columns(colIndex).Text)
End Sub
You still not anwer the question: are all column visible (no scrollbar) ?
Sorry this does not suffice the requirement...user can click anywhere on listview where i have to fire a pop up menu depending on the column selected
>> Sorry this does not suffice the requirement...user can click anywhere on listview where i have to fire a pop up menu depending on the column selected

I already thought that, but can you please answer jpaulino and mine question:
Are all columns visible without scrollbar ?
Yes all columns are visible...there is no scrollbar
Then this should work ...


Private Sub listView1_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim colstart As Integer = 0
        Dim colend As Integer = 0
        Dim x As Integer

        For x = 0 To (ListView1.Columns.Count - 1)
            colend = colend + ListView1.Columns(x).Width
            If colstart <= e.X And e.X <= colend Then
                Label1.Text = "Column clicked: " & x + 1
                Exit For
            End If
            colstart = colstart + ListView1.Columns(x).Width
        Next
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Same time Dhaest :-)
Sorry it still doesnt work...
i have setting hover over true..select full row false
All bound columns....
If it doesnt work post your code here

    Private Sub ListControls_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListControls.MouseDown
        Dim colstart As Integer = 0
        Dim colend As Integer = 0
        Dim x As Integer

        For x = 0 To (Me.ListControls.Columns.Count - 1)
            colend = colend + Me.ListControls.Columns(x).Width
            If colstart <= e.X And e.X <= colend Then
                MsgBox("Column clicked: " & x + 1)
                Exit For
            End If
            colstart = colstart + Me.ListControls.Columns(x).Width
        Next
    End Sub




 Public Sub LoadColumns()

        'Create a column for each record in the reports table
        'set the width of the column to the length of the report name

            Dim SQL As String = ""
        Dim dt As New DataTable

        Dim i As Integer = 2
        Dim Col As System.Windows.Forms.ListView.ColumnHeaderCollection
        Dim colstr As String = ""
        Col = ListControls.Columns
        Dim orow As DataRow


        Sql = "SELECT NAME FROM abc"
        Dim colcnt As Integer = 1
        dt = Populate(SQL)
        For Each orow In dt.Rows
            Try
                colstr = orow(0)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            colcnt = colcnt + 1
            Col.Insert(colcnt, "", colstr, 1000)
        Next
        sizeTheColumns(Me.ListControls)
    End Sub

  Public Sub sizeTheColumns(ByVal lvw As ListView)
        For i As Integer = 0 To lvw.Columns.Count - 1
            Application.DoEvents()
            lvw.Columns(i).Width = -2
        Next i
        'If lvw.Columns.Count > 1 Then
        '    lvw.Columns(0).Width = lvw.Columns(i).Width + 1
        'End If
    End Sub




If I undersatnd it correctly, then you hide the columns (size = -2 ?)
 Private Sub ListControls_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListControls.Click



        Dim Itm As System.Windows.Forms.ListView.ListViewItemCollection 'items in batch list
        Itm = ListControls.Items
        With ListControls.FocusedItem
            If Not .Selected Then Exit Sub
            SaveRunSettings = True
            If .ImageIndex = 0 Then

                .ImageIndex = 2  'UNTICK
            Else
                .ImageIndex = 0   'TICK
            End If
            ListControls.Visible = False
            ListControls.Visible = True
        End With
    End Sub
>> If I undersatnd it correctly, then you hide the columns (size = -2 ?)

Change it to 0
Fine..cheers will try and right back
Worked....cheers!!!!!!!!!!!

Anusdesai you should slit the point & 
Hi,
What if i  my listview is scrollable?then how can i find the column?
In that case, I don't think it's possible (perhaps it is, but I haven't seen it before)