View previous topic :: View next topic |
Author |
Message |
loopers Just Arrived
Joined: 19 Nov 2002 Posts: 1
|
Posted: Mon Dec 09, 2002 6:48 pm Post subject: Very Simple Visual Basic Syntax Help Needed |
|
|
When we insert certain CD into our CD-ROM like Warcraft 3 or Norton Antivirus 2003, there will be an .exe file (the installer with very nice interface) that automatically pops up from the CD. We do not have to go to My Computer icon (at our desktop) and access our CD-ROM drive to find the setup program.
I would like to put something like this in my CD (I have no problems in writing the commands for autorun.inf). I know how to create an .exe file (the automatically pop up installer) in Visual Basic 6.0. However, I have a very very hard time finding the correct Visual Basic syntax for a particular button in Visual Basic. This is what I want the VB program to do:-
Once the user click on a button, this button will run a file call a.txt
Any help will be truly appreciated.
|
|
Back to top |
|
|
ShaolinTiger Forum Fanatic
Joined: 18 Apr 2002 Posts: 16777215 Location: Kuala Lumpur, Malaysia
|
Posted: Mon Dec 09, 2002 6:54 pm Post subject: |
|
|
Well there is 2 ways I can think to do it, I'm no VB programmer though.
I have done something similar to this before and it's not hard..
Either
a) Spawn an external program with the correct parameters e.g. "notepad" + "%txtfilename%.txt"
The code is:
Code: |
Shell "notepad.exe yourdoc.txt", vbNormalFocus |
b) Just read the text file in line by line and display it in a memo field so the user displays it in your app rather than a new program spawning.
To get from a file:
Code: |
Private Function GetFromFile() As Boolean
Try
List1.Items.Clear() 'Clear the list before we can load from the text file
Dim sr As StreamReader
sr = File.OpenText(Application.StartupPath & "\Test.txt")
Dim strItems As String
'loop through the text file
While sr.Peek <> -1
strItems = sr.ReadLine()
List1.Items.Add(strItems) 'add the contents to the list box
End While
sr.Close() 'close the file
Return True 'file was there...with no errors
Catch 'ignore errors
Return False 'most likely we don't have file permission (to delete)
End Try
End Function |
|
|
Back to top |
|
|
TheKingster Link Spammer
Joined: 03 May 2002 Posts: 0 Location: UK
|
Posted: Mon Dec 09, 2002 7:19 pm Post subject: |
|
|
Theres something weird with the shell command in VB6 and you have to use an API call.
However follow Shaolins method first, the first way is the correct and simple way and the second way is just making a hard job of an easy thing, unless of course you wish it to be displayed in a multiline text box that you have drawn on your form - which would look better.
If you have any problems then post back on here and PM me to say you have posted cause I dont get time to check all posts, but I will post any results on here so all members can see.
Its just something I experienced with the shell command a few months ago but I cant remember it at the moment, will look it back up if you need though.
|
|
Back to top |
|
|
Wombat Trusted SF Member
Joined: 24 Apr 2002 Posts: 0 Location: Canberra
|
|
Back to top |
|
|
loopers Just Arrived
Joined: 19 Nov 2002 Posts: 1
|
Posted: Wed Dec 11, 2002 7:08 pm Post subject: Further help needed. |
|
|
Thanks for your code, Shaolin Tiger. Actually, I am a beginner in VB programming. So I really don't know how to implement this code here:-
Code: |
Private Function GetFromFile() As Boolean
Try
List1.Items.Clear() 'Clear the list before we can load from the text file
Dim sr As StreamReader
sr = File.OpenText(Application.StartupPath & "\Test.txt")
Dim strItems As String
'loop through the text file
While sr.Peek <> -1
strItems = sr.ReadLine()
List1.Items.Add(strItems) 'add the contents to the list box
End While
sr.Close() 'close the file
Return True 'file was there...with no errors
Catch 'ignore errors
Return False 'most likely we don't have file permission (to delete)
End Try
End Function
|
I just need a very simple button. What I wanted to do is this:-
1st step
- Open a standard .EXE file
2nd step
- Draw a rectangular Command Button in the Form1
3rd step
- Double-click on the Command Button and we get to see this:-
Code: |
Private Sub Command1_Click()
End Sub
|
So now, I will try to implement the code that you have given me. Here is what I did ...
Code: |
Private Sub Command1_Click()
Dim sr As StreamReader
sr = File.OpenText(Application.StartupPath & "C:\Windows\Desktop\Test.txt")
End Sub
|
But I still can't open a text file called Test.txt
I know this must be a simple question but I am very new in VB programming
I am a C programmer myself with reasonable knowledge. So if there is some C programming discussion or question, I will be very happy to exchange knowledge too. Thanks
|
|
Back to top |
|
|
Wombat Trusted SF Member
Joined: 24 Apr 2002 Posts: 0 Location: Canberra
|
Posted: Fri Dec 13, 2002 12:55 am Post subject: Re: Further help needed. |
|
|
loopers wrote: |
So now, I will try to implement the code that you have given me. Here is what I did ...
Code: |
Private Sub Command1_Click()
Dim sr As StreamReader
sr = File.OpenText(Application.StartupPath & "C:\Windows\Desktop\Test.txt")
End Sub
|
But I still can't open a text file called Test.txt |
Well that little bit of code has declared a StreamReader object and set the object to point to the text file. Fair enough. It will be working, but don't expect to see anything on your screen: so far all that's happened is some memory has been allocated to the job of reading lines from the text file, but nothing has actually been done. Add a List Box object to your form (call it "List1") and stick the rest of Shaolin's code in and you'll see the List Box get filled with text from the file.
But unless I've misunderstood, that's not what you're trying to do. I think that what you're after is to launch the text file externally in an appropriate application such as Notepad.exe, in which case you should look at the API call mentioned above.
VB is great once you've got the hang of it. In a nutshell, you declare an object or variable, then set it to something or give it a value, then access it's properties/events/methods or read/change it's value. The syntax and control structures are fairly easy to follow, if you read Shaolin's code and try to translate into plain-english (like pseudo-code) you should find that it makes sense. Good luck!
|
|
Back to top |
|
|
|