Koyas
Koyas

Reputation: 98

Set the Progress Bar Value according to data entered?

How can Set the Progress Bar Value increasing/ decreasing according to data entered in each controls on a form ? I mean, I am working with number of controls, so go for a sample [Suppose I have four textboxes, Progress Bar Maximum 100, when entering txt_1 Progress value have to increase to 100/4= 25, if I remove data from txt_1 the Value should decreased to Zero.

Sorry for my language, I am not good in English.

Can any one help me, please..... Thank You.

Upvotes: 1

Views: 2319

Answers (1)

Creator
Creator

Reputation: 1512

The Firs thing you do is set the progressbar maximum in your formload event to the number of textboxes you are using.

Dim TextBoxNumber As Integer = 4
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ProgressBar1.Maximum = TextBoxNumber
End Sub

Then you need to make 2 events.
1. TextBox enter event."Private Sub TextBox_Enter(sender As Object, e As EventArgs)"
2. TextBox TextChanged event"Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)"

Then Link all the textboxes you want to use to the events.
enter image description here enter image description here

In these events you will use the Sender.
Make a Dim and convert the sender to type Textbox to use its properties." Dim TextControl As TextBox = CType(sender, TextBox)"

In the Enter event you count the text that is entered in the textbox.
If the count is 0 you set the boolean to true and if its more than 0 you set the Boolean to False.
This Boolean you will use in the Text Change event.

Dim CheckTextBox As Boolean
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles TextBox4.Enter, TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
    Dim TextControl As TextBox = CType(sender, TextBox)
    If TextControl.Text.Count > 0 Then
        CheckTextBox = False
    Else
        CheckTextBox = True
    End If
End Sub

In the Text Change event you need to do a few things.
1.Use a if statement to check if the textbox is empty."This will only trigger if the user deletes the text in the textbox."
So if empty remove 1 from the progressbar and set CheckTextBox"Boolean" to True
"ProgressValue = ProgressValue - 1"
"CheckTextBox = True"

2.Then use a ElseIf to check if the CheckTextBox"Boolean" is True.
If true add 1 to progressbar and set Boolean to false.
"ProgressValue = ProgressValue + 1"
"CheckTextBox = False"

You need to set the Boolean to false because otherwise you will add 25 for every time you add somthing to the textbox.

Dim ProgressValue As Integer
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
    Dim TextControl As TextBox = CType(sender, TextBox)

    If TextControl.Text = "" Then
        ProgressValue = ProgressValue - 1
        CheckTextBox = True

    ElseIf CheckTextBox = True Then
        ProgressValue = ProgressValue + 1
        CheckTextBox = False
    End If
    ProgressBar1.Value = ProgressValue
End Sub

Upvotes: 1

Related Questions