User control location

pilsdumps

Member
Joined
Jul 27, 2009
Messages
18
Programming Experience
3-5
I have a user control (that displays some basic information about some events) called AE. I have another user control called CTCAE containing a panel that I wish to populate with the AEs. Within each AE is a tablelayoutpanel control, and within each cell of this is a label.

The labels are populated from a database, and using the e.Graphics.MeasureString of the AE paint event, I've managed to get the AE labels to resize according to the lengths of the labels. So far so good.

However, the problem I'm having is locating the AE control within the panel in CTCAE. If I simply add each AE to the panel, as expected they are all placed in the top left corner and overwrite one another. I've tried a couple of approaches to overcome this;

1. add a property to the AE control that gives the height of the highest label and use this when adding the controls to the panel
2. get the height of the AE control when adding to the CTCAE panel

Using these heights, I could increment the location of the next AE control.

However, the height of each control is not correctly recorded. The height is always given as 38, which is the original size of the control prior to being resized by the paint event. It seems that the paint event resizes the control for display, but I can't access its new size.

How can I get the each controls height so I can locate them correctly?

Thanks
 

pilsdumps

Member
Joined
Jul 27, 2009
Messages
18
Programming Experience
3-5
User control renders slowly

Hi,

I have a user control containing some labels that need to resize according to how long the label text is. The text comes from a database.

I've achieved what I want using the paint event of the control;

VB.NET:
        Dim StringSize As New SizeF

        'check which label is longest
        Dim tmp_ae As ae
        Dim max_label As Label = lbl_ae
        Dim max_length As Integer = 0

        tmp_ae = sender

        For Each c As Control In tmp_ae.tlp_ae.Controls

            If TypeOf c Is Label Then
                If Len(c.Text) > max_length Then
                    max_label = c
                    max_length = Len(c.Text)
                End If

            End If

        Next

        StringSize = e.Graphics.MeasureString(max_label.Text, m_font)

        'work out the height and assign it
        Dim calc_height As Integer = (StringSize.Width / max_label.Width + m_offset) * (StringSize.Height)

        If calc_height < 20 Then
            Me.tlp_ae.Height = 20
        Else
            Me.tlp_ae.Height = calc_height
            Me.lbl_ae.Height = calc_height
            Me.Height = calc_height
        End If

    End Sub

The problem is that the control takes a second or two to redraw when different data is used, first briefly showing the labels at their original height then flickering as they resize. There is quite a lot of processing going on in the paint event, and as it is being called frequently I guess the slowness and flickering is no surprise.

I've tried adding in suspendlayout and resumelayout, added doublebuffering
VB.NET:
        Me.SetStyle(ControlStyles.DoubleBuffer _
                    Or ControlStyles.UserPaint _
                    Or ControlStyles.AllPaintingInWmPaint, True)
        Me.UpdateStyles()

all without any noticeable improvement.

I'd rather users had a blank form for a second than the flickering. Is there a way I can create the controls, resize them and then show them?

Thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,546
Location
Norway
Programming Experience
10+
The labels are populated from a database, and using the e.Graphics.MeasureString of the AE paint event, I've managed to get the AE labels to resize according to the lengths of the labels. So far so good.
Resizing the labels in a Paint event does not sound as a good idea. You can use Control.CreateGraphics to get a Graphics instance used for measurements, resize controls and then done with it.
CTCAE containing a panel
I think replacing this Panel with a FlowLayoutPanel would solve your problem.
Within each AE is a tablelayoutpanel control
Best Practices for the TableLayoutPanel Control
They advice to use this control sparingly, I don't know how this and the alternatives affects your case.
 
Top Bottom