<<See Tutorial 2 for creating this project. >>
We are going to use our "Lotto" program to utilize an InputBox. By now you should know how to go to the controls toolbox and place another Button control on your desktop. You may put it any place that you think is appropriate on your form.
Next, pick a textbox control and place it near your button. Go to the properties window for the textbox and find the property called "Name". Rename your texbox to "txtMyInput". Next, doubleclick the button and start coding your button, again, refer to previous tutorials to learn how to do this. In your code window, you need to dimension a new variable.
Dim strMyResult as String
The Dim statement is used to tell VB that you are going to use a variable and you tell it the name that you want to use. "strMyResult" is a name of a variable that I just made up. Many programmers like to use a certain way of naming their variables by adding the first 3 letters of the type of variable, so that, later, when they are looking at their code and see this variable, they know what type of variable that it is. So, the first 3 letters of my variable is "str", short for String. A String variable can hold many characters and is what is used for text in TextBox controls. In most programming languages, if you assign a number into a string variable, you couldn't use it for straight math purposes until you convert it to a number type variable.
Earlier, in Tutorial 2 we dimensioned a variable as type integer, and as I said then, if you dont understand math, you will have problems in dealing with number type variables. So, this time we need a string variable becuse we need an input of type "text" or string. Text and string will be interchangeable in my explanations except when I am talking about a type of variable. There is no type "text" variable; just string for general characters. Now, in plain english, we have dimensioned a variable "As" a type.
Let's now use the inputbox. Instead of copying the code below, type this code into your code window.
strMyResult=InputBox("Enter your name: " ,"MyInputBox","Bill")
As you may already see, the result of what you will input in the inputbox will be placed into the variable strMyResult. The inputbox function practically prompts you to put the proper information in each of it's sections as you type the code. The first section "InputBox" is the name of the built in function for VB, the next section inside the parentheses is the prompt that will appear near the input box. The next section is the label on top of the window that appears for the inputbox, the next section is the default value for the input box (in other words, if you just press enter or click the enter button "Bill" will get put into the variable strMyResult, and the inputbox will close).
OK, let's use the variable for something.
More code!
txtMyInput.Text=strMyResult
Let's try it out. Press F5 and the program will run. Go to the button that you just placed (I hope you gave it a label of some kind instead of the default Button##) and click on the button. your inputbox should popup and wait for your input. Just press enter. The textbox that you just placed should have the text "Bill" in it. Press the button again and enter something else inside it. Now close the form and get back to the code for the button.
More code!
MsgBox strMyResult
The above code will cause a popup to occur and whatever you enter in the inputbox will appear in the popup.
Try it!
You can turn this feature off by placing Rem in front of (left side of) the MsgBox code or simply by using the short form for Rem, which is the single quote('). The code should turn green when you move off of the line. This tells you that it is a remark. I always use the single quote for my code, but it is hard to read on a webpage.
There are many uses for the inputbox, I'll leave it up to you to decide where and when you will need user input.
This is the end of the tutorial for Learning How to Use the InputBox Control.
Source http://www.littlebeeps.com
Reviews:
Post a Comment