Results 1 to 15 of 15

Thread: i'm stuck....

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    10

    i'm stuck....

    i'm sure this is super simple but i can't figure it out.all i want to do is use common dialog control to open a text file and load it into a listbox.i have this code that works for a textbox but i can't make it work for a listbox...



    Private Sub Command1_Click()

    CommonDialog1.Filter = "Text Files (*.txt)|*.txt| " & _
    "All Files (*.*)|*.*| "

    CommonDialog1.FilterIndex = 9

    CommonDialog1.ShowOpen
    strFileName = CommonDialog1.FileName

    Open strFileName For Input As #1
    Text1.Text = Input(LOF(1), 1)
    Close #1
    Exit Sub

    End Sub

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: i'm stuck....

    Welcome to the forums

    Listbox requires each item to be individually added, i.e., List1.AddItem [string value]

    So, suggest either reading the text file line by line or read it all into one large string and use Split() to separate the string on a common delimiter, i.e., carriage return most likely.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    10

    Re: i'm stuck....

    Quote Originally Posted by LaVolpe View Post
    Welcome to the forums

    Listbox requires each item to be individually added, i.e., List1.AddItem [string value]

    So, suggest either reading the text file line by line or read it all into one large string and use Split() to separate the string on a common delimiter, i.e., carriage return most likely.



    only been using vb for three days so i don't understand your reply.

    but i use this:


    Private Sub Command8_Click()
    On Error GoTo ErrHand
    With CD1
    .DialogTitle = "Load List"
    .Filter = "Text Files|*.txt"
    .CancelError = True
    .ShowOpen
    cLB.LoadListFromFile .FileName
    End With
    ErrHand:
    End Sub


    wit this class module:


    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWndLock As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal sParam As String) As Long

    Private Const LB_FINDSTRINGEXACT = &H1A2
    Private Const LB_DELETESTRING = &H182
    Private Const LB_GETCURSEL = &H188
    Private Const LB_INSERTSTRING = &H181
    Private Const LB_GETCOUNT = &H18B
    Private Const LB_GETTEXT = &H189
    Private Const LB_SETCURSEL = &H186
    Private Const LB_SETHORIZONTALEXTENT = &H194
    Private Const LB_ITEMFROMPOINT = &H1A9

    Private LB As ListBox
    Private LBBound As Boolean
    Private m_Scroll As Boolean

    Public Function BindList(LBox As ListBox) As Boolean
    Set LB = LBox
    If LB.hWnd > 0 Then
    BindList = True: LBBound = True
    Else
    BindList = False: LBBound = False
    End If
    End Function

    Public Property Get Text() As String
    Dim sItemText As String * 255
    Dim FinalOutStr As String

    sItemText = ""
    Call SendMessageStr(LB.hWnd, LB_GETTEXT, Me.ListIndex, ByVal sItemText)
    FinalOutStr = RTrim(sItemText)
    If Len(FinalOutStr) > 0 Then FinalOutStr = Left(FinalOutStr, Len(FinalOutStr) - 1)
    Text = FinalOutStr
    End Property

    Public Property Get List(Index As Long) As String
    Dim sItemText As String * 255
    Dim FinalOutStr As String

    sItemText = ""
    Call SendMessageStr(LB.hWnd, LB_GETTEXT, Index, ByVal sItemText)
    FinalOutStr = RTrim(sItemText)
    If Len(FinalOutStr) > 0 Then FinalOutStr = Left(FinalOutStr, Len(FinalOutStr) - 1)
    List = FinalOutStr
    End Property

    Public Property Get ListCount() As Long
    ListCount = SendMessageLong(LB.hWnd, LB_GETCOUNT, 0&, 0&)
    End Property

    Public Property Get ListIndex() As Long
    ListIndex = SendMessage(LB.hWnd, LB_GETCURSEL, 0, 0)
    End Property

    Public Property Let ListIndex(Index As Long)
    Call SendMessage(LB.hWnd, LB_SETCURSEL, Index, 0)
    End Property

    Public Sub AddItem(Item As String, Optional Index As Long = -1, Optional CheckForDuplicateEntry As Boolean = False)
    Dim DupEntry As Long
    If Index = -1 Then Index = Me.ListCount
    If CheckForDuplicateEntry Then
    DupEntry = SendMessageByString&(LB.hWnd, LB_FINDSTRINGEXACT, 0, Item)
    If DupEntry = -1 Then Exit Sub
    End If
    Call SendMessage(LB.hWnd, LB_INSERTSTRING, Index, ByVal Item)
    End Sub



    Public Function LoadListFromFile(FilePath As String, Optional AllowDups As Boolean = True)
    Dim sLineIn As String
    Dim DupEntry As Long

    On Error GoTo ErrLoadListFromFile

    Open FilePath For Input As #1
    While Not EOF(1)
    If AllowDups = True Then
    Line Input #1, sLineIn
    If Trim$(sLineIn) <> "" Then Me.AddItem sLineIn
    Else
    Line Input #1, sLineIn
    DupEntry = SendMessageByString&(LB.hWnd, LB_FINDSTRINGEXACT, 0, Trim$(sLineIn))
    If Trim$(sLineIn) <> "" And DupEntry = -1 Then Me.AddItem sLineIn
    End If
    Wend
    Close #1
    FilePath = ""
    AfterLoadListFromFile:
    Exit Function

    ErrLoadListFromFile:
    Resume AfterLoadListFromFile
    End Function

    Public Sub SaveListToFile(OutputFile As String, Optional AppendFile As Boolean = False)
    On Error GoTo Error_Killer
    Dim Nbr As Long
    Dim sItemText As String * 255
    Dim FinalOutStr As String

    If AppendFile Then
    Open OutputFile For Append As #1
    Else
    Open OutputFile For Output As #1
    End If

    For Nbr = 0 To Me.ListCount - 1
    sItemText = ""
    Call SendMessageStr(LB.hWnd, LB_GETTEXT, Nbr, ByVal sItemText)
    FinalOutStr = RTrim(sItemText)
    FinalOutStr = Left(FinalOutStr, Len(FinalOutStr) - 1)
    Print #1, FinalOutStr
    Debug.Print Len(FinalOutStr)
    Next Nbr

    Close #1

    Exit Sub
    Error_Killer:
    Close #1
    Exit Sub
    End Sub



    Public Sub RemoveDuplicateEntries(Optional FastLag As Boolean = False)
    On Error Resume Next
    Dim CurCount As Long
    Dim CurPos As Long
    Dim DupEntry As Long
    Dim sItemText As String * 255
    Dim FinalOutStr As String

    CurCount = Me.ListCount
    If CurCount = 0 Then Exit Sub
    CurPos = 0
    While CurPos < CurCount
    sItemText = "": FinalOutStr = ""
    Call SendMessageStr(LB.hWnd, LB_GETTEXT, CurPos, ByVal sItemText)
    FinalOutStr = RTrim(sItemText)
    If Len(FinalOutStr) > 0 Then FinalOutStr = Left(FinalOutStr, Len(FinalOutStr) - 1)
    If FinalOutStr <> "" Then
    DupEntry = SendMessageByString&(LB.hWnd, LB_FINDSTRINGEXACT, 0, FinalOutStr)
    If DupEntry <> CurPos Then
    Call SendMessage(LB.hWnd, LB_DELETESTRING, CurPos, 0)
    CurCount = CurCount - 1
    Else
    CurPos = CurPos + 1
    End If
    Else
    CurPos = CurPos + 1
    End If
    If FastLag = False Then DoEvents
    Wend
    End Sub




    Public Function Clear()
    LB.Clear
    End Function


    and it's fine.just seems there has to be a easier way.and when i add other code,like a sort function i get a ton of errors on code that works fine by itself.would be grateful if someone could help me.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: i'm stuck....

    Try replacing this
    Code:
    Open strFileName For Input As #1
    Text1.Text = Input(LOF(1), 1)
    Close #1
    with this
    Code:
    Dim LineFromFile As String
    Open strFileName For Input As #1
    Do While Not EOF(1)
        Line Input #1, LineFromFile
        list1.AddItem LineFromFile
    Loop
    Close #1
    Chnage list1 to the name of the actual listbox you are using if it is not list1

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    10

    Re: i'm stuck....

    problem solved.i got it working using this:


    Private Sub open_Click()
    Dim sfile As String
    With Dialog
    .CancelError = False
    .Filter = "Text Files (*.txt)|*.txt| " & _
    "All Files (*.*)|*.*| "
    .ShowOpen
    If .FileName = "" Then
    Exit Sub
    End If
    sfile = Dialog.FileName
    Dialog.FileName = ""
    End With
    List1.clear
    Dim a As String
    Dim X As String
    On Error GoTo error
    Open sfile For Input As #1
    Do Until EOF(1)
    Input #1, a$
    List1.AddItem a$
    Label1.Caption = List1.ListCount
    Loop
    Close 1
    sfile = ""
    Exit Sub
    error:
    X = MsgBox("File Not Found", vbOKOnly, "Error")
    End Sub

  6. #6
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Re: i'm stuck....

    That's what LaVolpe suggested, the one you said you didn't understand.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    10

    Re: i'm stuck....

    i didn't understand.i had to Frankenstein bits of code together and got lucky lol.thank you guys for the help tho.
    Last edited by JayAgro; Jun 30th, 2017 at 11:21 AM.

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: i'm stuck....

    Indent your code

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: i'm stuck....

    only been using vb for three days ......
    A bit of advice. Don't.

    Don't, that is, use VB6 (as this appears to be the application you are just starting to learn.
    I started learning it in the 90's. I didn't switch over to start learning VB.Net until really, a couple of months ago. Although .Net MAY be a bit harder to comprehend in its object-oriented-world, you will save yourself a lot if you start learning IT now, (no, Not I.T.) instead of VB6. .Net is much more powerful and you can do SO much more with it (as I am learning). AND, there are plenty of FREE downloadable versions of Visual Studio (in which VB.Net is contained---I am using Visual Studio 2017).

    Second piece of advice (of course you can ignore both of these): READ books and practice with simple projects first, possibly following one of the many on-line tutorials to help one learn these programs. OR, go to classes. But, most important, read books about the language you choose.

    This forum is super-great, but it helps tremendously if you have basic understandings of a language before posting questions. And never, never, never, "Frankenstein" (as you put it) other's code examples---you'll never learn the CONCEPTS, and without those, you'll never be a successful programmer.

    Don't want to sound negative, and I also want to welcome you to this forum, but please consider those two bits of advice.

    Sammi

  10. #10
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: i'm stuck....

    Jay

    I too welcome you to the Forums

    To "translate" jcis's post #8, in the future, when you post code frags, you should
    use the "code wrapper". It is obtained by clicking the # icon on the far right of the
    menu bar. Paste your code between the wrappers. You then get the following ,,,

    Code:
    Private Sub open_Click()
        Dim sfile As String
        With Dialog
            .CancelError = False
            .Filter = "Text Files (*.txt)|*.txt| " & _
            "All Files (*.*)|*.*| "
            .ShowOpen
            If .FileName = "" Then
                Exit Sub
            End If
            sfile = Dialog.FileName
            Dialog.FileName = ""
        End With
        List1.clear
        Dim a As String
        Dim X As String
        On Error GoTo error
        Open sfile For Input As #1
        Do Until EOF(1)
            Input #1, a$
            List1.AddItem a$
            Label1.Caption = List1.ListCount
        Loop
        Close 1
        sfile = ""
        Exit Sub
    error:
        X = MsgBox("File Not Found", vbOKOnly, "Error")
    End Sub
    EDIT

    .. assuming, of course, that your code was indented ..

    Spoo
    Last edited by Spooman; Jun 30th, 2017 at 03:45 PM.

  11. #11
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: i'm stuck....

    Quote Originally Posted by SamOscarBrown View Post
    A bit of advice. Don't.
    Don't, that is, use VB6 (as this appears to be the application you are just starting to learn.
    I started learning it in the 90's. I didn't switch over to start learning VB.Net until really, a couple of months ago. Although .Net MAY be a bit harder to comprehend in its object-oriented-world, you will save yourself a lot if you start learning IT now, (no, Not I.T.) instead of VB6. .Net is much more powerful and you can do SO much more with it (as I am learning). AND, there are plenty of FREE downloadable versions of Visual Studio (in which VB.Net is contained---I am using Visual Studio 2017).
    Sammi
    Sam,
    I too learned VB6 (VB3 16bit onwards) in the early 90's.
    I swore that I would never learn VB.NET (not being a fan of OO).
    Your post has slightly weakened my resolve.
    Are you using the free version ?
    Are there any limitations ?
    Perhaps a forum or a thread for those tempted to learn (switch from VB6) would be handy.

    Regards,
    Rob

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: i'm stuck....

    Quote Originally Posted by Bobbles View Post
    Sam,
    I too learned VB6 (VB3 16bit onwards) in the early 90's.
    I swore that I would never learn VB.NET (not being a fan of OO).
    Your post has slightly weakened my resolve.
    Well, I'd ask SAM when he writes:
    "... .Net is much more powerful and you can do SO much more with it... "

    What concretely he means, because I've never (in the last 15 years we had this "debate")
    got shown a concrete Desktop-App-example from any .NETer which proved that claim.

    But perhaps SAM will surprise me, being the first who shows a Demo that does a few of these magical,
    "so much more powerful things" ...

    Also keep in mind, that there's no real big Desktop-Apps out there from MS, which
    are written in .NET (their Office-suite, the MS-Browser, the Windows-Explorer-Shell etc.
    are all "Native compiled Apps, based on COM", not on .NET).

    The only Desktop-Apps I know of, which are written by MS with the help of .NET are
    - the SQLServer-Manager (which became a slow resource-hog after they switched it from the COM-version).
    - the Visual-Studio-IDE itself (although since Version 2012 which had nearly unbearable startup-times due to .NET, they replaced more and more parts of it with native-binaries now)

    They recognized their mistake - and are now paddling back with ".NET-native" (Roslyn-compiler) and ".NET-standard" (a reduced platform-portable framework with less functionality) -
    but that's the "future of .NET" - and anybody who writes Apps now, based on .NET-Winforms, is writing "old, basically deprecated stuff",
    because "UWP" is the new GUI-stuff - also ASP.NET is basically deprecated (vNext being the new toy) ...

    Of yourse you could try to restrict yourself, targetting the new .NET-native and .NET-standard (version 2.0 released recently) -
    but try to write a decent little .NET-App with those new "future-proof" .NET-targets (not using WinForms, but the new "Universal GUI-Framework" UWP) -
    and you whish yourself back to VB6.

    Olaf

  13. #13

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    10

    Re: i'm stuck....

    thanks for the advice guys i used vb6 back in the day and just recently felt nostalgic for it cause i had so much fun using it.problem is i forgot almost everything lol.

  14. #14
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: i'm stuck....

    Sammi

    Make that 3 (well. 4, if we include Jay ,, )
    I too would like to know what more one can do with .NET

    Spoo

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: i'm stuck....

    I'll start a new thread....

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