Results 1 to 20 of 20

Thread: ListView

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    ListView

    Can anyone tell me how to get my code to work in a listview please.

    VB Code:
    1. Option Explicit
    2. Dim mPerson As PersonalInfo
    3. Dim mFileNum As Integer
    4. Dim mRecordLen As Long
    5. Dim mCurrentRecord As Long
    6. Dim mLastRecord As Long
    7. Dim ShiftTest As Integer
    8.  
    9. Private Sub cmdSave_Click()
    10.  
    11.     MsgBox ("Members Saved"), vbInformation, "Douglas Lads Club"
    12.  
    13.     SaveCurrentRecord
    14.  
    15. End Sub
    16.  
    17.  
    18. Public Sub SaveCurrentRecord()
    19.  
    20.     mPerson.MemberID = txtID.Text
    21.     mPerson.MembersName = txtName.Text
    22.     mPerson.MembersAddress = txtAddress.Text
    23.     mPerson.MembersPostalCode = txtPostal.Text
    24.     mPerson.MembersHomePhone = txtHome.Text
    25.     mPerson.MembersMobileNumber = txtMobile.Text
    26.  
    27.     Put #mFileNum, mCurrentRecord, mPerson
    28.  
    29. End Sub
    30.  
    31. Public Sub ShowCurrentRecord()
    32.  
    33.     Get #mFileNum, mCurrentRecord, mPerson
    34.  
    35.     txtID.Text = Trim(mPerson.MemberID)
    36.     txtName.Text = Trim(mPerson.MembersName)
    37.     txtAddress.Text = Trim(mPerson.MembersAddress)
    38.     txtPostal.Text = Trim(mPerson.MembersPostalCode)
    39.     txtHome.Text = Trim(mPerson.MembersHomePhone)
    40.     txtMobile.Text = Trim(mPerson.MembersMobileNumber)
    41.  
    42.  
    43. frmAddressBook.Caption = "Douglas Lads Club" + Str(mCurrentRecord) + "/" + Str(mLastRecord)
    44. End Sub
    45.  
    46. Private Sub cmdBack_Click()
    47.    
    48.     If mCurrentRecord = 1 Then
    49.    
    50.     Beep
    51.    
    52.     MsgBox "This is the first record !", vbExclamation, "Douglas Lads Club"
    53.    
    54.     Else
    55.    
    56.     SaveCurrentRecord
    57.    
    58.     mCurrentRecord = mCurrentRecord - 1
    59.    
    60.     ShowCurrentRecord
    61.    
    62.     End If
    63.    
    64.     txtID.SetFocus
    65.    
    66. End Sub
    67.  
    68. Private Sub cmdDelete_Click()
    69.  
    70. Dim DirResult
    71. Dim TmpFileNum
    72. Dim TmpPerson As PersonalInfo
    73. Dim RecNum As Long
    74. Dim TmpRecNum As Long
    75.  
    76.  
    77. If MsgBox("Delete the current record?", vbYesNo + vbCritical, "Douglas Lads Club") = vbNo Then
    78.  
    79.  
    80. txtID.SetFocus
    81.  
    82.  
    83.  
    84. Exit Sub
    85. End If
    86.  
    87. If Dir("Data/addressbook.tmp") = "addressbook.tmpP" Then
    88.  
    89. Kill "Data/addressbook.tmp"
    90. End If
    91.  
    92. TmpFileNum = FreeFile
    93.  
    94. Open "Data/addressbook.tmp" For Random As TmpFileNum Len = mRecordLen
    95.  
    96. RecNum = 1
    97. TmpRecNum = 1
    98. Do While RecNum < mLastRecord + 1
    99. If RecNum <> mCurrentRecord Then
    100. Get #mFileNum, RecNum, TmpPerson
    101. Put #TmpFileNum, TmpRecNum, TmpPerson
    102. TmpRecNum = TmpRecNum + 1
    103. End If
    104. RecNum = RecNum + 1
    105. Loop
    106.  
    107.  
    108. Close mFileNum
    109. Kill "Data/addressbook.dat"
    110.  
    111.  
    112.  
    113. Close TmpFileNum
    114.  
    115. Name "Data/addressbook.tmp" As "Data/addressbook.dat"
    116.  
    117.  
    118. mFileNum = FreeFile
    119.  
    120. Open "Data/addressbook.dat" For Random As mFileNum Len = mRecordLen
    121.  
    122. mLastRecord = mLastRecord - 1
    123.  
    124.  
    125. If mLastRecord = 0 Then mLastRecord = 1
    126.  
    127. If mCurrentRecord > mLastRecord Then
    128.    mCurrentRecord = mLastRecord
    129. End If
    130. ShowCurrentRecord
    131. txtID.SetFocus
    132.    
    133. End Sub
    134.  
    135. Private Sub cmdSearch_Click()
    136.  
    137.     Dim NameToSearch As String
    138.    
    139.     Dim Found As Integer
    140.    
    141.     Dim RecNum As Long
    142.    
    143.     Dim TmpPerson As PersonalInfo
    144.    
    145.     NameToSearch = InputBox("Search for Member ID :", "Search Members")
    146.    
    147.     If NameToSearch = "" Then
    148.    
    149.     txtID.SetFocus
    150.    
    151.     Exit Sub
    152.    
    153.     End If
    154.    
    155.     NameToSearch = UCase(NameToSearch)
    156.    
    157.     Found = False
    158.    
    159.     For RecNum = 1 To mLastRecord
    160.    
    161.     Get #mFileNum, RecNum, TmpPerson
    162.    
    163.     If NameToSearch = UCase(Trim(TmpPerson.MemberID)) Then
    164.    
    165.     Found = True
    166.    
    167.     Exit For
    168.    
    169.     End If
    170.    
    171.     Next
    172.    
    173.     If Found = True Then
    174.    
    175.     SaveCurrentRecord
    176.    
    177.     mCurrentRecord = RecNum
    178.    
    179.     ShowCurrentRecord
    180.    
    181.     Else
    182.    
    183.     MsgBox "The Member ID you entered  could not be found!" & vbCrLf & vbCrLf & "Tip : Enter the correct Member Id Number to search!", vbInformation, "Douglas Lads Club"
    184.    
    185.     End If
    186.    
    187.     txtID.SetFocus
    188.  
    189. End Sub
    190.  
    191. Private Sub Form_Paint()
    192.  
    193.     Shadow Me, txtID, 2, vbBlack
    194.     Shadow Me, txtName, 2, vbBlack
    195.     Shadow Me, txtAddress, 2, vbBlack
    196.     Shadow Me, txtPostal, 2, vbBlack
    197.     Shadow Me, txtHome, 2, vbBlack
    198.     Shadow Me, txtMobile, 2, vbBlack
    199.    
    200. End Sub
    201.  
    202. Private Sub Form_Unload(Cancel As Integer)
    203.  
    204.     SaveCurrentRecord
    205.  
    206. End Sub
    207.  
    208. Private Sub cmdExit_Click()
    209.     SaveCurrentRecord
    210.     Unload Me
    211. End Sub
    212.  
    213. Private Sub cmdNew_Click()
    214.    
    215.     SaveCurrentRecord
    216.    
    217.     mLastRecord = mLastRecord + 1
    218.    
    219.     mPerson.MemberID = ""
    220.    
    221.     mPerson.MembersName = ""
    222.    
    223.     mPerson.MembersAddress = ""
    224.    
    225.     mPerson.MembersPostalCode = ""
    226.    
    227.     mPerson.MembersHomePhone = ""
    228.    
    229.     mPerson.MembersMobileNumber = ""
    230.    
    231.     Put #mFileNum, mLastRecord, mPerson
    232.    
    233.     mCurrentRecord = mLastRecord
    234.    
    235.     ShowCurrentRecord
    236.    
    237.     txtID.SetFocus
    238.    
    239. End Sub
    240.  
    241. Private Sub cmdNext_Click()
    242.    
    243.     If mCurrentRecord = mLastRecord Then
    244.    
    245.     Beep
    246.    
    247.     MsgBox "Its the last record !", vbExclamation, "Douglas Lads Club"
    248.    
    249.     Else
    250.    
    251.     SaveCurrentRecord
    252.    
    253.     mCurrentRecord = mCurrentRecord + 1
    254.    
    255.     ShowCurrentRecord
    256.    
    257.     End If
    258.    
    259.     txtID.SetFocus
    260.    
    261. End Sub
    262.  
    263. Private Sub Form_Load()
    264.  
    265.     mRecordLen = Len(mPerson)
    266.    
    267.     mFileNum = FreeFile
    268.    
    269.     Open "Data/addressbook.dat" For Random As mFileNum Len = mRecordLen
    270.    
    271.     mCurrentRecord = 1
    272.    
    273.     mLastRecord = FileLen("Data/addressbook.dat") / mRecordLen
    274.    
    275.     If mLastRecord = 0 Then
    276.    
    277.     mLastRecord = 1
    278.    
    279.     End If
    280.    
    281.     ShowCurrentRecord
    282.    
    283. End Sub

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    On the assumption that you already know how to add the listview to your components tab...

    To retaining existing code... place all textboxes relevant to mPerson in a picture box... make another picture box and place the listview control in this 2nd pic box... then add this in your Form_Load() event after call to ShowCurrentRecord.

    VB Code:
    1. Dim lstNew As ListItem
    2.  
    3. 'following hides textboxes and shows listview
    4. Picture1.Visible = False
    5. Picture2.Visible = True
    6.  
    7. ListView1.View = lvwReport
    8. ListView1.LabelEdit = lvwManual
    9. With ListView1.ColumnHeaders
    10.    .Clear
    11.    .Add , , "ID"
    12.    .Add , , "Name"
    13.    .Add , , "Address"
    14.    .Add , , "Postal Code"
    15.    .Add , , "Home Phone"
    16.    .Add , , "Mobile Num"
    17. End With
    18.  
    19. For mCurrentRecord = 1 To mLastRecord
    20.    Get #mFileNum, mCurrentRecord, mPerson
    21.    Set lstNew = ListView1.ListItems.Add
    22.  
    23.    lstNew.Text = Trim(mPerson.MemberID)
    24.    lstNew.Tag = mCurrentRecord
    25.    lstNew.SubItems(1) = Trim(mPerson.MembersName)
    26.    lstNew.SubItems(2) = Trim(mPerson.MembersAddress)
    27.    lstNew.SubItems(3) = Trim(mPerson.MembersPostalCode)
    28.    lstNew.SubItems(4) = Trim(mPerson.MembersHomePhone)
    29.    lstNew.SubItems(5) = Trim(mPerson.MembersMobileNumber)
    30. Next
    31.  
    32. mCurrentRecord = 1  'reset current to start

    Try to get a feel for the listview first. All other codes will have to be updated also... we will remove textbox ties to the data source bit by bit and redirect them to the listview.
    Last edited by leinad31; Oct 7th, 2006 at 09:35 AM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    Say i clicked save record is there anyway that i can get the listview to refresh itself when i have clicked the command button.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    You have two choices...

    1) update listview items then update the entire file by downloading all listview info

    2) update immediately the record in the file per listitem update

    So which implementation would you prefer? Also in what order do you want the update? File first before listview or theother way around?

    Either way you will need an interface to accept new values, this can be done with Picture1.Visible = True under a new command button cmdEdit. We will use the textboxes as data entry controls.

    But before that, next and back will become obsolete so add these in your form load event

    cmdNext.Visible = False
    cmdBack.Visible = False

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    how do i gewt it to update immediately.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    Quote Originally Posted by Jamie_Garland
    how do i gewt it to update immediately.
    I updated my post, pls answer

    "Also in what order do you want the update? File first before listview or the other way around?"

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    Listview then File

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    K. Before anything else we need to update mCurrentRecord
    VB Code:
    1. Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
    2.    mCurrentRecord = Val(Item.Tag)
    3. End Sub

    You will also need a button cmdEdit
    VB Code:
    1. Private Sub cmdEdit_Click()
    2.    If ListView1.SelectedItem Is Nothing Then Exit Sub
    3.    ListView1.Enabled = False  'so he can't change selected item
    4.    'you can slo use Picture2.Visible = False,
    5.    'but we'll show both picture boxes for now so you can compare
    6.  
    7.    With ListView1.SelectedItem  'initial values
    8.       txtID.Text = .Text
    9.       txtName.Text = .SubItems(1)
    10.       txtAddress.Text = .SubItems(2)
    11.       txtPostal.Text = .SubItems(3)
    12.       txtHome.Text = .SubItems(4)
    13.       txtMobile.Text = .SubItems(5)
    14.    End With
    15.  
    16.    Picture1.Visible = True
    17.    txtID.SetFocus
    18. End Sub

    You will need 2 more buttons in Picture1 (so they are shown when pic 1 is shown); cmdAccept and cmdCancel
    VB Code:
    1. Private Sub cmdCancel_Click()
    2.    Picture1.Visible = False
    3.    ListView1.Enabled = True
    4. End Sub
    5.  
    6. Private Sub cmdAccept_Click()
    7.    SaveCurrentRecord
    8.    ListView1.Enabled = True
    9. End Sub

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    To implement Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem) properly, in form load change this line

    mCurrentRecord = 1 'reset current to start

    To this

    ListView1.ListItems(1).Selected = True

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    My bad... this is shorter and uses existing procedure
    VB Code:
    1. Private Sub cmdEdit_Click()
    2.    If ListView1.SelectedItem Is Nothing Then Exit Sub
    3.    ListView1.Enabled = False  
    4.    ShowCurrentRecord
    5.    Picture1.Visible = True
    6.    txtID.SetFocus
    7. End Sub

    And I didn;t notice the update to frmAddressBook.Caption... transfer it to ListView's ItemClick event

    VB Code:
    1. Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
    2.    mCurrentRecord = Val(Item.Tag)
    3.    frmAddressBook.Caption = "Douglas Lads Club" + Str(mCurrentRecord) + "/" + Str(mLastRecord)
    4. End Sub

    When your done we will update implementation for delete
    Last edited by leinad31; Oct 7th, 2006 at 10:49 AM.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    This code seems to mess everything up

    Cant i just use

    VB Code:
    1. ListView1.Refresh

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    Have you got msn so i can send you my code and you can see what ive done couse im geting confused?

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    Phooey I'm getting sleepy, forgot to update listview. Change button name to AcceptEdit since we need to differentiate with AcceptNew
    VB Code:
    1. Private Sub cmdAcceptEdit_Click()
    2.    SaveCurrentRecord
    3. [b]   With ListView1.ListItems.SelectedItem
    4.       .Text = Trim(mPerson.MemberID)
    5.       .SubItems(1) = Trim(mPerson.MembersName)
    6.       .SubItems(2) = Trim(mPerson.MembersAddress)
    7.       .SubItems(3) = Trim(mPerson.MembersPostalCode)
    8.       .SubItems(4) = Trim(mPerson.MembersHomePhone)
    9.       .SubItems(5) = Trim(mPerson.MembersMobileNumber)
    10.    End With
    11.    cmdAcceptEdit.Visible = False[/b]
    12.    ListView1.Enabled = True
    13. End Sub
    14.  
    15. Private Sub cmdEdit_Click()
    16.    If ListView1.SelectedItem Is Nothing Then Exit Sub
    17.    ListView1.Enabled = False  
    18.    ShowCurrentRecord
    19.    Picture1.Visible = True
    20.    [b]cmdAcceptEdit.Visible = True[/b]
    21.    txtID.SetFocus
    22. End Sub

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    Quote Originally Posted by Jamie_Garland
    This code seems to mess everything up

    Cant i just use

    VB Code:
    1. ListView1.Refresh
    That will just repaint the listview... you have to assign new values that will be reflected in the refresh.

    Sure attach your project in a zip

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    ill pm you the download link in a sec

  16. #16
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    Nope don;t have msn, you can attach the zip in your post.

    EDIT: k I'll wait for the link

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    Link has been sent

  18. #18
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    K made some changes to frmAddressBook... though the app doesn't have handling routines for zero records yet. I also included code for resorting the list based on clicked columns.
    Attached Files Attached Files

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: ListView

    isnt there anyway i can keep both them on the forms instead of one needing to be visible.

  20. #20
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: ListView

    You will have problems with convergence (listview data matches textbox data) if the user edits data in textboxes when both are shown. To prevent that... In the last project, you could initially set the textboxes locked property to true, and unlock them only on cmdEdit.... When creating new records, set ListView1.Selected = nothing and refresh then go to textboxes (unlock same as in edit).... in the listview's itemclick event, call ShowCurrentRecord after updating mCurrentRecord to update textboxes, you navigate with listview so keep next and back buttons hidden.

    Have to go to bed now... so sleepy i'm programming slow. Changes took more time than I expected.

    Ciao!
    Last edited by leinad31; Oct 7th, 2006 at 01:06 PM.

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