alsam
asked on
Combobox in Listview
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...
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
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!!
ASKER
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....
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....
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
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
ASKER
Great...
but is there a way to provide combobox only for e.g colum 2...
All other colums should not have combo....
but is there a way to provide combobox only for e.g colum 2...
All other colums should not have combo....
that is what i am looking for .....:)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works
Thank you...
Thank you...
very welcome!!!