Solved

Progress Bar

Posted on 2012-03-15
13
475 Views
Last Modified: 2012-03-16
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
Comment
Question by:r3nder
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 7

Expert Comment

by:HaiFai
ID: 37723648
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
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37723666
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
 
LVL 6

Author Comment

by:r3nder
ID: 37723703
It thows an exception on active directory - good try though, any other ideas? I am out of them
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37723995
can you give the error details.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37724239
  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
 
LVL 6

Author Comment

by:r3nder
ID: 37727921
How would I call a thread for the ADWrapper.CreateAdAccount(sUserName, sPassword, sFirstname, sLastName, sGroupname, sEmail)?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 7

Expert Comment

by:HaiFai
ID: 37729036
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37730322
Put those values into a structure at class level or leave variables at class level and access in thread or backgroundworker.
0
 
LVL 6

Author Comment

by:r3nder
ID: 37730452
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37730635
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
 
LVL 6

Author Comment

by:r3nder
ID: 37730720
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37731451
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
 
LVL 6

Author Closing Comment

by:r3nder
ID: 37731857
Thanks CodeCruiser
It works great
R3nder
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now