Wednesday, October 6, 2010

First Post

Alright, here we go...
This is my new blog.  First a little about me, I am a student at Virginia Tech.  I am currently a freshman.  I plan to study Computer Engineering.  All of the Visual Basic programming knowledge I have is self taught, I have never taken a VB class in my life, and I don't think I ever will.  I am a very big fan of the .NET framework, and I absolutely love Visual Studio.  I have never felt quite as comfortable with any other IDE (Integrated Development Environment) other than Visual Studio.  I started programming on a TI-84 calculator and then moved on to VB.NET which I still use to this day.  I can also program in JAVA and C#.NET and will soon be learning C++.

But enough about me, lets do something with programming...

For my fist example / lesson, I will be demonstrating the use of Message Boxes.  I know that this is a very elementary topic, but you have to start somewhere, and I don't want to scare away any new programmers out there.

Difficulty: VERY EASY

To use a message box as a notification, you simply use the code:

MsgBox("Your message goes here!", ,"Message Box Title")


The reason I have that blank argument in there is because that is where the message box style would go.  For this example, we just wanted the message box to be a notification, so it only needs an 'OK' button.  Lucky for us that is the default style.  If you don't have a specific title in mind for the message box, you can simply close end the argument list with the message and the title will be the name of your application.


If you want to ask a question with the message box, such as yes or no, you must set a style parameter.  You must also be ready to receive the answer.



Dim ans As New MsgBoxResult
ans = MsgBox("Do you want to save before exiting?", MsgBoxStyle.YesNoCancel, "Save?")


If ans = MsgBoxResult.Yes Then
     ' Save and exit
End If
If ans = MsgBoxResult.No Then
     ' Don't save and exit
End If
If ans = MsgBoxResult.Cancel Then
     ' Don't save and don't exit
End If


As you can see here, I allowed there to be three possible answers to the question I asked: Yes, No, or Cancel.  First I had to declare a variable to store the user's response in.  This type of variable is aptly named 'MsgBoxResult'.  Then I told the program to assign the value of the message box to the variable I declared.  As you can see for the style parameter, I used 'MsgBoxStyle.YesNoCancel'  this tells the computer that you want a message box with those three options.  Now don't get confused, you can just put any old thing there, you must pick a style that exists.  


I can't think of much else to say about message boxes, that is about the limit of their functionality, so I will wrap up with a few notes to remember about message boxes.


- While the message box is showing, the program does not run.  What I mean is the code stops at the line of the message box and waits for the user to dismiss the message box before continuing.  This can be useful if you want to check on variable values in the middle of a complex operation. 
- Message boxes usually pop up on top of your application.  One exception to his is in ASP.NET (web page programming)  where the message box may not popup on top of the web browser.  I have not figured out how to fix this and it is very annoying.  Due to that flaw, message boxes are virtually useless in web programming.


I hope you enjoyed this little talk, and hopefully learned something.  If you have a good idea for next time feel free to email them to me.

No comments: