Results 1 to 11 of 11

Thread: Saving List count and retrieving?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    377

    Saving List count and retrieving?

    Hi i have the following code...

    For X = 0 To lstretri.ListCount - 1
    lstretri.ListIndex = X
    txtCluburl.Text = lstretri.List(X)
    lblcurrent.Caption = "Current: " & lstretri.ListIndex

    I want to put a piece of coding the the Form_Unload() to save the current Listbox Index to "hold.yem" if the program is accidently closed. Also I would like for the For statement to retrieve the number value of the Listbox Index that was save and plug it into the coding and continue from where it left off.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Try something like this
    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2. SaveSetting "MyApp", "FormName", "ListBoxName", List1.ListIndex
    3. End Sub
    4.  
    5. Private Sub Form_Load()
    6. Dim LIndex As String
    7. LIndex = GetSetting("MyApp", "FormName", "ListBoxName")
    8.      If LIndex <> "" Then
    9.         List1.ListIndex = CInt(LIndex)
    10.      End If
    11. End Sub

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    he might wanna know where the info is saved ??
    -= a peet post =-

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    377
    can someone show me how to do the saving and loading using the text file "hold.yem"

  5. #5
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    here is an example if you want to use an INI file

    VB Code:
    1. 'module code
    2.  
    3. Option Explicit
    4.  
    5.     Public Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    6.         "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
    7.         lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    8.    
    9.     Public Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    10.         "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
    11.         lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As _
    12.         String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    13.  
    14.  
    15.     Public Sub WriteToINI(Section As String, Key As String, KeyValue As String, _
    16.         Directory As String)
    17.    
    18.     Call WritePrivateProfileString(Section$, UCase$(Key$), KeyValue$, Directory$)
    19.    
    20.     End Sub
    21.    
    22.     Public Function GetFromINI(Section As String, Key As String, Directory As String) As String
    23.        Dim strBuffer As String
    24.        
    25.        strBuffer = String(750, Chr(0))
    26.        
    27.        Key$ = LCase$(Key$)
    28.        
    29.        GetFromINI$ = Left(strBuffer, GetPrivateProfileString(Section$, ByVal _
    30.             Key$, "", strBuffer, Len(strBuffer), Directory$))
    31.    
    32.     End Function

    use it like this

    VB Code:
    1. 'form code
    2. Option Explicit
    3. Dim intListBoxCount As Integer
    4.  
    5. Private Sub Form_Load()
    6.     If Not Dir$((App.Path & "\hold.yem")) = "" Then intListBoxCount = Int(GetFromINI("Saved Values", "ListBox_Count", (App.Path & "\hold.yem")))
    7.     MsgBox Str(intListBoxCount)
    8. End Sub
    9.  
    10. Private Sub Form_Unload(Cancel As Integer)
    11.     Call WriteToINI("Saved Values", "ListBox_Count", Str(lstretri.ListCount), (App.Path & "\hold.yem"))
    12. End Sub


    Let me know if this is what you were looking for.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    377
    Code:
    Option Explicit
    Dim intListBoxCount As Integer
    
    Private Sub Form_Load()
        If Not Dir$((App.Path & "\hold.yem")) = "" Then intListBoxCount = Int(GetFromINI("Saved Values", "ListBox_Count", (App.Path & "\hold.yem")))
        MsgBox Str(intListBoxCount)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Call WriteToINI("Saved Values", "ListBox_Count", Str(lstretri.ListCount), (App.Path & "\hold.yem"))
    End Sub

    Orginal loop from beginning
    For X = 0 To lstretri.ListCount - 1
    lstretri.ListIndex = X
    txtCluburl.Text = lstretri.List(X)
    lblcurrent.Caption = "Current: " & lstretri.ListIndex


    how would i make the above For Loop statement start with the item number that "intListBoxCount" holds

  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    VB Code:
    1. assuming you've loaded it from the ini file
    2. Orginal loop from beginning
    3. For X = [u]intListCount[/u] To lstretri.ListCount - 1
    4. lstretri.ListIndex = X
    5. txtCluburl.Text = lstretri.List(X)
    6. lblcurrent.Caption = "Current: " & lstretri.ListIndex
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    Sorry I didn't read you first post correctly


    VB Code:
    1. Private Sub Form_Load()
    2.     If Not Dir$((App.Path & "\hold.yem")) = "" Then intListBoxCount = Int(GetFromINI("Saved Values", "ListBox_Count", (App.Path & "\hold.yem")))
    3.     For X = intListBoxCount To lstretri.ListCount - 1
    4.    
    5.     Next X
    6. End Sub
    7.  
    8. Private Sub Form_Unload(Cancel As Integer)
    9.     Call WriteToINI("Saved Values", "ListBox_Count", Str(lstretri.ListIndex), (App.Path & "\hold.yem"))
    10. End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    377
    Code:
      If Not Dir$((App.Path & "\" + strName)) = "" Then
      intListBoxCount = Int(GetFromINI("Saved Values", "ListBox_Count", (App.Path & "\" + strName)))
      For x = intListBoxCount To lstretri.ListCount - 1
      Else <-- ******************ERROR HERE!
      For x = 0 To lstretri.ListCount - 1
      End If
      
      lstretri.ListIndex = x
      txtCluburl.Text = lstretri.List(x)
      lblcurrent.Caption = "Current: " & lstretri.ListIndex
    why do i keep getting a Else without IF error.

  10. #10
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    VB Code:
    1. If Not Dir$((App.Path & "\" + strName)) = "" Then
    2.     intListBoxCount = Int(GetFromINI("Saved Values", "ListBox_Count", (App.Path & "\" + strName)))
    3.     For x = intListBoxCount To lstretri.ListCount - 1
    4.        ' your code here
    5.     Next x 'you next to add next x
    6.   Else
    7.     For x = 0 To lstretri.ListCount - 1
    8.        ' your code here
    9.     Next x 'you next to add next x
    10.   End If
    11.  
    12.   lstretri.ListIndex = x
    13.   txtCluburl.Text = lstretri.List(x)
    14.   lblcurrent.Caption = "Current: " & lstretri.ListIndex

  11. #11
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    better yet write it like this

    VB Code:
    1. If Not Dir$((App.Path & "\" + strName)) = "" Then
    2.         intListBoxCount = Int(GetFromINI("Saved Values", "ListBox_Count", (App.Path & "\" + strName)))
    3.     Else
    4.         intListBoxCount = 0
    5.     End If
    6.      
    7.      
    8.     For x = intListBoxCount To lstretri.ListCount - 1
    9.         lstretri.ListIndex = x
    10.         txtCluburl.Text = lstretri.List(x)
    11.         lblcurrent.Caption = "Current: " & lstretri.ListIndex
    12.     Next x 'you next to add next x

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