Solved

Combobox in Listview

Posted on 2010-09-19
8
515 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now