?
Solved

How to find a column number in listview which is clicked

Posted on 2007-07-31
28
Medium Priority
?
316 Views
Last Modified: 2013-11-26
Hi,
How to find a column number in listview which is clicked


Regards.
0
Comment
Question by:anusdesai
  • 11
  • 10
  • 7
28 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599394
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

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19599474
The code from jpaulino needs to be placed in the MouseClick-event !
0
 

Author Comment

by:anusdesai
ID: 19599475
Hi,
its pointing to wrong columns....i have 13 columns but maximum i get is 6 from the function
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 53

Expert Comment

by:Dhaest
ID: 19599499
Are all your columns on the screen ? I guess you have a scrollbar in your listbox !
0
 

Author Comment

by:anusdesai
ID: 19599505
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.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599507
Thanks Dhaest I forgot to tell it :-)

Anusdesai Its works correctly with the listview
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599514
anusdesai in the mouse down event.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599522
But if you have a scrollbar (like Dhaest already post it) it does not work.
0
 

Author Comment

by:anusdesai
ID: 19599537
Stilll no luck...it counts max 6 columns....are any settings needed
 hover over....
full row select ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19599539
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
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599569
You still not anwer the question: are all column visible (no scrollbar) ?
0
 

Author Comment

by:anusdesai
ID: 19599584
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
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19599601
>> 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 ?
0
 

Author Comment

by:anusdesai
ID: 19599614
Yes all columns are visible...there is no scrollbar
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19599631
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
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 1000 total points
ID: 19599632
So, this must work

    Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
        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
                Debug.Print ("Column clicked: " & x + 1)
                Exit For
            End If
            colstart = colstart + ListView1.Columns(x).Width
        Next
    End Sub
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599645
Same time Dhaest :-)
0
 

Author Comment

by:anusdesai
ID: 19599658
Sorry it still doesnt work...
i have setting hover over true..select full row false
All bound columns....
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599665
If it doesnt work post your code here
0
 

Author Comment

by:anusdesai
ID: 19599716

    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




0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19599746
If I undersatnd it correctly, then you hide the columns (size = -2 ?)
0
 

Author Comment

by:anusdesai
ID: 19599748
 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
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599781
>> If I undersatnd it correctly, then you hide the columns (size = -2 ?)

Change it to 0
0
 

Author Comment

by:anusdesai
ID: 19599789
Fine..cheers will try and right back
0
 

Author Comment

by:anusdesai
ID: 19599809
Worked....cheers!!!!!!!!!!!

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19599826
Anusdesai you should slit the point & 
0
 

Author Comment

by:anusdesai
ID: 19599850
Hi,
What if i  my listview is scrollable?then how can i find the column?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19599859
In that case, I don't think it's possible (perhaps it is, but I haven't seen it before)
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question