473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Listview Question

Hi all,

I want to implement a listview with editable subitems and I assume the
easiest way is to overlay a textbox over the item to be edited.

With this in mind I have come up with:

Using fullrowselect=true in the listview

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedE ventArgs) _
Handles ListView1.ItemSelectionChanged
Dim t As New TextBox
Me.Controls.Add(t)
t.Location = e.Item.Position
t.BringToFront()
End Sub

Unfortunately item.position only enables me to get the position of the first
column. How can I obtain the x position of the subitem that was clicked.

I have searched google, but the only examples I can find are in C++ which I
can't make any sense of.

Thanks,

Martin.
Mar 20 '06 #1
5 2349
I have found this VB6 example, but I can't figure out how to convert it to
..net, can anyone help me.

Thanks

'Declarations:
Private Type lvwItemClick
x As Long
y As Long
Flgs As Long
Itm As Long
SubItm As Long
End Type

Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal lMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Dim lvwClick As lvwItemClick

Private Sub ListView1_MouseDown(Button As Integer, _
Shift As Integer, x As Single, y As Single)

lvwClick .x = x / Screen.TwipsPerPixelX
lvwClick .y = y / Screen.TwipsPerPixelY

SendMessage ListView1.hwnd, 4153, 0, lvwClick

If Button = vbRightButton And lvwClick .Itm = 2 And lvwClick .SubItm = 1
Then _
PopupMenu mnuTest 'poppup menu mnuTest for click on Item 3, SubItem 1.

End Sub
"Martin Horn" <mv****@theinternet.com> wrote in message
news:Gc*****************@newsfe7-win.ntli.net...
Hi all,

I want to implement a listview with editable subitems and I assume the
easiest way is to overlay a textbox over the item to be edited.

With this in mind I have come up with:

Using fullrowselect=true in the listview

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedE ventArgs) _
Handles ListView1.ItemSelectionChanged
Dim t As New TextBox
Me.Controls.Add(t)
t.Location = e.Item.Position
t.BringToFront()
End Sub

Unfortunately item.position only enables me to get the position of the
first column. How can I obtain the x position of the subitem that was
clicked.

I have searched google, but the only examples I can find are in C++ which
I can't make any sense of.

Thanks,

Martin.

Mar 20 '06 #2
I'm working on the same thing... Not yet complete, but it's getting there.

Private Sub lstRules_MouseUp(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles lstRules.MouseUp
Dim ThisItem As System.Windows.Forms.ListViewItem
Dim NewBox As Control

If Not (e.Button = Windows.Forms.MouseButtons.Left) Then Exit Sub

ThisItem = lstRules.HitTest(e.X, e.Y).Item
If ThisItem Is Nothing Then
Exit Sub
End If

NewBox = New System.Windows.Forms.TextBox
NewBox.Size = New System.Drawing.Size(lstRules.Columns(1).Width - 10,
12)
NewBox.Text = KeyItem.ItemText
NewBox.Location = New System.Drawing.Point(ThisItem.Position.X + _
lstRules.Columns(0).Width + 2,
ThisItem.Position.Y + 1)

Me.Controls.Add(NewBox)
Me.NewBox.BringToFront
AddHandler NewBox.LostFocus, AddressOf DestroyBox
NewBox.Focus()
End Sub

Private Sub DestroyBox(ByVal sender As Object, ByVal e As System.EventArgs)
If sender IsNot Nothing Then
Me.Controls.Remove(sender)
sender.Dispose()
End If
End Sub

Work in progress: Moving the textbox when the user scrolls the Listview.
If you're interested I can post that bit tomorrow

Hth,
Martin
"Martin Horn" <mv****@theinternet.com> wrote in message
news:Gc*****************@newsfe7-win.ntli.net...
Hi all,

I want to implement a listview with editable subitems and I assume the
easiest way is to overlay a textbox over the item to be edited.

With this in mind I have come up with:

Using fullrowselect=true in the listview

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedE ventArgs) _
Handles ListView1.ItemSelectionChanged
Dim t As New TextBox
Me.Controls.Add(t)
t.Location = e.Item.Position
t.BringToFront()
End Sub

Unfortunately item.position only enables me to get the position of the
first column. How can I obtain the x position of the subitem that was
clicked.

I have searched google, but the only examples I can find are in C++ which
I can't make any sense of.

Thanks,

Martin.

Mar 20 '06 #3
Hi Martin,

I have come up with something similar myself, but I can't see how to easily
adapt it to work with scrolling of the listview, so I would definitely be
interested to see your solution.

Here is my version anyway, which retrieves the text from the any of the 6
subitems in my listview and fills the textbox with it when the user double
clicks the listview subitem.

Note: My example doesn't dynamically create the textbox and I have omitted
the code to remove the textbox.

Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListView1.MouseDoubleClick
Dim ItemX As ListViewItem
ItemX = ListView1.GetItemAt(e.X, e.Y)
If ItemX Is Nothing Then Exit Sub

Dim col As Integer = GetColumnNumber(e.X)

EditBox.Top = ItemX.Position.Y
EditBox.Left = GetColumnX(e.X) + 2
EditBox.Text = ListView1.SelectedItems(0).SubItems(col).Text
EditBox.Width = ListView1.Columns(col).Width
EditBox.BringToFront()
EditBox.Focus()
End Sub

Private Function GetColumnNumber(ByVal MouseX As Integer) As Integer
If MouseX < GetTotalWidth(0) Then Return -1
If MouseX < GetTotalWidth(1) Then Return 0
If MouseX < GetTotalWidth(2) Then Return 1
If MouseX < GetTotalWidth(3) Then Return 2
If MouseX < GetTotalWidth(4) Then Return 3
If MouseX < GetTotalWidth(5) Then Return 4
If MouseX < GetTotalWidth(6) Then Return 5
End Function

Private Function GetColumnX(ByVal MouseX As Integer) As Integer
If MouseX < GetTotalWidth(0) Then Return 0
If MouseX < GetTotalWidth(1) Then Return GetTotalWidth(0)
If MouseX < GetTotalWidth(2) Then Return GetTotalWidth(1)
If MouseX < GetTotalWidth(3) Then Return GetTotalWidth(2)
If MouseX < GetTotalWidth(4) Then Return GetTotalWidth(3)
If MouseX < GetTotalWidth(5) Then Return GetTotalWidth(4)
If MouseX < GetTotalWidth(6) Then Return GetTotalWidth(5)
End Function

Private Function GetTotalWidth(ByVal Cols As Integer) As Integer
Dim tot As Integer = 0
For n As Integer = 0 To Cols - 1
tot += ListView1.Columns(n).Width
Next
Return tot
End Function

"Martin" <x@y.com> wrote in message
news:ex**************@TK2MSFTNGP09.phx.gbl...
I'm working on the same thing... Not yet complete, but it's getting there.

Private Sub lstRules_MouseUp(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles lstRules.MouseUp
Dim ThisItem As System.Windows.Forms.ListViewItem
Dim NewBox As Control

If Not (e.Button = Windows.Forms.MouseButtons.Left) Then Exit Sub

ThisItem = lstRules.HitTest(e.X, e.Y).Item
If ThisItem Is Nothing Then
Exit Sub
End If

NewBox = New System.Windows.Forms.TextBox
NewBox.Size = New System.Drawing.Size(lstRules.Columns(1).Width - 10,
12)
NewBox.Text = KeyItem.ItemText
NewBox.Location = New System.Drawing.Point(ThisItem.Position.X + _
lstRules.Columns(0).Width + 2,
ThisItem.Position.Y + 1)

Me.Controls.Add(NewBox)
Me.NewBox.BringToFront
AddHandler NewBox.LostFocus, AddressOf DestroyBox
NewBox.Focus()
End Sub

Private Sub DestroyBox(ByVal sender As Object, ByVal e As
System.EventArgs)
If sender IsNot Nothing Then
Me.Controls.Remove(sender)
sender.Dispose()
End If
End Sub

Work in progress: Moving the textbox when the user scrolls the Listview.
If you're interested I can post that bit tomorrow

Hth,
Martin
"Martin Horn" <mv****@theinternet.com> wrote in message
news:Gc*****************@newsfe7-win.ntli.net...
Hi all,

I want to implement a listview with editable subitems and I assume the
easiest way is to overlay a textbox over the item to be edited.

With this in mind I have come up with:

Using fullrowselect=true in the listview

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedE ventArgs) _
Handles ListView1.ItemSelectionChanged
Dim t As New TextBox
Me.Controls.Add(t)
t.Location = e.Item.Position
t.BringToFront()
End Sub

Unfortunately item.position only enables me to get the position of the
first column. How can I obtain the x position of the subitem that was
clicked.

I have searched google, but the only examples I can find are in C++ which
I can't make any sense of.

Thanks,

Martin.


Mar 21 '06 #4
I have changed my objective a bit. Since detecting a scroll event in a
Listview requires me to subclass the Listview (MS simply forgot to raise a
scroll event, although it is built in the API) I am going to do it right.
I'm building a subclass of the Listview that allows any kind of control
(checkbox, combobox, datetimepicker, textbox) to be added as subitem.

If you're interested I will publish the code on my site when I'm done.
"Martin Horn" <mv****@theinternet.com> schrieb im Newsbeitrag
news:a8*****************@newsfe2-gui.ntli.net...
Hi Martin,

I have come up with something similar myself, but I can't see how to
easily adapt it to work with scrolling of the listview, so I would
definitely be interested to see your solution.

Here is my version anyway, which retrieves the text from the any of the 6
subitems in my listview and fills the textbox with it when the user double
clicks the listview subitem.

Note: My example doesn't dynamically create the textbox and I have omitted
the code to remove the textbox.

Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListView1.MouseDoubleClick
Dim ItemX As ListViewItem
ItemX = ListView1.GetItemAt(e.X, e.Y)
If ItemX Is Nothing Then Exit Sub

Dim col As Integer = GetColumnNumber(e.X)

EditBox.Top = ItemX.Position.Y
EditBox.Left = GetColumnX(e.X) + 2
EditBox.Text = ListView1.SelectedItems(0).SubItems(col).Text
EditBox.Width = ListView1.Columns(col).Width
EditBox.BringToFront()
EditBox.Focus()
End Sub

Private Function GetColumnNumber(ByVal MouseX As Integer) As Integer
If MouseX < GetTotalWidth(0) Then Return -1
If MouseX < GetTotalWidth(1) Then Return 0
If MouseX < GetTotalWidth(2) Then Return 1
If MouseX < GetTotalWidth(3) Then Return 2
If MouseX < GetTotalWidth(4) Then Return 3
If MouseX < GetTotalWidth(5) Then Return 4
If MouseX < GetTotalWidth(6) Then Return 5
End Function

Private Function GetColumnX(ByVal MouseX As Integer) As Integer
If MouseX < GetTotalWidth(0) Then Return 0
If MouseX < GetTotalWidth(1) Then Return GetTotalWidth(0)
If MouseX < GetTotalWidth(2) Then Return GetTotalWidth(1)
If MouseX < GetTotalWidth(3) Then Return GetTotalWidth(2)
If MouseX < GetTotalWidth(4) Then Return GetTotalWidth(3)
If MouseX < GetTotalWidth(5) Then Return GetTotalWidth(4)
If MouseX < GetTotalWidth(6) Then Return GetTotalWidth(5)
End Function

Private Function GetTotalWidth(ByVal Cols As Integer) As Integer
Dim tot As Integer = 0
For n As Integer = 0 To Cols - 1
tot += ListView1.Columns(n).Width
Next
Return tot
End Function

"Martin" <x@y.com> wrote in message
news:ex**************@TK2MSFTNGP09.phx.gbl...
I'm working on the same thing... Not yet complete, but it's getting
there.

Private Sub lstRules_MouseUp(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles lstRules.MouseUp
Dim ThisItem As System.Windows.Forms.ListViewItem
Dim NewBox As Control

If Not (e.Button = Windows.Forms.MouseButtons.Left) Then Exit Sub

ThisItem = lstRules.HitTest(e.X, e.Y).Item
If ThisItem Is Nothing Then
Exit Sub
End If

NewBox = New System.Windows.Forms.TextBox
NewBox.Size = New System.Drawing.Size(lstRules.Columns(1).Width - 10,
12)
NewBox.Text = KeyItem.ItemText
NewBox.Location = New System.Drawing.Point(ThisItem.Position.X + _
lstRules.Columns(0).Width + 2,
ThisItem.Position.Y + 1)

Me.Controls.Add(NewBox)
Me.NewBox.BringToFront
AddHandler NewBox.LostFocus, AddressOf DestroyBox
NewBox.Focus()
End Sub

Private Sub DestroyBox(ByVal sender As Object, ByVal e As
System.EventArgs)
If sender IsNot Nothing Then
Me.Controls.Remove(sender)
sender.Dispose()
End If
End Sub

Work in progress: Moving the textbox when the user scrolls the Listview.
If you're interested I can post that bit tomorrow

Hth,
Martin
"Martin Horn" <mv****@theinternet.com> wrote in message
news:Gc*****************@newsfe7-win.ntli.net...
Hi all,

I want to implement a listview with editable subitems and I assume the
easiest way is to overlay a textbox over the item to be edited.

With this in mind I have come up with:

Using fullrowselect=true in the listview

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedE ventArgs) _
Handles ListView1.ItemSelectionChanged
Dim t As New TextBox
Me.Controls.Add(t)
t.Location = e.Item.Position
t.BringToFront()
End Sub

Unfortunately item.position only enables me to get the position of the
first column. How can I obtain the x position of the subitem that was
clicked.

I have searched google, but the only examples I can find are in C++
which I can't make any sense of.

Thanks,

Martin.



Mar 21 '06 #5
Although my solution works in the context in which I am using it, I am well
aware that it isn't very efficient and would be very interested in seeing
how you approach the problem, as I am a newbie programmer and what you are
suggesting is well beyond my capabilities at the moment.

If you want to email me the link to your site you can send it to
mvhorn@remove_this_bit.ntlworld.com or post it here instead.

Kind regards,

Martin.

"Martin" <x@y.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have changed my objective a bit. Since detecting a scroll event in a
Listview requires me to subclass the Listview (MS simply forgot to raise a
scroll event, although it is built in the API) I am going to do it right.
I'm building a subclass of the Listview that allows any kind of control
(checkbox, combobox, datetimepicker, textbox) to be added as subitem.

If you're interested I will publish the code on my site when I'm done.
"Martin Horn" <mv****@theinternet.com> schrieb im Newsbeitrag
news:a8*****************@newsfe2-gui.ntli.net...
Hi Martin,

I have come up with something similar myself, but I can't see how to
easily adapt it to work with scrolling of the listview, so I would
definitely be interested to see your solution.

Here is my version anyway, which retrieves the text from the any of the 6
subitems in my listview and fills the textbox with it when the user
double clicks the listview subitem.

Note: My example doesn't dynamically create the textbox and I have
omitted the code to remove the textbox.

Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListView1.MouseDoubleClick
Dim ItemX As ListViewItem
ItemX = ListView1.GetItemAt(e.X, e.Y)
If ItemX Is Nothing Then Exit Sub

Dim col As Integer = GetColumnNumber(e.X)

EditBox.Top = ItemX.Position.Y
EditBox.Left = GetColumnX(e.X) + 2
EditBox.Text = ListView1.SelectedItems(0).SubItems(col).Text
EditBox.Width = ListView1.Columns(col).Width
EditBox.BringToFront()
EditBox.Focus()
End Sub

Private Function GetColumnNumber(ByVal MouseX As Integer) As Integer
If MouseX < GetTotalWidth(0) Then Return -1
If MouseX < GetTotalWidth(1) Then Return 0
If MouseX < GetTotalWidth(2) Then Return 1
If MouseX < GetTotalWidth(3) Then Return 2
If MouseX < GetTotalWidth(4) Then Return 3
If MouseX < GetTotalWidth(5) Then Return 4
If MouseX < GetTotalWidth(6) Then Return 5
End Function

Private Function GetColumnX(ByVal MouseX As Integer) As Integer
If MouseX < GetTotalWidth(0) Then Return 0
If MouseX < GetTotalWidth(1) Then Return GetTotalWidth(0)
If MouseX < GetTotalWidth(2) Then Return GetTotalWidth(1)
If MouseX < GetTotalWidth(3) Then Return GetTotalWidth(2)
If MouseX < GetTotalWidth(4) Then Return GetTotalWidth(3)
If MouseX < GetTotalWidth(5) Then Return GetTotalWidth(4)
If MouseX < GetTotalWidth(6) Then Return GetTotalWidth(5)
End Function

Private Function GetTotalWidth(ByVal Cols As Integer) As Integer
Dim tot As Integer = 0
For n As Integer = 0 To Cols - 1
tot += ListView1.Columns(n).Width
Next
Return tot
End Function

"Martin" <x@y.com> wrote in message
news:ex**************@TK2MSFTNGP09.phx.gbl...
I'm working on the same thing... Not yet complete, but it's getting
there.

Private Sub lstRules_MouseUp(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles lstRules.MouseUp
Dim ThisItem As System.Windows.Forms.ListViewItem
Dim NewBox As Control

If Not (e.Button = Windows.Forms.MouseButtons.Left) Then Exit Sub

ThisItem = lstRules.HitTest(e.X, e.Y).Item
If ThisItem Is Nothing Then
Exit Sub
End If

NewBox = New System.Windows.Forms.TextBox
NewBox.Size = New System.Drawing.Size(lstRules.Columns(1).Width - 10,
12)
NewBox.Text = KeyItem.ItemText
NewBox.Location = New System.Drawing.Point(ThisItem.Position.X + _
lstRules.Columns(0).Width + 2,
ThisItem.Position.Y + 1)

Me.Controls.Add(NewBox)
Me.NewBox.BringToFront
AddHandler NewBox.LostFocus, AddressOf DestroyBox
NewBox.Focus()
End Sub

Private Sub DestroyBox(ByVal sender As Object, ByVal e As
System.EventArgs)
If sender IsNot Nothing Then
Me.Controls.Remove(sender)
sender.Dispose()
End If
End Sub

Work in progress: Moving the textbox when the user scrolls the Listview.
If you're interested I can post that bit tomorrow

Hth,
Martin
"Martin Horn" <mv****@theinternet.com> wrote in message
news:Gc*****************@newsfe7-win.ntli.net...
Hi all,

I want to implement a listview with editable subitems and I assume the
easiest way is to overlay a textbox over the item to be edited.

With this in mind I have come up with:

Using fullrowselect=true in the listview

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedE ventArgs)
_
Handles ListView1.ItemSelectionChanged
Dim t As New TextBox
Me.Controls.Add(t)
t.Location = e.Item.Position
t.BringToFront()
End Sub

Unfortunately item.position only enables me to get the position of the
first column. How can I obtain the x position of the subitem that was
clicked.

I have searched google, but the only examples I can find are in C++
which I can't make any sense of.

Thanks,

Martin.



Mar 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andrewcw | last post by:
I have a simple winform with the following code. But although I can read back the info, the display fails to provide the text or the cell background color changes. private void ListViewBroke()...
1
by: J_Max | last post by:
Hello, This might be a really easy question, but... I am developing a simple Smart Device application that uses a listview. I have a function that adds a item to the listview - code is below. I...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
3
by: MikeY | last post by:
Hopefully someone can help me on this. I am using C#, making Windows forms. I have created a listView with checkbox's. I have enabled the checkboxes under the properties, and all the data,...
1
by: Derck | last post by:
SORRY, for the crosspost, but I think I posted it in the wrong group! Hello all, I have a question.. I am tying to make a global listview class where other listviews in my application points...
12
by: J L | last post by:
When I fill a listview, I resize the columns to fit the data. I need to know if the data will fit vertically or if there will be a vertical scroll bar. I need to know this so I can allow for it on...
1
by: Chris | last post by:
Hi all, I posted the following in microsoft.public.dotnet.framework.windowsforms but it seems that group has little traffic. Hi all, I have a listview box which is populated from methods of...
12
by: garyusenet | last post by:
I have had no replies to my previous post so perhaps I didn't write it good enough. Please excuse new thread but i wanted to break from the last thread hopefully this thread will be better. ...
1
by: =?Utf-8?B?THluYkBtcy5jb20=?= | last post by:
I have a executable winforms application I would like to change. I use quite a number of listview controls in my main form. I dump about 15 columns of data into a couple of listviews. This data...
5
by: Mark Olbert | last post by:
How do I get the DataPager and ListView to play nice together when I use a custom datasource? In my webpage, I use linq to pull data from a SqlServer database and assign the resulting...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.