Results 1 to 16 of 16

Thread: How to make listview editable?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    14

    How to make listview editable?

    I am making a Student Information System, for grades, it is a waste of time if I make a label and a textbox for every subject....All I want is to make the listview editable like when I double click the item in the listview I can input a grade...

    ----Pls help me....

  2. #2
    Junior Member UncleBuck1932's Avatar
    Join Date
    Aug 2006
    Location
    United kingdom
    Posts
    28

    Re: How to make listview editable?

    Hi, simple enough mate. Too solve this...

    Select your Listbox and you should see a white box at the top right hand corner of it click it, now your there select Edit Items. There you can input data which you want and press ok.

    Hope it helps.
    John~

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    14

    Re: How to make listview editable?

    I'm sorry but I was using a listview not a listbox, when I run
    the program I want the user can edit the data in the listview Item
    like when I select the subject math then the firstgrading has a zero
    grade and I want to change the grid by double clicking it, then
    I the user can input the grade of the student...

    --pls help me

  4. #4
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    i need this too!
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to make listview editable?

    You need to set the LabelEdit property of your ListView to True, then in the event handler that you want to allow the user to change the text (such as the ListView.DoubleClick event handler), you get the selected item and call BeginEdit method on that item.
    Code:
    Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
            ListView1.SelectedItems(0).BeginEdit()
        End Sub

  6. #6
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    that helps. thanks. but it seems i can only edit the first column.
    what if I have like a transaction log, ie:
    Description, Qty, Unit Cost and want to be able to change the qty to 2 and the unit cost?
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  7. #7
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    I found this code online, but I do not know what to do with it...
    It is in its own class.
    I can do
    Code:
    Dim x1 As New xlistview
            x1.CreateControl()
    but am not sure what else to do. I can not ever see the new control.


    Code:
    Class xlistview
        Inherits ListView
        Dim li As ListViewItem
        Dim x As Integer
        Friend WithEvents editbox As TextBox
        Dim subitemselected As Integer
        Dim subitemtext As String
        Sub New()
            editbox = New TextBox
            editbox.Size = New Size(0, 0)
            editbox.Location = New Point(0, 0)
            Me.Controls.Add(editbox)
            editbox.Hide()
            Me.FullRowSelect = True
            Me.View = View.Details
        End Sub
        Private Sub xlistview_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            li = Me.GetItemAt(e.X, e.Y)
            x = e.X
        End Sub
        Private Sub xlistview_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
            Dim nstart As Integer = x
            Dim spos As Integer = 0
            Dim epos As Integer = Me.Columns(0).Width
            Dim i As Integer
            For i = 0 To Me.Columns.Count - 1
                If nstart > spos And nstart < epos Then
                    subitemselected = i
                    Exit For
                End If
                spos = epos
                epos += Me.Columns(i + 1).Width
            Next
            subitemtext = li.SubItems(subitemselected).Text
            editbox.Size = New Size(epos - spos, li.Bounds.Bottom - li.Bounds.Top)
            editbox.Location = New Point(spos + li.Bounds.X, li.Bounds.Y)
            editbox.Show()
            editbox.Text = subitemtext
            editbox.SelectAll()
            editbox.Focus()
        End Sub
        Private Sub editbox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles editbox.KeyDown
            If e.KeyCode = Keys.Enter Then
                li.SubItems(subitemselected).Text = editbox.Text
                editbox.Hide()
            End If
            If e.KeyCode = Keys.Escape Then
                editbox.Hide()
            End If
        End Sub
        Private Sub editbox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles editbox.Leave
            li.SubItems(subitemselected).Text = editbox.Text
            editbox.Hide()
        End Sub
    End Class
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  8. #8
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    ahhh - now I got it.
    Code:
            Dim xx As New xlistview
            xx.Name = "LV1"
            xx.Size = New System.Drawing.Size(500, 300)
    
            xx.Location = New System.Drawing.Point(20, 20)
            xx.View = View.Details
            xx.Columns.Add("A", 120, HorizontalAlignment.Left)
            xx.Columns.Add("B", 120, HorizontalAlignment.Left)
            xx.Columns.Add("C", 120, HorizontalAlignment.Left)
            xx.Columns.Add("D", 120, HorizontalAlignment.Left)
    
    
            xx.FullRowSelect = True
            xx.GridLines = True
            xx.LabelEdit = True
    
    
            Me.Controls.Add(xx)
    Then add some info to the boxes
    Code:
    Dim i As Integer
            Dim itmx As New ListViewItem
    
            For i = 0 To 19
                itmx = xx.Items.Add("Enter Name # " & i + 1)
    
                itmx.SubItems.Add("some info")
                itmx.SubItems.Add("more info")
                itmx.SubItems.Add("some info")
                itmx.SubItems.Add("more info")
            Next
    A double click on any cell will make it editable.
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to make listview editable?

    Quote Originally Posted by craigreilly
    that helps. thanks. but it seems i can only edit the first column.
    what if I have like a transaction log, ie:
    Description, Qty, Unit Cost and want to be able to change the qty to 2 and the unit cost?
    You have 2 options here:

    1. (The easy way) Get rid of the ListView and use a DataGridView instead. The ListView control will let you edit the ListViewItem only (aka the 1st column if you have the View property set to Details), not the subitems of the listviewitem. In order to change the subitems, you need to code it yourself. (which go to option 2)

    2. Create another form with textboxes coresponding to the subitems in your listviewitem. When you double click on a listviewitem, open this form for the user to enter in the new information. When the user close this form, you read the entered data then change your listviewitem's data in code.

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to make listview editable?

    Quote Originally Posted by craigreilly
    ahhh - now I got it.
    Code:
            Dim xx As New xlistview
            xx.Name = "LV1"
            xx.Size = New System.Drawing.Size(500, 300)
    
            xx.Location = New System.Drawing.Point(20, 20)
            xx.View = View.Details
            xx.Columns.Add("A", 120, HorizontalAlignment.Left)
            xx.Columns.Add("B", 120, HorizontalAlignment.Left)
            xx.Columns.Add("C", 120, HorizontalAlignment.Left)
            xx.Columns.Add("D", 120, HorizontalAlignment.Left)
    
    
            xx.FullRowSelect = True
            xx.GridLines = True
            xx.LabelEdit = True
    
    
            Me.Controls.Add(xx)
    Then add some info to the boxes
    Code:
    Dim i As Integer
            Dim itmx As New ListViewItem
    
            For i = 0 To 19
                itmx = xx.Items.Add("Enter Name # " & i + 1)
    
                itmx.SubItems.Add("some info")
                itmx.SubItems.Add("more info")
                itmx.SubItems.Add("some info")
                itmx.SubItems.Add("more info")
            Next
    A double click on any cell will make it editable.
    No, it's not... You create a new listview and don't set its View property to Details, so the default value is used (which is LargeIcon). So when you doubleclick on a "cell", you are actually double clicking on a listviewitem, and what got edited is just the listviewitem itself (column 0 in details view), not the subitems of the listviewitem.

    EDIT: Oh, OK... You're using a custom listview, not the standard listview. So I guess it will work.

  11. #11
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    Can this now be made into a Custom Control so I do not need the "Class" in every project I use it on?
    I only have VB 2003 Standard. If anyone knows how to - please...
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  12. #12
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    StanAV - not sure what you mean. Using the code I attached, I am able to go into an individual cell and update the content.
    Now I just need a way to detect the changes and save it to my DB.
    (of course the data in there right now is just test data)...
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  13. #13
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to make listview editable?

    Quote Originally Posted by craigreilly
    StanAV - not sure what you mean. Using the code I attached, I am able to go into an individual cell and update the content.
    Now I just need a way to detect the changes and save it to my DB.
    (of course the data in there right now is just test data)...
    Yeah, when I first read your post, I thought you're using the standard ListView control that is included with .net framework. Not until my reply posted did I realize that you use a custom listview (which inherits the standard listview)... That's when I put the "EDIT:" line at the end of my post.

  14. #14
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    we keep missing each other.
    Any idea how to create a Custom control from this?
    I only have Vb Pro and not VS . I do not see a way to create a control. Right now, I create this control in my form_load and it is not accessible anywhere else (ie: to save the information back to db).

    When I try "Public xx as new xlistview", it tells me:
    "Cannot expose a friend type outside of the public class 'Form1'"

    The info I find on this is sketchy and seems to be C related.
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  15. #15
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: How to make listview editable?

    i just realized you are in PVD. I grew up in Cranston...
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  16. #16
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to make listview editable?

    I you put the code for that class as a standalone class then you should be able to do
    Code:
    Public xx as new xListView()
    Here's the steps:
    1. Click on the Project menu and choose "Add Class"
    2. Name your class "xListView.vb"
    3. Cut and paste the code you have for the xListView class (in post #7)
    4. And don't forget to declare your class public. That is
    Code:
    'Change this line
    Class xListView
    
    'To this
    Public Class xListView

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width