Introduction to Visual Basic - By TheHedz This tutorial will take you through the basics of Visual Basic 6 Visual Basic 6 Tutorial Very Basics: Ok. We start off by opening vb and you should see a screen that shows a new project window right in the middle of your screen, and should have items like Standard EXE, Active X EXE, and so on. I want you to just click on Standard EXE then click Open. Now if you look to the left you should see the Standard Toolbar (tools to add to the program) -- in the middle you should see the your Form1 inside of a project1 window (where you change your codes and add everything to your project) -- and at the top, right should be your Project Explorer (where you add, take away, and organize your forms, projects, and modules) First we'll start off with the Toolbar, and I'll explain this from top to bottom and left to right. (A lot of this stuff I shouldn't even waste time telling you, but I will) Top, Left = Pointer -- The Pointer does nothing really except makes sure you don't accidentally add an extra command on a form or something. Top, Right = PictureBox -- If you want to add a picture to your form with a border and be able to control how much of the picture you want to show, then this is the thing for you. 2, Left = Label -- This is quite self-explanatory. You use this to add a label to your form. 2, Right = TextBox -- This is used very often. It allows you to add a place for the user to add text and has so many uses. 3, Left = Frame -- I use this a lot to keep things organized and labeled. 3, Right = Command Button -- This is used the most as you can tell. It lets the user click on it to do what ever you want it to do. 4, Left = Checkbox -- This will allow people to check off anything they want. It's normally used in surveys and options. 4, Right = Option Button -- In my opinion is just the same as a Check Box except it's circular. 5, Left = ComboBox -- The ComboBox allows you to give the user a list of options or a list of anything for them to select form. Example = Internet Explorer has it as the address bar 5, Right = ListBox -- ListBox is just what is says. It lists what ever you want and if you want the user to add items, they can. 6, Left = HScrollBar -- This makes what ever you want go left or right (normally used with text boxes) 6, Right = VScrollBar -- Makes anything go up and down 7, Left = Timer -- This is great for making thing happen when you want it to happen, also really good for doing things over and over (Loops) 7, Right = DriveListBox -- This is a ComboBox except it shows your main drives. Example = c: Hard a: Floppy drive 8, Left = DirListBox -- (Directory List Box) this lists your folders. 8, Right = FileListBox -- This one gives you a listing of all of the files in a directory. 9, Left = Shape -- With this you can draw a circle, square, or rectangle ect... 9, Right = Line -- Does just what it says, draws a line in your form. 10, Left = Image -- Just like a picture box except borderless and automatically adjusts it's height. (Of course using the properties window you can give it a border) The other two you don't really need to know right now To get those tools on the form all you have to do is click on one and then click anywhere on a form and drag it a little bit. Or just double-click on the tool Properties and Actions: Changing Properties from the Properties menu: To adjust the properties is really easy. Over to the right side of your screen you should see a box that says Properties - Form1 and almost all of those are too easy for you to need me to explain it for you. But the ones I will tell you are used a lot. Caption, that is the Title of the things that you'll be seeing. Name is what the tool is called but that's only used when the coding starts. Backcolor is the color of the background and and Forecolor is the color of the foreground (text usually.) To change these you'll need to click on where it says forecolor and then click on the little arrow. First it will give you system colors, which are basic colors like blue, black, white, and gray, and if you click Palette you can choose from a lot more colors. Left is how far left things are on your screen, and Top is how high things are on your screen. The ones that I didn't mention you either wont need right now or are easy enough for you to figure out. For The Timer, to adjust how long it's intervals just click on the timer and select the intervals from there. Changing Properties with code: This is really not hard at all. To get where you can type in code you double click on a tool on the form, or on the form itself. Say you wanted to adjust code for a Command Button then you would double click on the command button. Ok to change properties when the program starts up you would double click on the form and you should see something that says Private Sub Form_Load() End Sub This as you can tell means when the form that you've selected loads up then it will do anything that you write in-between Private Sub Form_Load() and End Sub. If you want to change the caption of a command button, you'll first need to get the name of the command button that you want to alter and then you would type Private Sub Form_Load() Command1.caption = "what ever" End Sub See VB is really easy to understand. After you type command1. It should give you a list of things that you could do after that. Basic Actions From Code with If, Then Statements: If, Then statements are basically just like things you would run into in real life. Like say if you picked up a pencil and drew a line and you wanted to express that in words you would say If I drew on a piece of paper then it would leave a black line. That's exactly how VB is. If you wanted to make a very basic password box here's how you would do that. 1. Add a Text Box to your form, go to the properties window and then go to text and erase it so the box is blank 2. Add a Command Button 3. Double click on the command button to get to the code. Then you should see this Private Sub Command1_Click() End Sub From there you would type in this Private Sub Command1_Click() If Text1.Text = "password" Then MsgBox "Good Job", vbOKOnly, "Correct" Else MsgBox "Incorrect Password", vbOKOnly, "Error:" End Sub That might look complicated, but it's really not. I'll explain everything. If the text that the user put in the textbox is = to "password" then show a message box that will tell you if you've gotten it right or not. Msgbox (Tell VB that you want a message box to pop up) "Good Job" (The first text that you give a message box will that what is said in it) VbOkOnly (This is going to be what button(s) show up in the message box) don't worry you don't have to know these. After you type the, it will give you a list of these. "Correct" (The second text that's entered is the Title of the message box) In If, Then Statements Else is used if anything Else other then what you want to happen happens. Note: To run it to see if it works just click on that little arrow, play button looking thing at the top or go to run then Start, or just press F5. Note: If you want to save it, go to file then save project as, or if you want to make it into an actual usable program then go to File then Make project1.exe Here's the same code except it will start a different form instead of showing a message box. 1.Create a new form by going to Project then Add Form. Now If you were doing that excersie with me Then change the code to the following, or Else start a new project. Private Sub Command1_Click() If Text1.Text = "password" Then 'If the text in textbox1 is = to password Form1.Hide `makes the first form disappear Form2.Show `makes the second form show up End If `you need to add this if you use up more than one line in order for VB `to know that you're done with that If statement End Sub `ends command 1's code Note: if you type ' then VB will ignore everything from that point on in that line of code. That is for comments. So feel free to copy that whole code above. Changing Drives, Directories, And File Lists to match. First, I want you to close that project by going to File, Remove Project. Next make a new project by going to File, New Project, or just press Ctrl-n. Then go to Standard Exe. 1. Add a DriveListBox 2. Add a DirListBox 3. Add a FileListBox Ok now everything is going to be done when it starts up so this code is going to go into the form1_load. So double click on form1 Then follow the next example. For the paths to be determined from the start we need to do this: Private Sub Form_Load() ` Does what we want when the program starts up On Error Resume Next ` Always type this if you think there could be an error ` what is means is if an error occurs then do `what comes next ` and dont bother us Dir1.Path = "C:\windows\desktop" ` Dir1 is the name of the DirListBox. ` This is to change the path to the desktop File1.Path = Dir1.Path ` Change's FileListBox's path to whatever path ` Dir1 is on. Notice when you use another things ` text or path you don't need quotes End Sub 'Ends this Ok if we run this now then everything would start up looking at the files and folders on the desktop, but we're not done yet. We want to be able to change the folders. So if you look at the top left hand corner of the code box you should see Form1 in bold and if you look to the right you should see Load in bold. I want you to click where it says Form and go down to where it says Dir1 then it should automatically change on the other side to Change, and look like this in the code box Private Sub Dir1_Change() End Sub Now I want you to type this in there Private Sub Dir1_Change() File1.path = Dir1.path `This is telling VB that when ` the Directory box changes, to change the ` Filelistbox to whatever path the `directory box ` is in End Sub Note: See, here we don't need the if or then because by using Dir1_change, whatever you put in there is already going to be the Then action of if dir1 changes. Next go to the top left hand corner again and go to where it says Dir1 and change it to drive1. Private Sub Drive1_Change() On Error Resume Next `since Drive could deal with a: or your cd-rom `drive it could cause an error if nothing is in it. Dir1.Path = Drive1 `we change the path of directory 1 to match the Drive1 End Sub Note: Notice that we don't use Drive1.path. That's the paths in the Drive are the Directories. So for that we use the Dir1 Using Integers: 1. Make a new project 2. Put a command button on the form 3. Put a Textbox on the form Ok, the first that we'll do is make a letter stand for an integer, we'll us X. And to make a letter stand for an integer we have to do this Dim X as Integer When you declare something as an integer or variable you should either do it in the form1_load section or scroll up to the very top of the code box and make a space there and type that in. Now, I want you to put all of this in as I explain it to you. Dim X As Integer ` This will make X turn into an integer Private Sub Form_Load() X = 5 `This make the int X = the number 5 when it starts up Text1.Text = X `This makes the text that's in the textbox1 = 5 End Sub Private Sub Command1_Click() X = X - 1 `When command1 is clicked X = what ever # X is - 1. ` So since it's 5 at the beginning X will then become 4. If X <= 0 Then X = 0 `This makes sure X doesn't go under 0. `See it says if X is less than or = to 0 then make X = 0 Text1.Text = X `This Causes text1's text to display whatever number X is this time. End Sub You should understand all of the things I mentioned here but if you didn't just read over what I wrote and i am sure you'll figure it out. TheHedz