Results 1 to 36 of 36

Thread: Loading progress Circle?

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Loading progress Circle?

    Afternoon,

    searched the web but cannot find any type of code that will allow me to implement a loading circle. Can someone please post me some code that can achieve this?

    or can vb2008 allow me to change the look of the original proress bar?

    ta

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Loading progress Circle?


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    thanks for the link but isnt that code in C#?

  4. #4
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Loading progress Circle?

    The link Kevin posted is really helpful even if it is in C#, I've previously created my own circular progress bar within VB.NET but not even considered the method he used.

    My advice rafter would be to have a go at creating the control yourself, that way you become the artist and you can make changes in anyway you like. You can use not only the method mentioned in the post, but also if you didn't know, the syntax of C# is easily converted if it's not already the same.

    If you have any problems, post here and I'll have a look for you.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    Quote Originally Posted by OhGreen View Post
    The link Kevin posted is really helpful even if it is in C#, I've previously created my own circular progress bar within VB.NET but not even considered the method he used.

    My advice rafter would be to have a go at creating the control yourself, that way you become the artist and you can make changes in anyway you like. You can use not only the method mentioned in the post, but also if you didn't know, the syntax of C# is easily converted if it's not already the same.

    If you have any problems, post here and I'll have a look for you.
    thanks for the advice, ive tried converting it to vb.net but its way above me..

    i need to familiarize myself firstly with vb.net before anything else.

    can i have a look at the vb.net code that you did for the progress bar?

    ta

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,601

    Re: Loading progress Circle?

    Quote Originally Posted by rafter_01 View Post
    thanks for the advice, ive tried converting it to vb.net but its way above me..

    i need to familiarize myself firstly with vb.net before anything else.

    can i have a look at the vb.net code that you did for the progress bar?

    ta
    Why not simply download the one from the link and use that ?

  7. #7
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Loading progress Circle?

    I don't personally like gifting answers, I feel that you don't learn anything that way.

    Method:

    The idea is to draw two imaginary circles, one smaller than the other and placed in the middle of the larger.

    You then, draw a line from a point on the smaller circle to a point on the larger circle.

    Using the tick event of a timer, you can keep painting the next line on the next two points.

    For now that is your aim.

    To do that, take some baby steps:

    - Do some googling, learn how to paint a line on a form. Once you've done it, play around with changing it's colour, location etc until you're confident.

    - Make sure you know how to use a timer to move that line and draw it somewhere else. To do this, make some integer variables and use them as your x and y locations for the line, then on timer tick add 10 to the variables and redraw.

    You may not like the fact I'm not doing the work for you, but you'll learn a lot by trying the above.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    Quote Originally Posted by OhGreen View Post
    I don't personally like gifting answers, I feel that you don't learn anything that way.

    Method:

    The idea is to draw two imaginary circles, one smaller than the other and placed in the middle of the larger.

    You then, draw a line from a point on the smaller circle to a point on the larger circle.

    Using the tick event of a timer, you can keep painting the next line on the next two points.

    For now that is your aim.

    To do that, take some baby steps:

    - Do some googling, learn how to paint a line on a form. Once you've done it, play around with changing it's colour, location etc until you're confident.

    - Make sure you know how to use a timer to move that line and draw it somewhere else. To do this, make some integer variables and use them as your x and y locations for the line, then on timer tick add 10 to the variables and redraw.

    You may not like the fact I'm not doing the work for you, but you'll learn a lot by trying the above.
    "You may not like the fact I'm not doing the work for you", dont worry about it.. ill see what i can do.. but thanks for your advice

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    Quote Originally Posted by OhGreen View Post
    I don't personally like gifting answers, I feel that you don't learn anything that way.

    Method:

    The idea is to draw two imaginary circles, one smaller than the other and placed in the middle of the larger.

    You then, draw a line from a point on the smaller circle to a point on the larger circle.

    Using the tick event of a timer, you can keep painting the next line on the next two points.

    For now that is your aim.

    To do that, take some baby steps:

    - Do some googling, learn how to paint a line on a form. Once you've done it, play around with changing it's colour, location etc until you're confident.

    - Make sure you know how to use a timer to move that line and draw it somewhere else. To do this, make some integer variables and use them as your x and y locations for the line, then on timer tick add 10 to the variables and redraw.

    You may not like the fact I'm not doing the work for you, but you'll learn a lot by trying the above.
    so, does the code that i right for drawing two circles go into a class that is attached to the project?

    so if when i start by drawing a line on the form, will all my drawing code go in a class or directly in my form code?
    Last edited by rafter_01; Feb 22nd, 2012 at 06:55 AM. Reason: missed q

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Loading progress Circle?

    I would think that the most logical thing to do would be to create a custom control. You could inherit the PictureBox for example, which is already optimised for graphics, and override its OnPaint method to do the drawing. You then have all the logic encapsulated in a control that you can simply add to a form from the Toolbox, just like any other control. You could add logic to set the speed of the animation if you wanted and various other things like that.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    Quote Originally Posted by jmcilhinney View Post
    I would think that the most logical thing to do would be to create a custom control. You could inherit the PictureBox for example, which is already optimised for graphics, and override its OnPaint method to do the drawing. You then have all the logic encapsulated in a control that you can simply add to a form from the Toolbox, just like any other control. You could add logic to set the speed of the animation if you wanted and various other things like that.
    thanks for this but me bieng new to vb, would my override code go into a seperate class ?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Loading progress Circle?

    It would go in the custom control, which inherits PictureBox. You can't override a member of a class anywhere other than inside that class.

  13. #13
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Loading progress Circle?

    I would've thought creating a custom control would be a bit of a stretch for someone new to VB?

    That's the only reason I suggested a more primitive "draw it yourself" technique.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Loading progress Circle?

    It may seem like an advanced concept but the code you put in an overridden OnPaint method will be the same as the code you put in a Paint event handler, so you get a more flexible outcome from basically the same work.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    Quote Originally Posted by jmcilhinney View Post
    It may seem like an advanced concept but the code you put in an overridden OnPaint method will be the same as the code you put in a Paint event handler, so you get a more flexible outcome from basically the same work.
    guys im confused

    whats the difference between overriden OnPaint Method and paint event handler?

  16. #16
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Loading progress Circle?

    If you have time to create your own control as jmc recommended this would be best as you have full control over it. If instead time is important then I would use the control at Code Project. Simply compile the project which creates CircularProgressbarControl.dll and then reference this in your project, there is no need to convert this control to VB.NET as all we care about is in the end the control does what you want.

  17. #17
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Loading progress Circle?

    Quote Originally Posted by rafter_01 View Post
    guys im confused

    whats the difference between overriden OnPaint Method and paint event handler?
    The OnPaint method is called when a control needs painting. This method will raise the Paint event.

    So, you can either handle the Paint event, by effectively adding an event handler, or you can override the OnPaint event. In the latter case, *your* method will be called when the control needs painting.

    If you are performing painting/drawing within the control, you will ordinarily override the OnPaint event - this will leave the Paint method to be handled exclusively by the consumer of the control (e.g. the form that the control is hosted on).

    While handling the Paint event within the control *can* work, if the consumer also handles the paint event, it can lead to unpredictable behavior (multiple consumers of an event) since the order in which the methods are called that handle the event is not strictly defined. By overriding the OnPaint event you are ensuring that your drawing in that method is performed before (or after) the event handlers. You will raise the Paint method by calling the base OnPaint method that you overrode.

    The short answer is that you will always use the (overridden) OnPaint method when drawing within your own control.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  18. #18
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Loading progress Circle?

    Hi rafter, it wouldn't surprise me if you are suffering from information overload. I agree with OGreen, start simple. I suggest you break it down into three stages.

    Stage 1. Take an image like this Name:  RotaryIndicator.png
Views: 11620
Size:  2.1 KB (right click to save) and get it rotating in a picture box on a form. I'll give you an example of the code below.

    Stage 2. Generate the image yourself in code, so that you can change values like the color, the number of spokes, the line thickness etc.

    Stage 3. If you want to use the progress indicator in other projects, you could consider turning it into a custom control. That's not too hard in theory -- a lot of the code will be the same as in stage1/stage2. But in practice it can be a lot of work especially if you have a lot of properties you want to expose in the designer. And there are pitfalls.

    Here's what I suggest for Stage 1. Open a form, and add a picture box(PictureBox1, Size about 50x50 pixels, BackColor = Transparent. Leave other properties at default.), a Button (Button1) and a Timer (Timer1, Interval = 100). Try the following code to start and stop rotation:
    vb Code:
    1. Private rotaryImage As Image = Image.FromFile(_image file path+name _)
    2.     Private spokeCount As Integer = 14
    3.     Private angle As Single = 0
    4.  
    5.     Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    6.         Dim center As New PointF(PictureBox1.Width / 2.0F, PictureBox1.Width / 2.0F)
    7.         Dim drawLocation = New PointF(center.X - rotaryImage.Width / 2.0F, center.Y - rotaryImage.Height / 2.0F)
    8.         e.Graphics.TranslateTransform(center.X, center.Y)
    9.         e.Graphics.RotateTransform(angle)
    10.         e.Graphics.TranslateTransform(-center.X, -center.Y)
    11.         e.Graphics.DrawImage(rotaryImage, drawLocation)
    12.     End Sub
    13.  
    14.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    15.         angle += 360.0F / spokeCount
    16.         PictureBox1.Invalidate()
    17.     End Sub
    18.  
    19.     Private Sub StartRotaryIndicator()
    20.         Timer1.Start()
    21.     End Sub
    22.  
    23.     Private Sub StopRotaryIndicator()
    24.         Timer1.Stop()
    25.     End Sub
    26.  
    27.     Private Sub ResetRotaryIndicator()
    28.         angle = 0
    29.     End Sub
    30.  
    31.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    32.         If Timer1.Enabled = False Then
    33.             ResetRotaryIndicator()
    34.             StartRotaryIndicator()
    35.             PictureBox1.Show()
    36.         Else
    37.             StopRotaryIndicator()
    38.             PictureBox1.Hide()
    39.         End If
    40.     End Sub
    A few notes:
    1. SpokeCount = 14 is the number of spokes in this particular image.
    2. The only complicated part is in the Paint event. You have to shift the center of rotation from the origin to the actual center (line 8), then do the rotation (line 9), and then shift it back again (line 10).
    3. The control is transparent, but only to its Parent control - normally the form. If you want to show it on a button, for example, you need to set PictureBox1.Parent = button and adjust the Location accordingly. This limitation applies to all transparency in Windows Forms controls.

    BB

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    Quote Originally Posted by boops boops View Post
    Hi rafter, it wouldn't surprise me if you are suffering from information overload. I agree with OGreen, start simple. I suggest you break it down into three stages.

    Stage 1. Take an image like this Name:  RotaryIndicator.png
Views: 11620
Size:  2.1 KB (right click to save) and get it rotating in a picture box on a form. I'll give you an example of the code below.

    Stage 2. Generate the image yourself in code, so that you can change values like the color, the number of spokes, the line thickness etc.

    Stage 3. If you want to use the progress indicator in other projects, you could consider turning it into a custom control. That's not too hard in theory -- a lot of the code will be the same as in stage1/stage2. But in practice it can be a lot of work especially if you have a lot of properties you want to expose in the designer. And there are pitfalls.

    Here's what I suggest for Stage 1. Open a form, and add a picture box(PictureBox1, Size about 50x50 pixels, BackColor = Transparent. Leave other properties at default.), a Button (Button1) and a Timer (Timer1, Interval = 100). Try the following code to start and stop rotation:
    vb Code:
    1. Private rotaryImage As Image = Image.FromFile(_image file path+name _)
    2.     Private spokeCount As Integer = 14
    3.     Private angle As Single = 0
    4.  
    5.     Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    6.         Dim center As New PointF(PictureBox1.Width / 2.0F, PictureBox1.Width / 2.0F)
    7.         Dim drawLocation = New PointF(center.X - rotaryImage.Width / 2.0F, center.Y - rotaryImage.Height / 2.0F)
    8.         e.Graphics.TranslateTransform(center.X, center.Y)
    9.         e.Graphics.RotateTransform(angle)
    10.         e.Graphics.TranslateTransform(-center.X, -center.Y)
    11.         e.Graphics.DrawImage(rotaryImage, drawLocation)
    12.     End Sub
    13.  
    14.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    15.         angle += 360.0F / spokeCount
    16.         PictureBox1.Invalidate()
    17.     End Sub
    18.  
    19.     Private Sub StartRotaryIndicator()
    20.         Timer1.Start()
    21.     End Sub
    22.  
    23.     Private Sub StopRotaryIndicator()
    24.         Timer1.Stop()
    25.     End Sub
    26.  
    27.     Private Sub ResetRotaryIndicator()
    28.         angle = 0
    29.     End Sub
    30.  
    31.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    32.         If Timer1.Enabled = False Then
    33.             ResetRotaryIndicator()
    34.             StartRotaryIndicator()
    35.             PictureBox1.Show()
    36.         Else
    37.             StopRotaryIndicator()
    38.             PictureBox1.Hide()
    39.         End If
    40.     End Sub
    A few notes:
    1. SpokeCount = 14 is the number of spokes in this particular image.
    2. The only complicated part is in the Paint event. You have to shift the center of rotation from the origin to the actual center (line 8), then do the rotation (line 9), and then shift it back again (line 10).
    3. The control is transparent, but only to its Parent control - normally the form. If you want to show it on a button, for example, you need to set PictureBox1.Parent = button and adjust the Location accordingly. This limitation applies to all transparency in Windows Forms controls.

    BB
    many many thanks for this,

    by the way how do i stop the image from bouncing? i just want it to rotate.

    now i must learn how to turn it into a custom control...

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    Re: Loading progress Circle?

    Quote Originally Posted by boops boops View Post
    Hi rafter, it wouldn't surprise me if you are suffering from information overload. I agree with OGreen, start simple. I suggest you break it down into three stages.

    Stage 1. Take an image like this Name:  RotaryIndicator.png
Views: 11620
Size:  2.1 KB (right click to save) and get it rotating in a picture box on a form. I'll give you an example of the code below.

    Stage 2. Generate the image yourself in code, so that you can change values like the color, the number of spokes, the line thickness etc.

    Stage 3. If you want to use the progress indicator in other projects, you could consider turning it into a custom control. That's not too hard in theory -- a lot of the code will be the same as in stage1/stage2. But in practice it can be a lot of work especially if you have a lot of properties you want to expose in the designer. And there are pitfalls.

    Here's what I suggest for Stage 1. Open a form, and add a picture box(PictureBox1, Size about 50x50 pixels, BackColor = Transparent. Leave other properties at default.), a Button (Button1) and a Timer (Timer1, Interval = 100). Try the following code to start and stop rotation:
    vb Code:
    1. Private rotaryImage As Image = Image.FromFile(_image file path+name _)
    2.     Private spokeCount As Integer = 14
    3.     Private angle As Single = 0
    4.  
    5.     Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    6.         Dim center As New PointF(PictureBox1.Width / 2.0F, PictureBox1.Width / 2.0F)
    7.         Dim drawLocation = New PointF(center.X - rotaryImage.Width / 2.0F, center.Y - rotaryImage.Height / 2.0F)
    8.         e.Graphics.TranslateTransform(center.X, center.Y)
    9.         e.Graphics.RotateTransform(angle)
    10.         e.Graphics.TranslateTransform(-center.X, -center.Y)
    11.         e.Graphics.DrawImage(rotaryImage, drawLocation)
    12.     End Sub
    13.  
    14.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    15.         angle += 360.0F / spokeCount
    16.         PictureBox1.Invalidate()
    17.     End Sub
    18.  
    19.     Private Sub StartRotaryIndicator()
    20.         Timer1.Start()
    21.     End Sub
    22.  
    23.     Private Sub StopRotaryIndicator()
    24.         Timer1.Stop()
    25.     End Sub
    26.  
    27.     Private Sub ResetRotaryIndicator()
    28.         angle = 0
    29.     End Sub
    30.  
    31.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    32.         If Timer1.Enabled = False Then
    33.             ResetRotaryIndicator()
    34.             StartRotaryIndicator()
    35.             PictureBox1.Show()
    36.         Else
    37.             StopRotaryIndicator()
    38.             PictureBox1.Hide()
    39.         End If
    40.     End Sub
    A few notes:
    1. SpokeCount = 14 is the number of spokes in this particular image.
    2. The only complicated part is in the Paint event. You have to shift the center of rotation from the origin to the actual center (line 8), then do the rotation (line 9), and then shift it back again (line 10).
    3. The control is transparent, but only to its Parent control - normally the form. If you want to show it on a button, for example, you need to set PictureBox1.Parent = button and adjust the Location accordingly. This limitation applies to all transparency in Windows Forms controls.

    BB
    btw at stage 2, you mentioned changing the color, thickness etc, how would i implement this?

  21. #21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width