?
Solved

Forcing Paint Event

Posted on 2009-02-22
2
Medium Priority
?
269 Views
Last Modified: 2013-11-27
The code below draws some Labels on the screen and fills them with random numbers.

As you can see, the code resides in a Button1_Click event.

How can I get rid of all those labels so that when I click Button1 again, the whole thing refreshes and puts up the *new* Labels?.

At the moment, the screen does not 'update' with the new Labels. The old Labels (from the first click) remain in view.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim lbList As New List(Of Label)
        Dim i As New Integer
        Dim theLeft As Integer = 70
        Dim seed As Integer, lower As Integer, upper As Integer
        Dim randobj As New Random(seed)
        'Dim j As Integer
        'For j = 0 To 5
        '    Console.Write(" {0,10} ", randobj.Next())
        'Next j
 
        For i = 1 To 7
            lbList.Add(New Label())
        Next i
 
        lower = 0 : upper = 5
        For Each lb As Label In lbList
            lb.Height = 50
            lb.Width = 80
            lb.Left = theLeft
            theLeft += 80
            lb.Font = New Font("Arial Black", 8, FontStyle.Bold)
            lb.Text = randobj.Next(lower, upper)
 
            lb.Visible = True
            lb.BorderStyle = BorderStyle.FixedSingle
            Me.Controls.Add(lb)
        Next lb
        MessageBox.Show("Done")
        
 
    End Sub

Open in new window

0
Comment
Question by:KarlTheHopeless
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 375 total points
ID: 23707636
You should only create the Labels ONCE...when the Form is first shown would be a good time.  Then, in the Button Click event, all you need to do is pick new values and display them.  I've moved "lbList" up to the Form level so it can be accessed from the Load() event and the Button Click() event:
Public Class Form1
 
    Private lbList As New List(Of Label)
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim theLeft As Integer = 70
        For i As Integer = 1 To 7
            Dim lb As New Label
            lbList.Add(lb)
            lb.Height = 50
            lb.Width = 80
            lb.Left = theLeft
            theLeft += 80
            lb.Font = New Font("Arial Black", 8, FontStyle.Bold)
            lb.Visible = True
            lb.BorderStyle = BorderStyle.FixedSingle
            Me.Controls.Add(lb)
        Next
        Button1.PerformClick()
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static randobj As New Random
        Dim lower As Integer = 0, upper As Integer = 5
        For Each lbl As Label In lbList
            lbl.Text = randobj.Next(lower, upper)
        Next
    End Sub
 
End Class

Open in new window

0
 

Author Closing Comment

by:KarlTheHopeless
ID: 31549894
Thank you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question