• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

Very Simple Visual Basic Syntax Help Needed

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More

View previous topic :: View next topic  
Author Message
loopers
Just Arrived
Just Arrived


Joined: 19 Nov 2002
Posts: 1


Offline

PostPosted: Mon Dec 09, 2002 6:48 pm    Post subject: Very Simple Visual Basic Syntax Help Needed Reply with quote

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
View user's profile Send private message
ShaolinTiger
Forum Fanatic
Forum Fanatic


Joined: 18 Apr 2002
Posts: 16777215
Location: Kuala Lumpur, Malaysia

Offline

PostPosted: Mon Dec 09, 2002 6:54 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
TheKingster
Link Spammer
Link Spammer


Joined: 03 May 2002
Posts: 0
Location: UK

Offline

PostPosted: Mon Dec 09, 2002 7:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
Wombat
Trusted SF Member
Trusted SF Member


Joined: 24 Apr 2002
Posts: 0
Location: Canberra

Offline

PostPosted: Mon Dec 09, 2002 11:38 pm    Post subject: Reply with quote

I had a similar question a while back, a quick site search on "shell" found the topic that shows you how to use the API call instead of using Shell:

http://www.security-forums.com/forum/viewtopic.php?t=736
Back to top
View user's profile Send private message
loopers
Just Arrived
Just Arrived


Joined: 19 Nov 2002
Posts: 1


Offline

PostPosted: Wed Dec 11, 2002 7:08 pm    Post subject: Further help needed. Reply with quote

Thanks for your code, Shaolin Tiger. Smile 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 Smile
Back to top
View user's profile Send private message
Wombat
Trusted SF Member
Trusted SF Member


Joined: 24 Apr 2002
Posts: 0
Location: Canberra

Offline

PostPosted: Fri Dec 13, 2002 12:55 am    Post subject: Re: Further help needed. Reply with quote

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
View user's profile Send private message
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Looking for more Windows Networking info?

Sign up to the WindowsNetworking.com Monthly Newsletter, written by Enterprise Security MVP Deb Shinder, containing news, the hottest tips, Networking links of the month and much more. Subscribe today and don't miss a thing!
View a sample newsletter.

Become a WindowsNetworking.com member!

Discuss your Windows Networking issues with thousands of other Windows Newtorking experts. Click here to join!

Community Area

Log in | Register

Readers' Choice

Which is your preferred data recovery solution?

Follow TechGenix on Twitter