0

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 text file, ex 1.txt when am clicking the command buttion, 1.txt file will open and 1.txt data's should display

TAbdiukov
  • 1,185
  • 3
  • 12
  • 25
Gopal
  • 11,712
  • 52
  • 154
  • 229
  • I want to display a contents of the file. – Gopal Jul 29 '09 at 10:04
  • What do you mean by "display"? Do you want to show the file name? Or the contents of the file? "Display" to allow the user to edit the file? "Display" in a message box? Help us to help you :) – Binary Worrier 57 secs ago – Binary Worrier Jul 29 '09 at 10:04
  • Yes, but "display" how? Do you want to open it in Notepad? Do you want to put it in a TextBox? There is not enough information in your question. – Binary Worrier Jul 29 '09 at 10:05
  • Displin in a control ? in a tootltip ? opening notepad ? Dump in a console ? Be more explicit. – Clement Herreman Jul 29 '09 at 10:05
  • Is already a Notepad File, just i want to show the content of the file. – Gopal Jul 29 '09 at 10:13
  • 2
    But how??? - do you want to show it by firing up notepad or are you trying to load the contents in to a text box etc? – Chris W Jul 29 '09 at 10:14

4 Answers4

6

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.)

Community
  • 1
  • 1
Stefan
  • 11,423
  • 8
  • 50
  • 75
  • Nice answer but your code to read a text file into a string is flawed (no offence). Details in my answer. http://stackoverflow.com/questions/1199143/how-to-display-the-text-file-while-clicking-the-button/1199860#1199860 – MarkJ Jul 29 '09 at 12:21
  • Good point, I changed the row in my example. I dont have vb6 installed anymore so I could'nt test it. – Stefan Jul 30 '09 at 11:58
5

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.

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
3

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 :)

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
1

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

Here's an example.

Chris W
  • 3,304
  • 3
  • 26
  • 28