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
LVL 6
r3nderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r3nderAuthor Commented:
Thanks CodeCruiser
It works great
R3nder
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.