Results 1 to 11 of 11

Thread: [RESOLVED] Open .txt in RichTextBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    6

    Resolved [RESOLVED] Open .txt in RichTextBox

    Hi All, new member here..

    Basically, I have .txt file with data as below:

    moref:800
    name:nox-A
    uuid:564d3ae6-5c80-79bb-daee-60b0215df8ee
    ipaddr:192.168.1.12
    moref:810
    name:nox-B
    uuid:564d29e7-d264-b779-870c-f402936b41dc
    ipaddr:192.168.1.14
    moref:832
    name:nox-C
    uuid:564d943d-0801-7f3d-0297-f428999e3619
    ipaddr:192.168.1.16

    I want to open this .txt in RichTextBox with some filtering via combobox. Let say, If I choose "name" from combobox, richtextbox only display :
    nox-A
    nox-B
    nox-C

    If ipaddr via combobox :
    192.168.1.12
    192.168.1.14
    192.168.1.16

    I know how to open via richtextbox but donno how to filter it. Hope someone can guide me on how to achieve this.

    Thanks

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

    Re: Open .txt in RichTextBox

    Welcome to the forums.

    I am assuming the file is not huge? If so, I think you may want to do the following:
    1. Read your file and store it in a UDT (user-defined type) structure array
    2. Add the IPs to the combobox and use the combo's .ItemData property to cross-reference its items to the UDT array.

    Tasks include creating a UDT array, opening file, reading it, populating the UDT from the file, appending IPs to the combobox and using its .ItemData member. Are you comfortable with these? If not, please indicate what you don't know how to do.

    There are other methods that can achieve what you want, but I think what I mentinoed is the easiest. If you file is huge, you may want to consider other methods.

    P.S. Your UDT might look like:
    Code:
    Private Type USERSTRUCT
        MoreF As Long
        Name As String
        UUID As String
        IPAddr As String
    End Type
    
    Dim theUsers() As USERSTRUCT
    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 2009
    Location
    Malaysia
    Posts
    6

    Re: Open .txt in RichTextBox

    To be frank, I'm more on bash scripting and my knowledge in VB is 3 out of 10. I'm only knew how to modify ready made source code. To create & use VB function, module & class it's little bit difficult for me to do it. Plus vb programming just to make my daily work more easier.

    So, the answer of your question, "No" I dont know how to do it but are willing to learn.

    Back to the topic, it's small flat file with 3KB in size.

    Thanks

  4. #4
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Open .txt in RichTextBox

    i would use 2 richtextboxes one hidden, load the hidden one from the text file. Display results in the second one: using selstart, find and sellength
    Heres an example:
    http://www.planet-source-code.com/vb...xtCodeId=69098
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    6

    Re: Open .txt in RichTextBox

    Okay, without knowing much, I give a try to :
    - Create UDT
    - Open & read the file


    vb Code:
    1. 'UDT aka user define type
    2. Private Type ESX
    3.     moref As Long
    4.     name As String
    5.     uuid As String
    6.     ipaddr As String
    7. End Type
    8.  
    9. Dim vms() As ESX
    10. Dim allvmsESX(50) As ESX
    11.  
    12. 'Open & Read File
    13. Private Sub Form_Load()
    14. Dim strFile As String
    15. Dim nFile As Integer, strOut As String
    16.  
    17. strFile = "C:\example.txt"
    18. nFile = FreeFile
    19. Open strFile For Input As #nFile
    20.     Do Until EOF(nFile)
    21.         Input #nFile, strOut
    22.     Loop
    23. Close #nFile
    24.  
    25. End Sub

    So, now how to populate & what next? any idea?

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

    Re: Open .txt in RichTextBox

    Code:
    'UDT aka user define type
    Private Type ESX
        moref As Long
        name As String
        uuid As String
        ipaddr As String
    End Type
    
    Dim vms() As ESX
    Dim allvmsESX(50) As ESX
    
    'Open & Read File
    Private Sub Form_Load()
    Dim strFile As String
    Dim nFile As Integer, strOut As String
    Dim iPos as Integer, Index As Integer
    
    strFile = "C:\example.txt"
    ' add error checking in case file doesn't exist 
    nFile = FreeFile
    Open strFile For Input As #nFile
        Do Until EOF(nFile)
            Line Input #nFile, strOut
            If Len(strOut) Then 
                ' Assumption: File format is exactly like the following (no spaces in line)
                ' moref:800
                ' name:nox-A
                ' uuid:564d3ae6-5c80-79bb-daee-60b0215df8ee
                ' ipaddr:192.168.1.12
                iPos = InStr(strOut, ":")
                If iPos Then 
                    Select Case LCase$(Left$(strOut, iPos-1))
                    Case "moref"
                        If Index = 0 Then 
                             ReDim vms(0)
                        Else 
                             ReDim Preserve vms(Index)
                        End If
                        ' should add error checking here too. What if value is non-numeric?
                        vms(Index).moref = CLng(mid$(strOut,I+1))
                        Index = Index + 1
                    Case "name"
                        vms(Index-1).name = mid$(strOut,I+1)
                    Case "uuid"
                        vms(Index-1).uuid = mid$(strOut,I+1)
                    Case "ipaddr"
                        vms(Index-1).ipaddr = mid$(strOut,I+1)
                    Case else
                          ' error text not formatted as expected
                    End Select
                Else
                    ' error text is not formatted as expected
                End If
            End If
        Loop
    Close #nFile
    
    If Index > 0 Then
         cboIPs.Clear ' combo for IP address
         For Index = 0 To Index - 1
              ' add IPs to combo and use ItemData prop to cross-ref
              cboIPs.Add vms(Index).ipaddr
              cboIPs.ItemData(cboIPs.NewIndex) = Index
         Next
    End If
    End Sub
    
    ' now when a combo box item is selected
    vms(cboIPs.ItemData(cboIPs.ListIndex)) references the selected IP
    ' of course, this idea is based on the fact that no two vms() items will have same IP
    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}

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    6

    Re: Open .txt in RichTextBox

    Thanks for your effort dude! BTW,

    Run time error-13 on this line :
    vms(Index).moref = CLng(Mid$(strOut, i + 1))

    when I move along mouse cursor to this line, it's return :
    vms(Index).moref = 0

    when I changed moref as string it's return =""

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

    Re: Open .txt in RichTextBox

    Oh, duh on me! Change i + 1 to iPos + 1 throughout. That's the problem with "air code", not tested. Sorry.
    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}

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    6

    Re: Open .txt in RichTextBox

    nvm, you did enough! My main target actually call combo by "name" instead "ipaddr". After modified it, things I thought impossible become possible now.

    Thanks once again for your effort. BTW, how could I rank you?

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

    Re: Open .txt in RichTextBox

    Quote Originally Posted by athlon_crazy View Post
    nvm, you did enough! My main target actually call combo by "name" instead "ipaddr". After modified it, things I thought impossible become possible now.

    Thanks once again for your effort. BTW, how could I rank you?
    Thanks is enough. You're welcome. If you want to "rank" me, click on the scale icon located at the far left of this post.
    Last edited by LaVolpe; Jun 20th, 2009 at 03:28 PM.
    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}

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    6

    Re: [RESOLVED] Open .txt in RichTextBox

    Done & Resolved!

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