473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Label painting issue when form moved offscreen

116 64KB
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?

12 2118
Rabbit
12,516 Expert Mod 8TB
It would help to see the code.
Apr 27 '13 #2
robertybob
116 64KB
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
12,516 Expert Mod 8TB
Did you try clearing it before redrawing?
Apr 28 '13 #4
robertybob
116 64KB
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
12,516 Expert Mod 8TB
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
robertybob
116 64KB
Ah ok. Let me investigate the dragdrop - been away for a few days. Thanks Rabbit.
May 3 '13 #7
robertybob
116 64KB
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
12,516 Expert Mod 8TB
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
robertybob
116 64KB
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
12,516 Expert Mod 8TB
What if in your Me_Move event you just invalidated the label?
May 3 '13 #11
robertybob
116 64KB
Ha! Genius! Was working this harder than needed. Seems to work a charm. Many thanks, Rabbit.
May 10 '13 #12
Rabbit
12,516 Expert Mod 8TB
Glad you got it resolved. Good luck with your project.
May 10 '13 #13

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

Similar topics

1
by: Vetrivel | last post by:
Application architecture : Develop interface between two existing systems, a. Enterprise CRM system b. Web based intranet system. Environment : Intranet Server : IIS and ASP. Script :...
2
by: adawson | last post by:
Hi, I need to execute some code when form submission is stopped before the page is returned from the server. For example, a user clicks a button to submit the form, realizes they've made a...
5
by: Ron Vecchi | last post by:
I know the math I need to perform on width and height to keep an aspect ratio but where and how would I implement keeping a set aspect ratio on a form when a user resizes it. Override OnResize?...
0
by: Steve Bezner | last post by:
I have an issue in which a link label on a windows form does not draw the focus rectangle if you programmatically force it to have focus via the Control.Focus() method. To duplicate, create...
1
by: bob | last post by:
Currently i'm writing some low level communication modules in C++ and is thinking of putting it into a library so that it can be used in C#. My concern is the performance issue when putting C++...
2
by: Jaikumar | last post by:
Hi, 1) I have created one windows application, In the main form ( form1) i have added one usercontrol (usercontrol1), In that user control i am drawing one image. 2) In the UserControl1 i am...
6
by: Roger Ries via DotNetMonster.com | last post by:
I'm trying to change the text in a label from another form. lblInfo.Text = "ABC" Works for the form your currently in but how the heck do you change that label information from another form. ...
0
by: pargat.singh | last post by:
Hi Everyone: I have asimple app as below using System; using System.IO;
2
by: abid gee | last post by:
Please give a kind look on my question. and please comments. I am Using C# as development tool of Dot Net 2.0. I wrote a function read_data() that read data from Serial Port continuously.Till...
3
by: Elphaba31 | last post by:
Hello Gurus, I have been tasked with correcting issues with our upgrade(Access97/2000 to Access2007) and essentially know very little about Access (any version) or Visual Basic. Here is my current...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.