tags:

views:

270

answers:

4

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

Using VB 6

Am New to VB 6

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

Need VB 6 code Help?

+1  A: 

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

Here's an example.

Chris W
+2  A: 

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
+4  A: 

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

Stefan
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
Good point, I changed the row in my example. I dont have vb6 installed anymore so I could'nt test it.
Stefan
+2  A: 

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.

MarkJ