ListBox Behavior in VB.NET

In this arcticle I have described about the behavior of ListBox with the help of calculating the table of any number. ListBox is considered as one of the main control of window form in VB.NET.
  • 2450

INTRODUCTION

This article is related to brief discussion about ListBox . ListBox control enables us to display a list of items from which the user can select one or multiple items from the list. The default is for the ListBox to list the items vertically however setting the multicolumn property to true the items are listed horizontally. The control is typically used to display data from a database. The items listed within the ListBox cannot be edited directly.

Namespace: System::Windows::Forms
System.Windows.Forms (in system.windows.forms.dll)
 

Here I  am going to explain some behavior and use of  ListBox along with TextBox and Button. For this we take an example of times table of any number. Let start a new project and take one form. Within the form take three TextBoxes , one Button and a ListBox . After taking these Controls check that TabIndex  property of  TextBox1 must set to 0 and TabIndex property of TextBox2 is set to 1 and set Text property of Button is to Calculate.

The way of addition of  Listbox shown as follow:

img2.jpg

Then of  form should be looks like that:

img1.jpg

When the Caluculate  Button is clicked, the programme will put the numbers from the Textbox into two variables. We'll then put a value into a variable called multiplier.

So, here's the code for the entire programme. Double click on Calculate button, and add the following:

Dim number1 As Integer
Dim number2 As Integer
Dim multiplier As Integer
Dim answer As Integer
Dim i As Integer
number1 = Val(TextBox1.Text)
number2 = Val(TextBox2.Text)
multiplier = Val(Textbox3.Text)

After adding this code in calculate the codewindow appeares like that

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim
number1 As Integer
    Dim
number2 As Integer
    Dim
multiplier As Integer
    Dim
answer As Integer
   
number1 = Val(TextBox1.Text)
    number2 = Val(TextBox2.Text)
    multiplier = Val(TextBox3.Text)
    Do
While multiplier < Val(TextBox3.Text)+1
    For
i = number2 To number1
    answer = i * multiplier
    ListBox1.Items.Add(i & " Times " & multiplier & " = " & answer)
    Next
i
    multiplier = multiplier + 1
   
Loop
End Sub
End Class

 When we have finished our programme and  run the programme and see how it works. We should see this appear in our List Box

img3.jpg

When we click on the Calculator Button the output become shown as follows:

img4.jpg

I hope by this way we can understand ListBox more easily and confidentially.

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.