Frage

I've created my own winforms month-view calendar in VB.net.

To do it I've used a table layout panel, with 42 separate cells. In each of the cells is a label called lblDay1, lblDay2, etc.

When I load the page, the labels are all written to with the correct numbers for that month.

 Dim daysInMonthCnt As Integer =31 'Assume 31 days for now
 Dim firstDay As Integer = Weekday("1/" & Now.month & "/" & Now.year) 'Get weekday for 1st of month
 For dayCount As Integer = firstDay To daysInMonthCnt
    Dim lbl As Label
    lbl = CType(pnlMonthBody.Controls("lblDay" & dayCount), Label)
    lbl.Text = dayCount 'Write to label
 Next dayCount

Unfortunately, that turns out to be incredibly slow to load. Can anyone please suggest a faster method.

War es hilfreich?

Lösung

Just writing values to a so small number of labels is a really fast process. The problems you are experiencing have to do most likely with the VB.NET "problems" while refreshing the contents of GUI controls; the best way to fix this is looking into multithreading, as suggested by FraserOfSmeg.

As far as I think that this is a pretty simplistic GUI with a low number of controls and a not too demanding algorithm (big amount of/long loops is the prime cause of GUI-refreshing problems), you might get an acceptable performance even without relying on multithreading. In your situation, I would do the following:

  1. A container (the TableLayoutPanel you are using or something simpler, like a Panel) including all the labels at the start. In case of not getting too messy (what does not seem to be the case, with just 42 labels) I would include them in the "design view" (rather than at run time).
  2. A function populating all the labels depending upon the given month.
  3. A "transition effect" for the container called every time the user selects a different month. You can accomplish this quite easily with a Timer relocating the container (e.g., when the button is clicked the container's position is set outside the form and then comes back gradually (20 points per 10ms -> made-up numbers) until being back to its original position).
  4. Synchronising the two points above: the values of the labels will start changing when the transition starts; in this way the user will not notice anything (just a nice-appealing transition month to month).

This GUI should deliver the kind of performance you are after. If not, you should improve its performance by relying on additional means (e.g., the proposed multi-threading).

SAMPLE CODE TO ILLUSTRATE POINT 3

Add a panel (Panel1), a button (Button1) and a timer (Timer1) to a new form and the code below.

Public Class Form1

    Dim curX, origX, timerInterval, XIncrease As Integer
    Dim moving As Boolean
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If (curX >= origX) Then
            If (moving) Then
                curX = Panel1.Location.X
                moving = False
                Timer1.Stop()
            Else
                curX = 0 'getting it out of the screen
                moving = True
            End If
        Else
            curX = curX + XIncrease
        End If
        Panel1.Location = New Point(curX, Panel1.Location.Y)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Timer1.Start()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        XIncrease = 100
        timerInterval = 100
        Panel1.BackColor = Color.Maroon
        origX = Panel1.Location.X
        curX = origX
        With Timer1
            .Enabled = False
            .Interval = timerInterval
        End With

    End Sub
End Class

This is very simplistic, but shows the idea clearly: when you click the button the panel moves in X; by affecting the timerInterval and XIncrease values you can get a nice-looking transition (there are lots of options, bear in mind that if you set curX to minus the width of the panel, rather than to zero, it goes completely outside the form).

Andere Tipps

If you're purely interested in speeding up your code I'd suggest running the loading code on multiple threads simultaneously. This may be overkill depending on your application needs but it's a good way to code. As a side note so the program looks a little more slick for the end user I'd suggest always running time consuming processes such as this on a separate thread(s).

For info on multithreading have a look at this page:Mutlithreading tutorial

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top