Results 1 to 4 of 4

Thread: [RESOLVED] Could Someone look this Listview/Edit code over for me

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    191

    Resolved [RESOLVED] Could Someone look this Listview/Edit code over for me

    I've got this pretty close to the way i want it to work..but it's still not quit right for some reason..and i know the code is ...well.... crappy lol sorry the switch from vb6 to vb2005 is a bit of a challenge for me.
    Before you look the code over here's how i want it to work:

    Left Mouse Button Single Click..Nothing more than selecting multiple items
    Left Mouse Button Double Click..reserved for opening files from listview
    Right Mouse Button Single Click.. Check/UnCheck all Selected items
    (Right Mouse Button Single Click can also be used to select and check at same time)
    Right Mouse Button Double Click.. Check/UnCheck all items

    Below is what i managed to throw together..i'm hoping Ident or someone else here can make heads or tails out of what i got so far..lol and possible show me how to make work 100% the way i want rather than the 50% it's at right now.
    oh i should also point out that my coding is well like this..throw something together that works..then refine it..this is non-refined version obviously hehe

    I also attached a test pdf so you can fill the ListView with it if you want

    Code:
    Imports System.IO
    
    Public Class Form1
       Dim item As ListViewItem
       Dim CheckAll As Boolean
       Dim CheckMe As Boolean
       Dim A As Long
       Dim Checked As Boolean, CheckedOK As Boolean
       Dim Bip As Integer, Bop As Integer, EditMe As Boolean, I As Long
       Dim SubItem As ListViewItem.ListViewSubItem = Nothing
       Dim Clicker As Boolean
       Private Declare Function ShellExecute _
                       Lib "shell32.dll" _
                       Alias "ShellExecuteA" (ByVal hwnd As Long, _
                                              ByVal lpOperation As String, _
                                              ByVal lpFile As String, _
                                              ByVal lpParameters As String, _
                                              ByVal lpDirectory As String, _
                                              ByVal nShowCmd As Long) As Long
    
       Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
    
          If e.Button = Windows.Forms.MouseButtons.Left Then
             Clicker = False
             CheckAll = False
          End If
    
          If e.Button = Windows.Forms.MouseButtons.Right Then
             Clicker = True
             If CheckedOK = False Then
                CheckAll = False
                CheckMe = CheckAll
                ListViewCheckedState(True)
                CheckedOK = True
             Else
                CheckAll = False
                CheckMe = CheckAll
                ListViewCheckedState(False)
                CheckedOK = False
                Clicker = False
             End If
             ' Next
    
          End If
    
          If TextBox1.Visible = True Then
             'SubItem.Text = TextBox1.Text
             ListView1.Items(Bip).SubItems(Bop).Text = TextBox1.Text
             TextBox1.Visible = False
          End If
    
       End Sub
    
        Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
            Dim HitMe As ListViewHitTestInfo = ListView1.HitTest(e.Location)
            Dim itm As ListViewItem = Me.ListView1.GetItemAt(e.X, e.Y)
            Dim XX As Integer = HitMe.Item.SubItems.IndexOf(HitMe.SubItem)
            Dim info As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
            'itm.Index = Row   XX.ToString = Col Index
    
          If e.Button = Windows.Forms.MouseButtons.Right Then
             If CheckedOK = False Then
                CheckedOK = True
             ElseIf CheckedOK = True Then
                CheckedOK = False
             End If
             If CheckedOK = False Then
                CheckAll = True
                CheckMe = True
                ListViewCheckedState(False)
                CheckedOK = True
             Else
                CheckAll = True
                CheckMe = False
                ListViewCheckedState(True)
                CheckedOK = False
             End If
             ' Next
    
          End If
    
    
          If EditMe = True Then
             Bip = itm.Index
             Bop = XX
             If XX > 0 Then
                TextBox1.Visible = True
                SubItem = info.Item.GetSubItemAt(e.X, e.Y)
                TextBox1.Top = SubItem.Bounds.Y + 26
                TextBox1.Left = SubItem.Bounds.X + 12
                TextBox1.Height = SubItem.Bounds.Size.Height
                TextBox1.Width = SubItem.Bounds.Width + 3
    
                TextBox1.Text = SubItem.Text
             Else
                TextBox1.Visible = True
                itm = ListView1.GetItemAt(e.X, e.Y)
                TextBox1.Left = itm.Bounds.X + 36
                TextBox1.Top = itm.Bounds.Y + 26
                TextBox1.Height = itm.Bounds.Size.Height
                TextBox1.Width = ListView1.Columns(0).Width - 23
    
                TextBox1.Text = itm.Text
             End If
          End If
    
        End Sub
    
       Private Sub ListViewCheckedState(ByVal Checked As String)
          If CheckAll = True Then
             For Each item As ListViewItem In ListView1.Items
                If CheckedOK = True Then
                   item.Checked = True
                ElseIf CheckedOK = False Then
                   item.Checked = False
                End If
             Next
          Else
    
             For Each item As ListViewItem In ListView1.SelectedItems
                If CheckedOK = True Then
                   item.Checked = True
                Else
                   item.Checked = False
                End If
             Next
          End If
       End Sub
    
        Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
    
          If CheckAll = True Then
    
             If CheckedOK = True Then
                e.NewValue = CheckState.Checked
             ElseIf CheckedOK = False Then
                e.NewValue = CheckState.Unchecked
             End If
    
          ElseIf Clicker = False Then
             If ListView1.SelectedItems.Count > 1 Then
                For Each item As ListViewItem In ListView1.SelectedItems
                   If item.Checked = False Then
                      e.NewValue = CheckState.Unchecked
                   Else
                      e.NewValue = CheckState.Checked
                   End If
                Next
             End If
          ElseIf Clicker = True Then
             If CheckedOK = False Then
                e.NewValue = CheckState.Unchecked
             Else
                e.NewValue = CheckState.Checked
             End If
          End If
       End Sub
     
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListView1.Columns.Add("ModelNumber", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("Document", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("ProductType", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("ByteSize", 0, HorizontalAlignment.Left)
            ListView1.Columns.Add("FileSize", 100, HorizontalAlignment.Right)
            ListView1.Columns.Add("Status", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("Location", 0, HorizontalAlignment.Left)
            ListView1.Columns.Add("CrC32", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("FileName", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("WebLink", 0, HorizontalAlignment.Left)
    
        End Sub
    
        Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click
            If CmdEdit.Text = "Edit Off" Then
                EditMe = True
                CmdEdit.Text = "Edit On"
            ElseIf CmdEdit.Text = "Edit On" Then
                EditMe = False
                CmdEdit.Text = "Edit Off"
            End If
        End Sub
    
        Private Sub CmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLoad.Click
            OpenFileDialog1.ShowDialog()
            Dim filepath As String = OpenFileDialog1.FileName
            Dim inputstream As New IO.StreamReader(filepath)
            Dim newstr() As String
    
            Do While inputstream.Peek <> -1
    
                newstr = inputstream.ReadLine().Split("|")
    
                ListView1.Items.Add(newstr(0))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(1))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(2))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(3))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(4))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(5))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(6))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(7))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(8))
                Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(newstr(9))
            Loop
    
            inputstream.Close()
        End Sub
    End Class
    Attached Files Attached Files
    Last edited by RRice; Feb 21st, 2012 at 09:13 PM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    191

    Re: Could Someone look this Listview/Edit code over for me

    one more addition ..i noticed that when i'm in edit mode when i double clicked to open the edit line the check state would change with each double click, so i added this
    Code:
      Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
          If EditMe = True Then
             Clicker = True
             If CheckedOK = False Then
                CheckAll = False
                CheckMe = CheckAll
                ListViewCheckedState(True)
                CheckedOK = True
             Else
                CheckAll = False
                CheckMe = CheckAll
                ListViewCheckedState(False)
                CheckedOK = False
                Clicker = False
             End If
          End If
       End Sub
    This seems to only keep items that are unchecked from becoming checked lol More refinment i guess lol crap doesn't really work at all either grrrr
    Last edited by RRice; Feb 21st, 2012 at 09:54 PM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    191

    Re: Could Someone look this Listview/Edit code over for me

    I'm actually kinda glad this hasnt gotten any reply's yet lol made me think even harder..i scrapped the above so it's only now just for ref..and i focused on the left click first..that is trying to get it stop checking /unchecking when i clicked or double clicked..

    using the code provided by Ident i can now start this again..
    to stop any all checking left or right clicks i did this.
    Code:
     Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked
        ListViewCheckedState(False)
      End Sub
    From here maybe i can sort out all checks and clicks :-)
    Course if this doesn't all work out ....er I'll be back hehe
    Last edited by RRice; Feb 22nd, 2012 at 05:07 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    191

    Re: [RESOLVED] Could Someone look this Listview/Edit code over for me

    I think i found what i was looking for

    not sure if it can be coded a little better or not..but just in case anyone else is looking to do the same thing i was here's what i did.
    Code:
      Private inhibitAutoCheck As Boolean
    
      Private Sub listView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
        inhibitAutoCheck = True
      End Sub
    
      Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        inhibitAutoCheck = True
      End Sub
    
      Private Sub listView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
        If inhibitAutoCheck Then
          If e.CurrentValue = CheckState.Unchecked Then
            e.NewValue = CheckState.Checked
          ElseIf e.CurrentValue = CheckState.Checked Then
            e.NewValue = CheckState.Unchecked
          End If
        End If
      End Sub

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