How to display the text file while clicking the button

前端 未结 4 1565
日久生厌
日久生厌 2020-12-22 12:50

How to display the file(*.txt) while clicking the command button

How to display the content of the file while clicking the button

Data\'s are stored in the t

相关标签:
4条回答
  • 2020-12-22 13:01

    Add a textbox to a form, make it multiline=true, add a button to the form. And in the buttons click handler add this:

    Private Sub Button1_Click()
      Dim iFile As Long
      Dim strFilename As String
      Dim strTheData as String
    
      strFilename = "C:\1.txt"
    
      iFile = FreeFile
    
      Open strFilename For Input As #iFile
       strTheData = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
      Close #iFile
      text1.text=strThedata
    End Sub
    

    This will read the text in the file and add it to the textbox.

    Edit: Changed the line that reading the content to be more robust as pointed out by MarkJ in this answer (Cred goes to to MarkJ to pointing out that.)

    0 讨论(0)
  • 2020-12-22 13:04

    No offence intended, but it sounds like you need a beginners tutorial on VB6. (I think this because you don't seem to be able to articulate exactly what you need help with, possibly because you don't know enough about what you're trying to do).

    Googling for VB6 Tutorial will give lots of links, this one looks good

    Hope this helps, and apologies if I'm wrong :)

    0 讨论(0)
  • 2020-12-22 13:05

    Stefan's answer contains a flaw: the code to read a text file into a string isn't very robust. It's a very common mistake - the same flawed code is on some excellent VB6 web sites. His code is

    Open strFilename For Input As #iFile
    strTheData = Input$(LOF(iFile), #iFile)
    Close #iFile  
    

    Unfortunately this throws an error 62 "input past end of file" if the text file contains ASCII zero characters. Also it doesn't work in all countries (it throws an error for most strings in double byte character sets like Chinese or Japanese).

    Perhaps those problems are a bit obscure: but there's better code to do this job in the VB6 manual (here), it's also three lines, and it never fails.

    Open strFilename For Input As #iFile
    strTheData = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
    Close #iFile  
    

    It looks more complicated: but actually the only difference is that the conversion from ANSI to Unicode is explicit rather than implicit. It runs just as fast, and it always works.

    0 讨论(0)
  • 2020-12-22 13:17

    To just open a file using the current default file handler try using the ShellExecute API function.

    Here's an example.

    0 讨论(0)
提交回复
热议问题