Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I already create 2 panel in Form, which Panel1 is set to Auto Scroll. Panel2 is inside Panel1 and contain a Fill Rectangle which is set with Timer so it looks like a progress bar. The problem is, when i scroll Panel1 to right, yes.. it scrolled to right but when I scroll it back to left, the color in left side is back to normal alias to its default color. Then how to preserve this color on the left side when I scroll it to right?

Any response, Thanks :)
VB
'This code is inside Form1_Load
Grafik = Panel2.CreateGraphics
Timer1.Start()

'This code is inside Timer1_Tick
time += 10
If time <= 100 Then
Grafik.FillRectangle(Brushes.BlueViolet, 0, 0, time, 25)
ElseIf time <= 200 Then
Grafik.FillRectangle(Brushes.Brown, 100, 0, time - 100, 25)
ElseIf time <= 300 Then
Grafik.FillRectangle(Brushes.DarkOrange, 200, 0, time - 200, 25)
ElseIf time <= 400 Then
Grafik.FillRectangle(Brushes.DarkSalmon, 300, 0, time - 300, 25)
ElseIf time <= 500 Then
Grafik.FillRectangle(Brushes.DarkSlateGray, 400, 0, time - 400, 25)
End If


http://s15.postimg.org/ix9q15rzv/cap.png[^]

When it scroll to right then scroll back to left, it should be violet color, but it is default like gray..
Posted
Updated 18-Jun-15 4:19am
v3

You cannot "contain a FillRectangle"; this is not a stationary object. You can render rectangle using System.Drawing.Graphics.FillRectangle, but it happens in response to Paint event.

First, scrolling. For scrolling, you can have your control where you render graphics inside some scrollable control, such as Panel, and scroll that panel. Panel is ScrollableControl, so please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol%28v=vs.110%29.aspx[^].

Inner control should render what you want by overriding OnPaint method or handling the Paint event (then you could also use Panel, but OnPaint would require a derived class). But your graphics is not static, it is animated, or is changed in any way.

The idea is: you need to have some data model which describe your graphics, in your case, some data set describing current rectangle color/size/location. You rendering method should not use constant values (using hard-coded immediate constants is bad style anyway, really bad for maintenance of your project anyway), but values from the model. If you change the values and call Invalidate method, it will trigger re-drawing the graphics in the invalidated control. You need to understand how it works. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
Zoom image in C# .net mouse wheel[^].

Then you can create a separate thread which implements the scenario of the animated sequence. It could be anything, such as rendering "physics" of some game. The scenario in a separate tread is much easier to implement than timers, because it is sequential, logically independent from your UI. There are specific problems with timers you don't want to have. I don't want to waste more time for explanation; just don't use timers. My past answer explains such animation:
How to speed up my vb.net application?[^].

Your thread only changes the data, and the triggers re-drawing by calling Control.Invalidate. But you cannot call this method directly in a non UI thread (the same goes for timers, excluding the timer System.Windows.Forms.Timer which is totally unsuitable for animation due to its prohibitively bad accuracy). It is also explained in my past answer referenced above. You need to use Control.Invoke to delegate the Control.Invalidate call to the UI thread. See also my past answers:
Problem with Treeview Scanner And MD5[^],
Control.Invoke() vs. Control.BeginInvoke()[^].

That's all.

—SA
 
Share this answer
 
You problem is that you paint on the Panel only once. Once you scroll the Panel over, everything you painted is lost. You have to do the painting of the Panel in the Panels Paint event. Every time the Panel is scrolled or another window is passed over your panel you will lose everything you painted and have to repaint it. Windows will tell you when you have to repaint by raising your Panels Paint event.
 
Share this answer
 
I don't understand completly what you want to do ... but I would do the following :

You create your own Control.
This Control inherits from Panel (if you wish - I would take Control).
Inside this Control you integrate the Timer.
Inside this Control you override the OnPaint-method. This method gives you a Graphics-Object for the FillRectangle and also this method is re-called by each invalidating of your control.
With your Timer you force an Invalidate for your control.

I hope, this helps you - else response ...
 
Share this answer
 
Comments
rudy-peto 18-Jun-15 10:16am    
Thanks for response.. Okay, I upload an image above. Maybe you can take a look to what I mean.
Ralf Meier 18-Jun-15 10:26am    
What happens if time exceeds the value of 500 ?
How do you realize (or wish to realize) the "scrolling to back" ?
rudy-peto 18-Jun-15 10:31am    
When the time exceeds the value of 500, well..nothing happen.. 'time' just as a counter.. I plan to make the timer stop. But my problem is when scroll back, the color that should be violet, it changes to gray alias default..
Sergey Alexandrovich Kryukov 18-Jun-15 10:36am    
Using the timer is a really bad idea here. Please see Solution 2.
—SA
Ralf Meier 18-Jun-15 10:43am    
Solution 2 and also Solution 3 are just the same as I suggested in my Solution ... only with more words around ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900