Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a panel which i draw something..
This is a part of my code(You dont need to read all code, i just wanted to show how it looks like):
Dim g As Graphics = msjscrlist.Item(sender.tag).Controls("Powner").Controls("P").CreateGraphics
for i=0 to ......
code...
for k=0 to ......
code...
msjcount += 1
linecount = 1
If g.MeasureString(ben, New Font("tahoma", 8)).Width > msjscrlist.Item(sender.tag).Controls("Powner").Controls("P").Width - 10 Then
  linecount = Math.Ceiling(g.MeasureString(ben, New Font("tahoma", 8)).Width / msjscrlist.Item(sender.tag).Controls("Powner").Controls("P").Width - 10)
End If
If itsme = True Then
  g.FillRectangle(New SolidBrush(Color.Green), msjscrlist.Item(sender.tag).Width - g.MeasureString(ben, New Font("tahoma", 8)).Width - 20, sonalt + 8, g.MeasureString(ben, New Font("tahoma", 8)).Width + 20, linecount * 28)
  g.DrawString(ben, New Font("Tahoma", 8), Brushes.White, New Point(msjscrlist.Item(sender.tag).Width - g.MeasureString(ben, New Font("tahoma", 8)).Width - 10, sonalt + 16))
  sonalt += 8 + linecount * 28
  ben = Nothing
Else
  g.FillRectangle(New SolidBrush(Color.Teal), 0, sonalt + 8, g.MeasureString(hedef, New Font("tahoma", 8)).Width + 20, linecount * 28)
  g.DrawString(hedef, New Font("Tahoma", 8), Brushes.White, New Point(10, sonalt + 16))
  sonalt += 8 + linecount * 28
  hedef = Nothing
End If
next
code...
next


The problem is; graphics that i draw are disappearing when i scroll the panel, how can i make those graphics persistent? People say: do drawing in 'onpaint' event. But i have a long code, wouldn't it be slow?
Posted

1 solution

"But i have a long code, wouldn't it be slow?"
Well - it's your choice: If you want the graphics you draw to remain visible, then you have to draw them in a persistent way - and that means that OnPaint needs to be involved.

But...there is something else you could do. If your graphics are unchanging (or change infrequently) then instead of drawing in the Paint event (and the time it takes to do that each time) you could draw onto a Image instead, and display that in the Panel (possibly via a PictureBox control). That way, the drawing code is executed once, and the Paint event just draws the appropriate part of the image.
 
Share this answer
 
Comments
MuhsinFatih 31-Dec-13 8:54am    
Thanks, but there's a problem;
I did it the way you told me, using a bitmap. But in my code, the bitmap's height gets bigger each step of those loops(reads users messages from sql server), and I have to use g.measurestring() function to get lenght of each message(if message's width is bigger than panel's width, it adds one more line which means more height). The messages can be more than a hundred. Do I have to calculate the lenght of all messages before drawing? Or can I extend the bitmap anyway?
(Sory for late answer)
OriginalGriff 31-Dec-13 9:00am    
This may sound like a silly question, but...
Why are you drawing the strings anyway? If all you are doing is reading strings from a database and displaying them with different background colours, why draw them at all? Can't you just use something text based, like a DataGridView or similar?
MuhsinFatih 1-Jan-14 6:31am    
I know that would be much easier, i can add labels programmatically, but in VB.Net, a label has a lot of value even you don't use them. If I use labels, for each message string from sql, it will use a lot of strings in memory(tell if i'm wrong). So, I want to use something that wont make problem when hundreds of messages come.
OriginalGriff 1-Jan-14 6:42am    
And you think that Images will be smaller? :OMG:
Strings are trivial sizes:
"hello" occupies 5 bytes (ok, ok, actually more than that, since it's a reference, and there is heap related space and control to worry about as well.
But as an image, 25 pixels high, its 4 bytes per pixel and about 25x25x5 pixels (plus the heap space) so call it around 2500 times as much space as the string version! (And strings are a lot, lot quicker to play with as the system is well and truly optimised to deal with displaying them)

Seriously: strings are small, faster and a lot more flexible.
Yes, individual labels could use a large number of controls, (but how many are you planning on showing your user at once anyway?) and there are much better ways to do it: Look at a DataGridView for example, and it can pretty much even use the data direct from the DB...
MuhsinFatih 1-Jan-14 9:38am    
When you add a label, it does a draw function as every visible object. Bitmap was not my idea, I asked for best option, what i wonder is: Why labels' graphics are persistent, and being drawed fast, but graphics we draw are not?
Just now, an idea came to my mind. I'll define a list of string, and list of integer
I'll save each messages info in those lists, and use draw function. Or I'll add labels
Which one will use less memory and CPU? Or if you have a better idea, tell me please

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