Thursday, November 20, 2008

Fri Nov 21: Using combox boxes

Greetings, fellow code monkeez,

Today being Friday and all, I thought that we should keep things as simple yet as informative as possible. Plus, I wanted to introduce you all to a couple of extremely useful controls:

A) the PANEL

B) the combobox aka, "pulldown menu"

A panel is good for holding other controls together as if they functioned as one unit. Combo boxes are good for holding lists of items in a visible fashion, so you can see them and use them. We are going to make use of both today. I will show you how the first time around, then you will find some other uses for BOTH these controls

OK, lets launch into this, shall we?

Step A) start up a new project and call it PullDowns

Step B) Add a button to your new form, and set its text to say "Shaazaam!"

Step C) Next, add a Panel to your form. Make it as big as possible without overlapping the Button you just made. Set its background color to red or blue or green or something bright. Set its visibility to "False"

Step D) Add a webbrowser control onto the Panel, and resize it so that it takes up most of the room inside the panel.

Step E) drag a combobox onto the Panel, and set it just above the webbrowser control

Now to set some properties and add some simple code. Here are the next steps:

Step F) your ComboBox has a property called "ITEMS"; next to it is the word "Collections" followed by a little button with dots on it. Click on that button

Step G) This pops up a place where you can add items to your combobox, similar to the way you added items to your menus the other day. Our collections list is going to consist of webpage addresses, something like this:

http://www.thinkgeek.com/
http://www.bittorrent.com/
http://www.limewire.com/

By all means, set your website addresses to whatever you want, as long as they are school appropriate

Step H) Doubleclick on the combobox to get into its code. We are going to use the items in the combobox to direct the webbrowser control to display a webpage. The code for it is pretty simple:

MsgBox(ComboBox1.SelectedItem)
WebBrowser1.Navigate(New Uri(ComboBox1.SelectedItem))

Step I) Now, add this code to that button you made:
Panel1.Visible = True

RUN your program to verify that it works; I will be observing you through LanSchool. Once you have mastered this, I want you to be able to prove what you know by using both a combobox and a panel in some other way. PLEASE POST A DISCUSSION ON HOW YOU DID THIS.

Please upload this new mini project, then you may return to work on your project, which you will also upload before the class is done.

Cheers,
Mr. L

Wednesday, November 19, 2008

Thurs Nov 20: Time to Clock OUT!!!

Greetings, O keepers of the Microsoft code!
Today, we are going to accomplish three great and wonderful goals:

A) Learn the basics of using the TIMER control, and apply it to something useful

B) See if you can apply the techniques you've learned to make something else happen at a regular interval, guided by a timer.
MAKE A DISCUSSION POST ON OUR GROUP STATING WHAT YOU DID HERE

C) work on your individual projects -- and be sure to UPLOAD both of todays projects to the "VBDEPOT" folder at BOX.net


Right then, let's start with the first one. Here's what you have to do
1) start up a new project and name it after yourself plus the phrase "TimingX"

2) Add a label to Form1. You DON'T have to rename it, but you DO have to change the text and the font size. Make the size at least "42"; make the text say basically whatever you want, as long as its school appropriate

3) Set the Label's "visible" property to FALSE

4) Add a TIMER control to your form by dragging it from the toolbox onto to the Form. Set its "Interval" property to 1000, which means the same as one second

5) get into the TIMER code by doubleclicking on it. Add this code snippet:

' this calls a function named "secret word" thats stored in a module
SecretWord( )

6) OK, so now you need a function and a module to put it in. So, first things first, add a Module -- if you don't know how, ask your buddy, not me -- we've already done this several times now!

7) In the module, you are going to create a function that will make the Label appear and disappear according to what interval is set in the timer. Like any good function, it will need to return a variable. Since we're dealing with a "visible/hidden" situation, we will only need two states, ON and OFF. so your variable will be a boolean.

8) Here is the way I set up this function. Try it my way; or if you feel daring, try another way of doing this same thing:

Public Function SecretWord( ) As String
'the on or off variable

Dim OnOff As Boolean = False

'if the Label is hidden, then show it!
If Form1.Label1.Visible = False Then
Form1.Label1.Visible = True
OnOff = True

'if you can see the Label then HIDE it!
Else
Form1.Label1.Visible = False
OnOff = False
End If

'return the value of the variable
Return OnOff
End Function



9) Finally, get back into Form1, and this to its LOAD statement:

Timer1.Enabled = True 'make sure the timer is turned on
SecretWord( ) 'call the function the first time

OK, let's see if we can get that to work! I will come around to see if you've got it right. Once you've mastered that, I want to see you make a timer do something different: your choice as to what different will mean.

The rest of the class will be yours

Cheers,
Mr. L

Tuesday, November 18, 2008

Tues Nov 18: kicking butt, taking names

Or words to that effect! Hello, and welcome back. Today, you will be allowed time to work on your individual projects again. Pleae make sure that you upload them to our "Depot" folder sometime before the end of class.

Additionally, I would like each of you to think of a specific request for a technique you would like to master. Please do so in the form of a discussion post, and title it your name plus "basically requests". I will do my best to incorporate theese requests into the curriculum, so please be sure that your request are as precise and as specific as possible. Remember, when the great computer was asked the question "what is the meaning of life, the universe and everything?" it took a million years for it to come back with the answer "42"

Nuff said!

Mr. L

Sunday, November 16, 2008

Mon Nov. 17: finishing up and moving on

Greetings, buccaneers of code,

Today you will need to do things:

A) Make absolutely sure that ALL the bugs have been worked out of your "Menus" mini-projects from last Friday. They should work as fully functioning webpage editors by now; when they do, please upload them to our folder on Box.net. Make sure they are named after yourself plus the word "menus"

B) Continue working on your own projects. Pass them in as well at least 3 to 4 minutes before the end of the class.

Cheers,
Mr. L

PS If you discover anything cool, please be sure to post a discussion on our Google group. If you have any long term difficulties, please be sure to email me at my gmail address.

Tuesday, November 11, 2008

Fri Nov 14: part two

OK, so as you may have guessed, what we're going to try to do is to be able to open up a webpage in your textbox, where you can change it's code. Then, you can save the changes, THEN you can open up this new improved webpage in a webbrowser so you can see the differences.

Now, here's the rest of the steps in how to do that:

A) Add a label to the bottom of Form1, somewhere underneath the textbox. No need to rename this either

B) Add this code to the "Open in Browser menu item code, just above the one line of code you already have there (ya know, the one that says Form2.visible = true)

'set the value of the current file being worked on into this Label
Label1.Text = theFileName
'call the function in the module that makes use of the text value of this label
'in assigning a URL to the webbrowser control in Form2
ShowInBrowser( )

C) Since we're going to be passing information from one form to another, it would easier to use a module. So, create a module by clicking on the "Project" menu, then clicking Add Module

D) In this module, you will need to add a simple function that will receive the value of the text in that new label you made, and return it as a String variable to Form2. Here's how my function looks:


Public Function ShowInBrowser( ) As String
Dim sendPageInfo As String
sendPageInfo = Form1.Label1.Text
Return sendPageInfo
End Function

E) Finally, you have to get into the code for the "Form Load" event for Form2. You should know how to get there already; when you do arrive, add this code to it:

'show which webpage we're loading into the browser
MsgBox(ShowInBrowser())
'make the browser go to the webpage we want
WebBrowser1.Navigate( New Uri(ShowInBrowser( ) ) )

OK, that should do it. Compile, make sure you gots no errors, then run the program. You should be able to open a webpage in your textbox so that you only see its code, you can change that code, save the changes then observe the changes by opening it in a browser you made yourself!

cheers,
Mr. L

PS If you need a webpage to play with, I uploaded one to the VBdepot folder on Box.net

Fri Nov 14: monkeying around with web pages and menus!

Hello all you kwazy kode monkeeeees,

Today, before you work on your projects, you are going to add some more functionality to your "menus" project. Specifically, You are going to be able to open up WEBPAGES inside your textbox, mod their code then pop up the way the new webpage looks in another form which you will add to this lil project.

Here's what to do:

A) you will need to add an IF statement to your save menu item. It's to prevent saving files with no extensions and no filenames. Please place it BEFORE all the code you have in the SAVE menu item:

If theFileName = "" Then
SaveFileDialog1.Filter = "Text files (*.txt)*.txtweb pages (*.html)*.html"
SaveFileDialog1.ShowDialog()
theFileName = SaveFileDialog1.FileName
End If

B) notice how I have modified the "filter" so that you can see both text files and web pages when you are saving. You will also modify the filter found in the OPEN and SAVE AS menu items, in exactly the same way.

C) Add a second form -- nope don't care if you rename this one

D) Add a webbrowser control to this form -- also no need to rename this control

E) get back into "Form1" and add this item to the file menu "Open in browser"

F) Doubleclick on that new menu item, and add just this snippet of code:

Form2.Visible = True


OK, so whatya got so far? Well, you have a new menu item that opens up a new form that has a web browser control on it. Also, we've chnaged the filters of all our menu items so they can see both text and web pages. Lets make sure it works before going on to the next step

Cheers,

Mr. L

Wed Nov 12: Ok alright, quit yer bellyachin' here's the rest of the code!

Just add this stuff underneath the rest of the code in your SAVE AS menu item

'dim another streamwriter variable
Dim newWordWriter As System.IO.StreamWriter
'make a new streamwriter and pass it the variable
'thats equal to whatever the OpenFileDialog found
newWordWriter = New System.IO.StreamWriter(theFileName)
'set text of the textbox to all the stuff found by the reader
newWordWriter.Write(MyWords.Text)
'shut down the writer
newWordWriter.Close( )

Cheers,
Mr. L

Wed Nov 12: modifying the SAVE menu item, part two

OK, so now we know how to create and save files, open them, mod them then save them.

NOW, it's time to be able to add a "Save As" menu item. Here's how you do it:


A) goto your "Toolbox" at the left scroll down and find the "SAVEFILEDIALOG". drag it onto to the space at the bottom fo your form, the one that already has your menustrip and your OpenFileDialog. No need to rename this control, just leave it as SaveFileDialog1


B) In your form, if you click once on the FILE menu header, you will see the whole list of menu items you've got

C) at the bottom, you will see a thing that says "Type Here" that's where you type in "SAVE AS".

D) Very carefully drag that item up above the EXIT and below the SAVE -- not that it really matters, but wouldn't it just bug the crap out of you if we left it there?

E) doubleclick on that menu item to get into its code, and then add this code block:

'this just gives a title to the dialog box
SaveFileDialog1.Title = "Save a Text File As......."
'this says what kind of file will be saved
SaveFileDialog1.Filter = "Text Files*.txt"
'this establishes that we are naming this thing the way it
'was until we decide to name it something else
SaveFileDialog1.FileName = theFileName
'this opens the SaveDialog, so you can save the file with its new name
SaveFileDialog1.ShowDialog( )
'when you change the name of the file,
'this changes the value of the variable
'that way, the StreamWriter writes to the new file
'not the original
theFileName = SaveFileDialog1.FileName

Cheers,
Mr. L

PS There's something missing at the end of this code. Try to guess what you might need to copy and paste from somewhere else in your code.

Wed Nov 12: modifying the SAVE menu item, part one

Hey there fellow coders!

Here's how we can dramatically revamp your menu project, so that you can open an existing file, make some changes to it, then save the changes to that SAME file, in it's same location.

Here's what you do:

A) MOVE this line of code

Dim theFileName As String

from the place its in now to just below "Public Class Form1"
that way both the "Open" and "Save" menu items will be able to use it :-)

B) rip out all that crap that we have in the current SAVE menu item code, and replace it with this:

'dim a streamwriter variable
Dim wordWriter As System.IO.StreamWriter
'make a new streamwriter and pass it the variable
'thats equal to whatever the OpenFileDialog found
wordWriter = New System.IO.StreamWriter(theFileName)
'set text of the textbox to all the stuff found by the reader
wordWriter.Write(MyWords.Text)
'shut down the writer
wordWriter.Close( )

I swear to GOD, it really is that simple! Let's make sue that all have it working, OK?

Cheers,
Mr. L

Saturday, November 8, 2008

Mon Nov. 10: more menu fun, Part The Third

OK, now its time to get serious: it's all well and good to FIND a file, but we want to load it into the textbox on our form. Otherwise, what's the point? So here's what we need to do that:

a "reader" of some kind. We've already used file "writers" to create files and put stuff in them. This is kind of the same only in reverse. Here's what you gotta add to yer code:

'dim a streamreader variable
Dim wordReader As System.IO.StreamReader

'make a new streamreader and pass it the variable
'thats equal to whatever the OpenFileDialog found
wordReader = New System.IO.StreamReader(theFileName)

'set text of the textbox to all the stuff found by the reader
MyWords.Text = wordReader.ReadToEnd

'shut down the reader
wordReader.Close( )

OK, so lets see if you can use this to open a file inside your textbox. Try it out, and understand that it will ONLY look for "Notepad" type files.

As you can see, I have extensively COMMENTED my code, so its pretty darned obvious what the heck all this stuff is for. Ahem!! And btw, I have a final message to Mr. Octopus, Mr. Crab, Mr. StarFish, Mr. Sponge, etc etc etc: you got ZERO credit for the commenting assignment. I don't have the time to figure out who those names are supposed to mean


Cheers,
Mr. L

Mon Nov. 10: more menu fun, Part The Second

OK, now you've got a FileOpen thing going on, but it doesn't do very much. So let's remedy that: let's make it so it can find a file, and when you click on it, a Message box pops up that says the name of the file and where it is located. Add this code just BELOW where the other code went.

'this allows you to create a filename based on what you've chosen
Dim theFileName As String
theFileName = OpenFileDialog1.FileName
MsgBox(theFileName)

All this code does is make a String variable (useful for words and text and such) and then sets that variable equal to whatever is chosen in the OpenFile thingy. Then, it passes this to a MsgBox, which pops up to tell you what you've chosen

Make sure this works, then we will move on to the third and final installment

Cheers,
Mr. L

Mon Nov. 10: more menu fun, Part The First

Gr33tings krazy k0d3 m0nk3yz!


Today, we will be working with menu stuff again BEFORE you get to work on your projects. So open up that menu project you have stored somewhere in your vast archive of programming fun, and let's get started!

OK, ready? Here's the first you have to do:

A) You already have a thing called a menustrip at the bottom of your form. You are going to drag an "OpenFileDialog" thingamabob next to it. Just scroll down the list of controls at left and you should see it in there somewhere.

B) Now you will have to connect this new control to an item on your menu. There's an "Open" menuitem available so doubleclick on it to get into its code

C) We're going to do this a piece at a time, so let's start with some very simple things. Type in this following code:

'this says where you start
OpenFileDialog1.InitialDirectory = "C:\"
'this just gives a title to the dialog box
OpenFileDialog1.Title = "Open a Text File"
'this says which kind of files to look for
OpenFileDialog1.Filter = "Text Files*.txt"
'this opens a fileDialog, so you can go looking for files
OpenFileDialog1.ShowDialog( )


Basically (hahaha Ryan) this code makes a FileOpen dialog box pop up when you click on the Open menuitem. It will start in the main directory of your hard drive, and it will ONLY look for text files. Plus, it will have a cool title that says its only looking for text files. Try it and see what you get!

Cheers,
Mr. L

Thursday, November 6, 2008

Thurs Nov 6: Whose Line Is It Anyway?

Greetings VB code monkeys!

Today you will be looking over the code of (most) of your classmates, and rating each one based on it's "ease of translation". By this I mean two things:

A) How well is it commented? Has the author of this particular program made enough notes about what is going on in their program for you, a fellow programmer, to be able to figure out what they are doing now, and what they are going to TRY to do in the future.

B) How easy is it to "read" this program? Even beyond the commenting, is the code set up so that it is easier for you to figure out what they mean to do with this program? Have they re-named their controls with descriptive words that tell you what each thing is for? Have they divided up their program among different forms, and made use of modules, such that it seems that this program would not only be easy for you a fellow programmer to debug, but for the average person to use?

I will need you to post a discussion on our Google group where you will write at least two complete sentences about each program. Please list the name of the program -- PROGRAM1, PROGRAM2, etc etc -- followed by those descriptive complete sentences. At the end of each one, assign a rating from 1 to 5, with 1 being "what the heck was THAT? It's completely unreadable!" to 5 being "D--n you're good, I envy you, you complete show off!"

Additionally, I wish for you to make one constructive suggestion OR ask one specific question about each program. This should be done at the very end of your discussion posting.

When you are done with this, please continue on with your project work

Cheers,
Mr. L

PS Here is the address of the folder where you will find these code sheets:

http://www.box.net/shared/rsoath5sk8

Wednesday, November 5, 2008

Wed Nov 5: Before we begin....

Before we begin, I need each of you to do the following:

A) Open up your projects

B) Oen up WORD

C) Save this Word document with your name followed by the phrase "My Code"

D) Put your name at the very top of this document

E) Very carefully copy ALL the code for your program into this word document, one form at a time. Before sure to include ALL your code, including all comments you have made in it, EVEN IF THE CODE DOES NOT ALWAYS WORK

F) Also, be sure to include all your code, from ALL your forms and your module if you have one

G) When you are done with this, please upload this WORD doucment to the VBDEPOT folder on BOX.NET When I get them all then we may proceed to the next thing we are doing

Cheers,

Mr. L

Sunday, November 2, 2008

Nov 3: more work with functions and modules

Hey there again!

Now that you kinda sorta know how to call a function and then make it do somethign, lets see about calling a function that will call many functions, each of which does something different. Here is what to do:

A) get back into your module and add the following code:

Public Function CallAllRandoms( ) As Integer
Random1( )
Random2( )
Random3( )
Random4( )
Random5( )
End Function

Public Function Random1( ) As Integer
Dim q As Integer
q = Int(Rnd( ) * 5)
Return q
End Function

Public Function Random2( ) As Integer
Dim z As Integer
z = Int(Rnd( ) * 5)
Return z
End Function


B) get back into form1, and then doubleclick the button that says "Call Many Functions", and add this code to it:

CallAllRandoms()
Label1.Text = Random1( )
Label2.Text = Random2( )

Cheers,
Mr. L

Mon Nov. 3: Using Modules

Hey there hi there ho there,

Today, before you get onto working with your projects some more, you will need to do three things:
A) Create a module
B) Create a function
C) connect a button to some functions and make it do stuff!

Oh, OK, so you wanna actually KNOW what those two things are? Well, let's start with the second one. A function is just some code that makes things happen. It's kind of like the events that we've been using, events like "keydown" or "buttonclick"EXCEPTTTTTTTTTT that functions don't belong to any particular control. they can just kind of sit around all by themselves and wait to be used by any control you say that they should be used by. Which brings us to:

MODULES. A module is just a place where you can store functions. These functions can be called by any control or any form within your program.

Alllllllllllllllllllllllllllllllllllllllllll RIGHTY then, let's launch into this shall we?

What to do

A) start up a new project, and name it after yourself, plus the word "functions"

B) In "Form1" please create two buttons and five labels. Replace the text of one button with this text "MessageBox Function"

C) Replace the text of the other button with this "Call Many functions"

D) Create a module by clicking on the Project menu, then clicking "Add Module". This will add a tab called "Module1.vb" at the top of your form.

E) Click on that tab if necessary, and add this code right below where it says "Module Module 1"

Public Function AddRandom() As Integer 'declaring a function that adds integers
Dim x, y As Integer
'declaring two integers and assigning values to them
x = Int(Rnd( ) * 10) ' a random number
y = Int(Rnd( ) * 20) 'another random number
'adding two integers and return the value
Return (x + y)
End Function

F) Get back into the main form, and doubleclick on the button that says "MessageBox Function"

G) add this to its code:
MsgBox(AddRandom())

Run the program to make sure it works. When everyone is set, I will pass out the code sheets to make a function that calls other functions.

When everyone is done with that, you will upload this new program to the "VBdepot" folder on Box.net, and then proceed to working with your projects

Cheers,
Mr. L