Avatar of r3nder
r3nderFlag for United States of America asked on

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
Microsoft ApplicationsVisual Basic.NET.NET Programming

Avatar of undefined
Last Comment
r3nder

8/22/2022 - Mon
HaiFai

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
sachinpatil10d

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
ASKER
r3nder

It thows an exception on active directory - good try though, any other ideas? I am out of them
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
sachinpatil10d

can you give the error details.
Nasir Razzaq

  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.
ASKER
r3nder

How would I call a thread for the ADWrapper.CreateAdAccount(sUserName, sPassword, sFirstname, sLastName, sGroupname, sEmail)?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
HaiFai

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

Nasir Razzaq

Put those values into a structure at class level or leave variables at class level and access in thread or backgroundworker.
ASKER
r3nder

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Nasir Razzaq

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.
ASKER
r3nder

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
ASKER CERTIFIED SOLUTION
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
r3nder

Thanks CodeCruiser
It works great
R3nder
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.