Day 3 is for Textbox. Text Box is the Second most used object. its an Input object i.e we use it to accept input from the enduser and make decisions on that input and produce respective output.
A simple example will be to develop a small program that will accept two numeric values and add them and show the result. so for this purpose we will take Two TextBoxes , Three Lables and a Command Button.
Rename the Textboxes as TxtFirst and TxtSecond
Rename Lables as Lbl1, Lbl2 ,LblResult
change the captions of lables as First Number, Second Number, Result respectively.
Rename Command Button as CmdResult
Change its Caption To Add
Now goto Click Event of CmdResult and do the code as
Private sub CmdResult_Click()
LblResult.Caption="Result is = " & Val(TxtFirst.Text) + Val(TxtSecond.Text)
End Sub
Now Run the Program, Enter First number in first Textbox and second in the second textbox then click the Add Button, it will show you the Result.
----------------------------------------------------------------------------------------------
usually textboxes are used to get strings. i.e if you enter numeric values too in textboxes but they are considered string. if we remove the Val() function from the above code then it will consider the numbers as strings and will never add them but instead it will concate them so in that case the result of 2+3 will be 23 not 5
because + sign is the concatenation operator for Strings and by default everything in textbox is considered as string. you have to use Val() function to tell vb that you are using numeric properties not string properties.
another Concatenation operator is & sign in Vb, but it is for all types of data i.e 1 & 2 will be 12
or val(1) & val(2) will be 12 so it means that irrespective of datatypes it is a concatenation operator.
we opten use locked property of textbox to lock it for input or enabled property to just show something that cannot be changed by anybody directly by typing.
The Best example whete you can use command Button and Text box in a better way is example of Calculator.
ReplyDelete