?
Solved

Combobox in Listview

Posted on 2010-09-19
8
Medium Priority
?
535 Views
Last Modified: 2012-06-27
Hi, I followed instructions from Microsoft Support page and created my usercontrol which provide me with posibility to have combobox within listview (http://support.microsoft.com/kb/320342/).
It works Ok in the way that  when the user selects a row in the ListView, a calculation is performed to locate the bounding rectangle for the first column. How can I set to make  the bounding rectangle for the second column...
VB code below....
 Please help...I would appreciate your help a lot....thank you...



Imports System.Data.SqlClient

Public Class Form1
    Private lvItem As ListViewItem
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim con As New SqlConnection(My.Settings.MyConn)
        con.Open()

        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT [DESC] FROM Data_Source"
        cmd.Connection = con

        Dim dr As SqlDataReader
        Dim i As String

        ' Add a few items to the combo box list.
        dr = cmd.ExecuteReader(CommandBehavior.Default)
        Do While dr.Read
            i = Me.cbListViewCombo.Items.Add(RTrim(dr("Desc")))
        Loop
        dr.Close()

        ' Add a few items to the combo box list.

        Me.cbListViewCombo.Items.Add("NC")
        Me.cbListViewCombo.Items.Add("WA")

        'Set view of ListView to Details.
        Me.MyListView1.View = View.Details

        '' Turn on full row select.
        Me.MyListView1.FullRowSelect = True
        Me.MyListView1.GridLines = True


        '' Add some data to the ListView.
        Dim columnheader As ColumnHeader
        Dim lviewitem As ListViewItem

        ' Create column headers for the data.
        columnheader = New ColumnHeader()
        columnheader.Text = "Destination"
        Me.MyListView1.Columns.Add(columnheader)

        columnheader = New ColumnHeader()
        columnheader.Text = "Source"
        Me.MyListView1.Columns.Add(columnheader)

        '' Create sample ListView data.
        lviewitem = New ListViewItem("North Carolina")
        lviewitem.SubItems.Add("NA")
        Me.MyListView1.Items.Add(lviewitem)

        lviewitem = New ListViewItem("WA")
        lviewitem.SubItems.Add("Washington")
        Me.MyListView1.Items.Add(lviewitem)

        

        '' Loop through and size each column header to fit the column header text.
        Dim ch As ColumnHeader

        For Each ch In Me.MyListView1.Columns
            ch.Width = -2
        Next

    End Sub

    Private Sub cbListViewCombo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbListViewCombo.KeyPress
        ' Verify that the user presses ESC.
        Select Case (e.KeyChar)
            Case ChrW(CType(Keys.Escape, Integer))
                ' Reset the original text value, and then hide the ComboBox.
                Me.cbListViewCombo.Text = lvItem.Text
                Me.cbListViewCombo.Visible = False

            Case ChrW(CType(Keys.Enter, Integer))
                ' Hide the ComboBox.
                Me.cbListViewCombo.Visible = False
        End Select

        MsgBox("OK1")

    End Sub

    Private Sub cbListViewCombo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbListViewCombo.Leave
        ' Set text of ListView item to match the ComboBox.
        lvItem.Text = Me.cbListViewCombo.Text

        ' Hide the ComboBox.
        Me.cbListViewCombo.Visible = False


    End Sub


    Private Sub cbListViewCombo_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbListViewCombo.SelectedValueChanged
        ' Set text of ListView item to match the ComboBox.
        lvItem.Text = Me.cbListViewCombo.Text

        ' Hide the ComboBox.
        Me.cbListViewCombo.Visible = False


    End Sub

    Private Sub MyListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyListView1.MouseUp
        ' Get the item on the row that is clicked.
        lvItem = Me.MyListView1.GetItemAt(e.X, e.Y)

        ' Make sure that an item is clicked.
        If Not (lvItem Is Nothing) Then
            ' Get the bounds of the item that is clicked.
            Dim ClickedItem As Rectangle = lvItem.Bounds

            ' Verify that the column is completely scrolled off to the left.
            If ((ClickedItem.Left + Me.MyListView1.Columns(0).Width) < 0) Then
                ' If the cell is out of view to the left, do nothing.
                Return

                ' Verify that the column is partially scrolled off to the left.
            ElseIf (ClickedItem.Left < 0) Then
                ' Determine if column extends beyond right side of ListView.
                If ((ClickedItem.Left + Me.MyListView1.Columns(0).Width) > Me.MyListView1.Width) Then
                    ' Set width of column to match width of ListView.
                    ClickedItem.Width = Me.MyListView1.Width
                    ClickedItem.X = 0
                Else
                    ' Right side of cell is in view.
                    ClickedItem.Width = Me.MyListView1.Columns(0).Width + ClickedItem.Left
                    ClickedItem.X = 2
                End If

            ElseIf (Me.MyListView1.Columns(0).Width > Me.MyListView1.Width) Then
                ClickedItem.Width = Me.MyListView1.Width

            Else
                ClickedItem.Width = Me.MyListView1.Columns(0).Width
                ClickedItem.X = 2
            End If

            ' Adjust the top to account for the location of the ListView.
            ClickedItem.Y += Me.MyListView1.Top
            ClickedItem.X += Me.MyListView1.Left

            ' Assign calculated bounds to the ComboBox.
            Me.cbListViewCombo.Bounds = ClickedItem

            ' Set default text for ComboBox to match the item that is clicked.
            Me.cbListViewCombo.Text = lvItem.Text

            ' Display the ComboBox, and make sure that it is on top with focus.
            Me.cbListViewCombo.Visible = True
            Me.cbListViewCombo.BringToFront()
            Me.cbListViewCombo.Focus()
        End If


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(Me.cbListViewCombo.Text)
    End Sub
End Class

Open in new window

0
Comment
Question by:alsam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33711652
hi....I am Trying to Find out a solution as you wish but i think you are making your life difficult......just a question ..Did you consider using Datagridview with comboboxes?there ,you can make the cell looks like textboxes and when you click on it there is a combobox!!
0
 
LVL 1

Author Comment

by:alsam
ID: 33711683
Thank you very much for your reply in such short notice...
Yes I have considered your above proposition before but anyway it's still important to me to make this listview working as described in my original post....
 
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33711752
create a form1
combobox name it MyCombo
ListView name it MyListView
Combobox visible = false.


In this example the combobox goes in every column in every column in listView
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Add a few items to the combo box list
        Me.MyCombo.Items.Add("Master")
        Me.MyCombo.Items.Add("Guru")

        ' Set view of listview to list
        Me.MyListView.View = View.List

        ' Turn the grid on for reference
        Me.MyListView.GridLines = True

        ' Add some data to the listview
        For x = 1 To 99
            MyListView.Items.Add(New System.Windows.Forms.ListViewItem("Test" & x.ToString()))
        Next

    End Sub

    Private MyItem As ListViewItem

    Private Sub MyCombo_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyCombo.KeyUp
        ' Check if user types "ESC"
        If e.KeyCode = Keys.Escape Then
            ' Hide the combobox
            Me.MyCombo.Visible = False
        End If
    End Sub
   
    Private Sub MyCombo_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyCombo.SelectedValueChanged
        ' Set text of listview item
        MyItem.Text = Me.MyCombo.Text
        'this.MyCombo.Text = "";

        ' Hide the combobox
        Me.MyCombo.Visible = False
    End Sub

    Private Sub MyListView_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyListView.MouseUp
        ' Get the item that was clicked
        MyItem = Me.MyListView.GetItemAt(e.X, e.Y)

        ' Check if an actual item was clicked
        If MyItem IsNot Nothing Then
            Dim ClickedItem As Rectangle

            ' Get the bounds of the item clicked
            ClickedItem = MyItem.Bounds

            ' Adjust the top and left of the control
            ClickedItem.X += Me.MyListView.Left
            ClickedItem.Y += Me.MyListView.Top

            ' Set combobox bounds to match calculation
            Me.MyCombo.Bounds = ClickedItem

            ' Set the default text for the combobox to be
            ' the clicked item's text
            Me.MyCombo.Text = MyItem.Text

            ' Show the combobox and make sure it is on top
            ' and give focus to the combobox
            Me.MyCombo.Visible = True
            Me.MyCombo.BringToFront()
            Me.MyCombo.Focus()
        End If
    End Sub
End Class

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:alsam
ID: 33711796
Great...
but is there a way to provide combobox only for e.g colum 2...
All other colums should not have combo....
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33711807
that is what i am looking for .....:)
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 2000 total points
ID: 33711914
ok i think i got it...change your mouse up event like the code below ...The problem was with the width's and the column(0) instead of Columns(1)...(Always in your Code)
Private Sub MyListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyListView1.MouseUp
        ' Get the item on the row that is clicked.
        lvItem = Me.MyListView1.GetItemAt(e.X, e.Y)

        ' Make sure that an item is clicked.
        If Not (lvItem Is Nothing) Then
            ' Get the bounds of the item that is clicked.
            Dim ClickedItem As Rectangle = lvItem.Bounds

            ' Verify that the column is completely scrolled off to the left.
            If ((ClickedItem.Left + Me.MyListView1.Columns(1).Width) < 0) Then
                ' If the cell is out of view to the left, do nothing.
                Return

                ' Verify that the column is partially scrolled off to the left.
            ElseIf (ClickedItem.Left < 0) Then
                ' Determine if column extends beyond right side of ListView.
                If ((ClickedItem.Left + Me.MyListView1.Columns(1).Width) > Me.MyListView1.Width) Then
                    ' Set width of column to match width of ListView.
                    ClickedItem.Width = Me.MyListView1.Width
                    ClickedItem.X = Me.MyListView1.Columns(0).Width
                Else
                    ' Right side of cell is in view.
                    ClickedItem.Width = Me.MyListView1.Columns(1).Width + ClickedItem.Left
                    ClickedItem.X = 2
                End If

            ElseIf (Me.MyListView1.Columns(1).Width > Me.MyListView1.Width) Then
                ClickedItem.Width = Me.MyListView1.Width

            Else
                ClickedItem.Width = Me.MyListView1.Columns(1).Width
                ClickedItem.X = 2
            End If

            ' Adjust the top to account for the location of the ListView.
            ClickedItem.Y += Me.MyListView1.Top
            ClickedItem.X += Me.MyListView1.Left + Me.MyListView1.Columns(0).Width

            ' Assign calculated bounds to the ComboBox.
            Me.cbListViewCombo.Bounds = ClickedItem

            ' Set default text for ComboBox to match the item that is clicked.
            Me.cbListViewCombo.Text = lvItem.Text

            ' Display the ComboBox, and make sure that it is on top with focus.
            Me.cbListViewCombo.Visible = True
            Me.cbListViewCombo.BringToFront()
            Me.cbListViewCombo.Focus()
        End If


    End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:alsam
ID: 33711953
Works
Thank you...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33711984
very welcome!!!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 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