CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2010
    Posts
    4

    Edit text in FileListBox

    Hi all,

    I have a FileListBox that shows the *.txt files and a user has in a directory. When a user highlights the file they want and clicks a Command button, user can edit that file and save it as newfile.txt

    Here are the list of a text file :

    :25:186350587
    :28:153
    :60F:C100602IDR879511164,
    :61:1006020602DR313600,NTRF//
    :20:201006170828154
    :25:186350587
    :28:154
    :60F:C100603IDR462231690,
    :61:1006030603CR918000000,NTRF//
    :20:201006170828155
    :25:186350587
    :28:155
    :60F:C100604IDR703646190,
    :61:1006040604DR440000000,NTRF//
    :86:TARIK TRF (RTGS) | N WILLY MANOBY | HLP/121339
    :20:201006170828158
    :25:186350587
    :28:158
    :60F:C100607IDR169830638,
    :61:1006070607DR3920000,NTRF//
    :20:201006170828159
    :25:186350587
    :28:159
    :60F:C100608IDR34779678,
    :61:1006080608CR925800000,NTRF//
    :20:201006170828160
    :25:186350587
    :28:160
    :60F:C100609IDR235564678,
    :61:1006090609DR92826000,NTRF//
    :20:201006170828161
    :25:186350587
    :28:161
    :60F:C100610IDR637017878,
    :61:1006100610DR3123288,NTRF//
    :20:201006170828162
    :25:186350587
    :28:162
    :60F:C100611IDR1378990932,
    :61:1006110611DR42325828,NTRF//
    :20:201006170828166
    :25:186350587
    :28:166
    :60F:C100615IDR798699355,
    :61:1006150615DR500000000,NTRF//

    User want to insert all line that contain :25: with two zero, so it would be like :25:00
    Can anyone help me with the code in Visual Basic?

    Thanks
    Andi

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Edit text in FileListBox

    Do you want every line to be formatted with 00: or just the lines with :25?

    What code do you have to read the file? That's probably the place to start
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Edit text in FileListBox

    Yep

    Look at the line input and mid() functions
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jun 2010
    Posts
    4

    Re: Edit text in FileListBox

    Not every line, just the lines with :25:

    this is the code :

    Code:
    Dim pathasal As String
    Dim pathhasil As String
    Private Sub cmdProses_Click()
    
    Dim fileNo As Long
    Dim lineNo As Long
    Dim lineText As String
    Dim k  As String
       
        Open pathasal For Input As #1
        Open pathhasil For Output As #2
        
    Do Until EOF(1)
    
        Line Input #1, lineText
        
        p = InStr(lineText, ":25:")
        If p > 0 Then
            k = Left(lineText, p + 3) + "00" + Mid(lineText, p + 4)
            inputan = CStr(k)
        Else
            inputan = CStr(lineText)
        End If
           
        Write #2, CStr(inputan)
        lineNo = lineNo + 1
     
    Loop
    Close #2
    End Sub
    Private Sub cmdClose_Click()
    Unload Me
    End Sub
    Private Sub Drive1_Change()
     Dir1.Path = Drive1.Drive
    End Sub
    Private Sub Dir1_Change()
       File1.Path = Dir1.Path
    End Sub
    Private Sub File1_Click()
    pathasal = File1.Path + "\" + File1.FileName
    a = pathasal
    pathhasil = File1.Path + "\hasil.txt"
    End Sub
    Private Sub File1_DblClick()
    Dim r As Long, msg As String
              Dim str As String
              If Right(Dir1.Path, 1) = "" Then
                  str = Dir1.Path & File1.FileName
              Else
                  str = Dir1.Path & "" & File1.FileName
              End If
              Me.Caption = str
              r = OpenDocument(str)
              'If there is an error, the return value is
              'less than or equal to 32
              If r <= 32 Then
                  Select Case r
                      Case SE_ERR_FNF
                          msg = "File not found"
                      Case SE_ERR_PNF
                          msg = "Path not found"
                      Case SE_ERR_ACCESSDENIED
                          msg = "Access denied"
                      Case SE_ERR_OOM
                          msg = "Out of memory"
                      Case SE_ERR_DLLNOTFOUND
                          msg = "DLL not found"
                      Case SE_ERR_SHARE
                          msg = "A sharing violation occurred"
                      Case SE_ERR_ASSOCINCOMPLETE
                          msg = "Incomplete or invalid file association"
                      Case SE_ERR_DDETIMEOUT
                          msg = "DDE Time out"
                      Case SE_ERR_DDEFAIL
                          msg = "DDE transaction failed"
                      Case SE_ERR_DDEBUSY
                          msg = "DDE busy"
                      Case SE_ERR_NOASSOC
                          msg = "No association for file extension"
                      Case ERROR_BAD_FORMAT
                          msg = "Invalid EXE file or error in EXE image"
                      Case Else
                          msg = "Unknown error"
                  End Select
                  MsgBox msg
              End If
    End Sub
    It works but it's only replace the first :25: but the next :25: does not change to :25:00

    Thanks.
    Andi
    Last edited by HanneSThEGreaT; June 29th, 2010 at 05:19 AM. Reason: Addede [CODE] Tags!

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Edit text in FileListBox

    Use code tags when posting code please..
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Edit text in FileListBox

    I have copied your code of cmdProces_click() and tested it on a file I made from your example.
    It works perfectly, replacing every :25: to a :25:00, no problem.

    Only two things seem to be troublesome:
    When closing, you better Close the input file too. Use
    Close
    to close all files or
    Close #1, #2
    to close them explicitly.

    The variables p and inputan are not declared within the sub. Do you have Option Explicit active?
    The first statement of your form's code should be
    Option Explicit
    so as all undefined variable problems are revealed.

    But this should not be the problem. Your code loop seems to work as it should.

  7. #7
    Join Date
    Jun 2010
    Posts
    4

    Re: Edit text in FileListBox

    Hi All,

    I've the solution with textbox and command, this is the code :

    Private Sub cmdProses_Click()
    Dim txt
    txt = TextBox.Text
    If InStr(1, TextBox.Text, ":25:") > 0 Then
    TextBox.Text = Replace(txt, ":25:", ":25:00")
    End If
    End Sub

    but I have a new problem, the text after ":25:" has to be 10 digit long.
    For example:

    :25:0186350587
    :25:186350587
    :25:86350587
    :25:6350587
    :25:350587
    :25:50587
    :25:587
    :25:87
    :25:7

    So it has to be :

    :25:0186350587
    :25:0186350587
    :25:0086350587
    :25:0006350587
    :25:0000350587
    :25:0000050587
    :25:0000000587
    :25:0000000087
    :25:0000000007

    but with my code it only insert two digit of zero number, so it would be :

    :25:000186350587 (more than 10 digit)
    :25:00186350587 (more than 10 digit)
    :25:0086350587 (10 digit)
    :25:006350587 (less than 10 digit)
    :25:00350587 (less than 10 digit)
    :25:0050587 (less than 10 digit)
    :25:00587 (less than 10 digit)
    :25:0087 (less than 10 digit)
    :25:007 (less than 10 digit)



    Would you like to help me with the new code please

    Thank you
    Andi.

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Edit text in FileListBox

    Code:
    Private Sub Command1_Click()
    Dim P As Integer
    P = InStr(Text1.Text, ":25:")
    If P > 0 Then
        Label1.Caption = Mid(Text1.Text, 1, P + 3) & Right(String(10, "0") & Mid(Text1.Text, P + 4), 10)
    End If
    End Sub
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Jun 2010
    Posts
    4

    Re: Edit text in FileListBox

    The code does not work when I run it.
    FYI link below contain the complete of my program and sample text that I want to edit.

    http://docs.google.com/leaf?id=0B8HO...ut=list&num=50

    Thanks
    Andi

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Edit text in FileListBox

    "Does not work" Is not much of a description.
    The code I posted does work assuming that the textbox and the label exist on your form.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Apr 2009
    Posts
    394

    Re: Edit text in FileListBox

    As I said at another site... "Format" it out...
    Code:
    Dim My1stArray() As String, My2ndArray() As String, S As String
    Dim LB As Integer, UB As Integer, LoopCnt As Integer
    
    S = ":25:0186350587,:25:186350587,:25:86350587,:25:6350587,:25:350587,:25:50587,:25:587,:25:87,:25:7"
    
    My1stArray = Split(S, ",")
    
    LB = LBound(My1stArray)
    UB = UBound(My1stArray)
    
    For LoopCnt = LB To UB
      
      My2ndArray = Split(My1stArray(LoopCnt), ":25:")
      Debug.Print ":25:" & Format(My2ndArray(1), "0000000000")
      
    Next LoopCnt


    Good Luck

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

Featured