By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Label painting issue when form moved offscreen

100+
P: 116
Hi

I have a label which has text that changes dependent on user input using the paint command. Say it says 'Hello World'.

When I drag the form around such that any part of the label goes offscreen, when the label returns to the screen it wants to constantly repaint itself as it returns meaning that, dependent on how fast you drag it back, the label can read 'He He He He Hello World' or 'HHHHHHello World' etc

Is this a known issue and is there a way to prevent it?

The label is simply painted using

Expand|Select|Wrap|Line Numbers
  1. Private Sub MyLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyLabel.Paint
  2.  
In order to get it to change appearance it is invalidated when the user clicks a button to tell it to change to some new value. I'm assuming though that there's some core VB process within the painteventargs that triggers when the item gets back to the visible screen that is the root cause of this as all other labels return to the screen just fine.

Thanks!
Apr 27 '13 #1

✓ answered by Rabbit

What if in your Me_Move event you just invalidated the label?

Share this Question
Share on Google+
12 Replies


Rabbit
Expert Mod 10K+
P: 12,349
It would help to see the code.
Apr 27 '13 #2

100+
P: 116
Here is some code. It's pretty basic.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit On
  2. Imports System.ComponentModel
  3. Imports System.Runtime.InteropServices
  4. Imports System.Text
  5. Imports System.Windows.Forms
  6. Imports System
  7. Imports System.IO
  8. Imports System.Collections
  9. Imports System.Threading
  10.  
  11. Public Class Form1
  12.  
  13. Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
  14.         MyLabel.Invalidate()
  15. End Sub
  16.  
  17. Private Sub MyLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyLabel.Paint
  18.         Dim upperfont As Font = New Font("Arial", 10, FontStyle.Bold)
  19.         Dim lowerfont As Font = New Font("Arial", 8, FontStyle.Regular)
  20.         Dim upperbrush As SolidBrush = New SolidBrush(Color.Black)
  21.         Dim lowerbrush As SolidBrush = New SolidBrush(Color.FromArgb(17, 17, 17))
  22.         Dim upperstr As String = myName.Text
  23.         Dim lowerstr As String = myOccupation.Text
  24.         Dim uppersize As SizeF = e.Graphics.MeasureString(upperstr, upperfont, e.ClipRectangle.Size)
  25.         Dim lowersize As SizeF = e.Graphics.MeasureString(lowerstr, lowerfont, e.ClipRectangle.Size)
  26.         e.Graphics.DrawString(upperstr, upperfont, upperbrush, e.ClipRectangle.X + 3 + ((e.ClipRectangle.Width - uppersize.Width) / 2), e.ClipRectangle.Location.Y + 19)
  27.         e.Graphics.DrawString(lowerstr, lowerfont, lowerbrush, e.ClipRectangle.X + ((e.ClipRectangle.Width - lowersize.Width) / 2), e.ClipRectangle.Location.Y + 34)
  28. End Sub
  29.  
  30. End Class
  31.  
Apr 27 '13 #3

Rabbit
Expert Mod 10K+
P: 12,349
Did you try clearing it before redrawing?
Apr 28 '13 #4

100+
P: 116
Hi Rabbit, I'm learning VB on the job so not sure what you mean by clearing before redrawing. I thought the Invalidate() triggered some kind of clear/redraw? It seems to be redrawing every time the form is moved and fits the label painting to whatever part of the label is showing at the time.
Apr 29 '13 #5

Rabbit
Expert Mod 10K+
P: 12,349
But you're only invalidating once. If it's redrawing, you should try invalidating each time. Probably in the dragdrop event.
Apr 29 '13 #6

100+
P: 116
Ah ok. Let me investigate the dragdrop - been away for a few days. Thanks Rabbit.
May 3 '13 #7

100+
P: 116
Not sure Dragdrop is the solution - am looking at trying to determine if the form is being moved but apparently there's no way to do that.

The issue is that this is a static label - it cant be moved within the form. It displays perfectly when you move the form all over the screen. It fails when part of the label goes off the screen edge as I assume that when the form realises that a part that was not visible is now visible again it 'paints' the label. It's that event that I need to kill.

Still checking...
May 3 '13 #8

Rabbit
Expert Mod 10K+
P: 12,349
You can either subclass the form and look for the WM_MOVE message or use a timer to see if the form has changed location.
May 3 '13 #9

100+
P: 116
Thanks Rabbit. Might take a look at this later.

At the moment I've added a check named offScreen. When the form Move event is being triggered it checks to see if any part of the form is likely to be outside the screen and, if so, offScreen blocks the painting. It repaints when the form is fully on the screen again.

It's ugly and when label is moved off left side of screen the label is missing text til completely back but this is such a minor part of the application it will do. Was hoping for a simple 'don't repaint when label moving back onscreen' function but not available :)

This isn't going to help many people as it's quite bespoke to my needs but this is the gist of the code I'm running with at the moment.

Expand|Select|Wrap|Line Numbers
  1. Private offScreen = 0
  2. Private screenWidth As Integer = 99999
  3. Private screenHeight As Integer = 99999
  4.  
  5. Private Sub Me_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6. Dim myScreens() As Screen = Screen.AllScreens
  7. For s = 0 To myScreens.Count - 1
  8.     screenWidth += myScreens(s).Bounds.Width
  9.     screenHeight += myScreens(s).Bounds.Height
  10. Next
  11. End Sub
  12.  
  13. Private Sub MyLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyLabel.Paint
  14. If offScreen = 0 then
  15. ' do the painting
  16. End If
  17. End Sub
  18.  
  19. Private Sub Me_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
  20. offScreen = 0
  21. If sender.left + Me.Width > screenWidth OrElse sender.top + Me.Height > screenHeight OrElse sender.left < 0 OrElse sender.top < 0 Then offScreen = 1
  22. If offScreen = 0 Then
  23.    MyLabel.Refresh()
  24. End If
  25. End Sub
  26.  
  27.  
May 3 '13 #10

Rabbit
Expert Mod 10K+
P: 12,349
What if in your Me_Move event you just invalidated the label?
May 3 '13 #11

100+
P: 116
Ha! Genius! Was working this harder than needed. Seems to work a charm. Many thanks, Rabbit.
May 10 '13 #12

Rabbit
Expert Mod 10K+
P: 12,349
Glad you got it resolved. Good luck with your project.
May 10 '13 #13

Post your reply

Sign in to post your reply or Sign up for a free account.