banner



How To Add Text To A Label In Visual Basic

In Visual Bones 2019, there are many kinds of controls that yous can use to build a VB 2019 app. The controls can be divided into many categories, namely Common Controls, Containers, Menus and Toolbar, Data, Components, Printing, Dialogs and WPF Interoperability. The controls are available in the Toolbox, as shown in Figure 5.i.

Figure five.i: Visual Basic 2019 Controls

5.1 TextBox

In this lesson, you volition learn how to work with some of the common controls in Visual Basic 2017. Amidst the mutual controls are the characterization, text box, push, listing box, combo box, PictureBox, timer and more. However, we shall only deal with the text box and the characterization in this lesson. The text box is for accepting input from the user as well every bit to display the output. It tin handle string and numeric data merely non images or pictures. String in a text box tin be converted to a numeric data by using the function Val(text). The characterization control is only for displaying a caption/title or to brandish an output.

Example 5.1

In this awarding, add two text boxes and a button to the Form. Change the text of the push button to ADD. Next, click on the push and enter the following code:

Private Sub Button1_Click(sender As Object, east As EventArgs) Handles Button1.Click  MsgBox("The sum is "& Val(TextBox1.Text) + Val(TextBox2.Text))  End Sub

This program will add together the value in TextBox1 and the value in TextBox2 and displays the sum in a message box.

The output UI

vb2015_fig5.01
Effigy five.2

Subsequently clicking the Add push, you will get the answer in a bulletin box, as shown in Effigy five.3:

vb2015_fig5.02
Figure 5.3

v.ii The Label

The label can be used to provide instructions and guides to the user likewise every bit to brandish the output. It is different from the TextBox because information technology can only display static text, which means the user cannot alter the text. Using the syntax Characterization.Text, it can brandish text and numeric data. You lot can modify its text in the properties window or program it to modify at runtime.

Example 5.2

In this visual basic 2019 application, instead of showing the sum in a message box, nosotros want to display the sum on a Characterization. This is a elementary reckoner program that can calculate the sum of two numbers entered past the user. To blueprint the interface,  you add 2 TextBox control, three Label controls and a Button on the Course. Under the respective properties windows, change the name of the offset text box to txtNum1 and the name of the second TextBox to txtNum2. Next, change the text of the starting time Characterization to +, the text of the second Label to =. For the third Label, alter the autosize property to false and the name to LblAns. Elevate the tertiary Label to an advisable size and gear up its background to yellow color. For the Button, change its name to BtnCal and its text to Calculate.  Lastly, change the text of the Form to Simple Figurer. The two text boxes are used to accept inputs from the user. The Button is programmed to calculate the sum of the two numbers using the plus operator.

The Code

Public Course Form1 Individual Sub BtnCal_Click(sender Every bit Object, e Every bit EventArgs) Handles BtnCal.Click Dim num1 Every bit Single = Val(TxtNum1.Text) Dim num2 Every bit Unmarried = Val(TxtNum2.Text) Dim Sum As Single = num1 + num2 LblAns.Text = Sum.ToString  End Sub End Class        

The Dim keyword is to declare a number as a single precision number and the office Val is to convert a string to a number. The method ToString is to display output as a string. We shall learn more about Dim in a afterward lesson.

vb2015_fig5.1
Figure five.4 The UI

How To Add Text To A Label In Visual Basic,

Source: https://www.vbtutor.net/vb2019/vb2019_lesson5.html

Posted by: palmerrecance.blogspot.com

0 Response to "How To Add Text To A Label In Visual Basic"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel