• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

Progress Bar

I am trying to add a progress bar in this button click. can anyone show me where ?

and how
   
 Private Sub btnAddUser_Click(sender As System.Object, e As System.EventArgs) Handles btnAddUser.Click
        Try
            btnAddUser.Enabled = False
            Dim sUserName = txtUsername.Text
            Dim sFirstname = txtFirstName.Text
            Dim sLastName = txtLastName.Text
            Dim sPassword = txtPassword.Text
            Dim sGroupname = cbGroupAdd.SelectedValue
            Dim sEmail = txtAddEmail.Text
            If ((txtPassword.Text) = (txtConfirmPassword.Text)) Then
                ADWrapper.CreateAdAccount(sUserName, sPassword, sFirstname, sLastName, sGroupname, sEmail)
                MessageBox.Show(txtFirstName.Text & " " & txtLastName.Text & " has been created and added to " & sGroupname, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Passwords do not match")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Open in new window


Any help  is appreciated
0
r3nder
Asked:
r3nder
  • 5
  • 4
  • 2
  • +1
1 Solution
 
HaiFaiCommented:
Hi

In your example code there is no loop or anything where you can use progressbar.
You probably need to add progressbar under ADWrapper.CreateAdAccount
and there you need to find items max values and min values and just add counter to increase value of progressbar
0
 
sachinpatil10dCommented:
Try this to use simple ProgressBar

 Private Sub btnAddUser_Click(sender As System.Object, e As System.EventArgs) Handles btnAddUser.Click
        Try
            btnAddUser.Enabled = False
            Dim sUserName = txtUsername.Text
            Dim sFirstname = txtFirstName.Text
            Dim sLastName = txtLastName.Text
            Dim sPassword = txtPassword.Text
            Dim sGroupname = cbGroupAdd.SelectedValue
            Dim sEmail = txtAddEmail.Text
            ProgressBar1.Value += 5
            If ((txtPassword.Text) = (txtConfirmPassword.Text)) Then
                ProgressBar1.Value += 5
                ADWrapper.CreateAdAccount(sUserName, sPassword, sFirstname, sLastName, sGroupname, sEmail)
                While ProgressBar1.Value < 100
                    ProgressBar1.Value += 5
                End While
                MessageBox.Show(txtFirstName.Text & " " & txtLastName.Text & " has been created and added to " & sGroupname, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                ProgressBar1.Value = 0
                MessageBox.Show("Passwords do not match")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Open in new window


refer this link to use BackgroundWorker class

http://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners
0
 
r3nderAuthor Commented:
It thows an exception on active directory - good try though, any other ideas? I am out of them
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
sachinpatil10dCommented:
can you give the error details.
0
 
CodeCruiserCommented:
  While ProgressBar1.Value < 100
                    ProgressBar1.Value += 5
                End While


Great code @sachinpatil10d



@r3nder
I bet almost all of the time is spent on ADWrapper.CreateAdAccount line so you can not use a progressbar here. What you can do is show marquee style progressbar and execute the above code either in a separate thread or in a backgroundworker.
0
 
r3nderAuthor Commented:
How would I call a thread for the ADWrapper.CreateAdAccount(sUserName, sPassword, sFirstname, sLastName, sGroupname, sEmail)?
0
 
HaiFaiCommented:
ok if you really want progressbar and you dont have any values send to progressbar you can do it like this, but its not the rigth way.
You need to add timer and progressbar to code to get this work


  Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            ProgressBar1.Value = 1
        Else
            ProgressBar1.Value += 1
        End If
    End Sub


    Private Sub btnAddUser_Click(sender As System.Object, e As System.EventArgs) Handles btnAddUser.Click
        Try
            ProgressBar1.Minimum = 1
            ProgressBar1.Maximum = 100
            Timer1.Interval = 100
            Timer1.Enabled = True
            btnAddUser.Enabled = False
            Dim sUserName = txtUsername.Text
            Dim sFirstname = txtFirstName.Text
            Dim sLastName = txtLastName.Text
            Dim sPassword = txtPassword.Text
            Dim sGroupname = cbGroupAdd.SelectedValue
            Dim sEmail = txtAddEmail.Text
            If ((txtPassword.Text) = (txtConfirmPassword.Text)) Then
                ADWrapper.CreateAdAccount(sUserName, sPassword, sFirstname, sLastName, sGroupname, sEmail)
                MessageBox.Show(txtFirstName.Text & " " & txtLastName.Text & " has been created and added to " & sGroupname, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Passwords do not match")
            End If
            Timer1.Enabled = False
            ProgressBar1.Value = 1
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Open in new window

0
 
CodeCruiserCommented:
Put those values into a structure at class level or leave variables at class level and access in thread or backgroundworker.
0
 
r3nderAuthor Commented:
Ok thanks for the advice but origional question how do I access it in a seperate thread
here is the funcion for ADWrapper.CreateAdAccount()

       Public Shared Sub CreateAdAccount(ByVal sUserName As String, ByVal sPassword As String, ByVal sFirstName As String, ByVal sLastName As String, ByVal sGroupName As String, ByVal sEmail As String, ByVal sTelephoneNumber As String, ByVal sOffice As String, ByVal sDisplayName As String)
            Dim dirEntry As New DirectoryEntry(ADFullPath, ADAdminUser, ADAdminPassword)
            ' 1. Create user account
            Dim adUsers As DirectoryEntries = dirEntry.Children
            'Dim adUsers As DirectoryEntries = dirEntry.Container
            Dim newUser As DirectoryEntry = adUsers.Add("CN=" & sUserName, "user")
            ' 2. Set properties
            SetProperty(newUser, "givenname", sFirstName)
            SetProperty(newUser, "sn", sLastName)
            SetProperty(newUser, "SAMAccountName", sUserName)
            SetProperty(newUser, "userPrincipalName", sUserName)
            SetProperty(newUser, "mail", sEmail)
            SetProperty(newUser, "telephoneNumber", sTelephoneNumber)
            SetProperty(newUser, "physicalDeliveryOfficeName", sOffice)
            SetProperty(newUser, "displayName", sDisplayName)

            newUser.CommitChanges()
            ' 3. Set the password
            SetPassword(newUser, sPassword)
            ' 5. Add the user to the specified group
            AddUserToGroup(dirEntry, newUser, sGroupName)
            ' 6. Enable the account
            EnableAccount(newUser)
            ' 7. Close & clean-up
            newUser.Close()
            dirEntry.Close()
        End Sub

Open in new window


Can I add the progess bar here and access it in the form and if so how? I am new to this
Thanks for your help
0
 
CodeCruiserCommented:
Progressbar is useful in a scenario where you perform multiple operations and each one takes similar amount of time. For example deleting a file. In this scenario, only one line (such as commitchanges) may be taking majority of time so unless you want to frustrate your users (going to 99% quickly and then getting stuck there), I would advise to use a marquee.

Put those parameters in class variables and move the code into a BackgroundWorker.DoWork method.
0
 
r3nderAuthor Commented:
show me a simple way to do the background worker and implement what you said and I will stop bugging you :)
P.S. thanks for everything
0
 
CodeCruiserCommented:
Drag and drop a backgroundworker on form. In code window, use the drop downs near the toolbar to add its DoWork handler. Move your code in that handler. When you need to run the code, call the RunWorkerAsync on bgworker.

http://www.java2s.com/Code/VB/GUI/BackgroundWorkerDemo.htm
0
 
r3nderAuthor Commented:
Thanks CodeCruiser
It works great
R3nder
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now