Increasing performance of windows forms with multiple controls

Sindhu H 201 Reputation points


I am currently developing a windows app, which has at least 400 controls and a lot more events and actions.
However, now that I have started coding, with 25 controls itself, the winform is beginning to decline its performance by showing lag, while executing and also while coding especially while dragging and dropping controls on the designer tab (takes at least 15 seconds to display).
I have been using multiple forms, but still facing the same problem.

Please help me guide you to overcome this error.

Thank you in advance.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,869 questions
{count} votes

Accepted answer
  1. Castorix31 83,101 Reputation points

    From comments,
    a test with 400 images in a ScrollViewer embedded with ElementHost (+ a click event) =>


    Add the 4 references first :

        ' Add reference to PresentationFramework  
        ' Add reference to PresentationCore  
        ' Add reference to WindowsBase  
        ' Add reference to WindowsFormsIntegration  
        ' Add Imports System.Windows.Forms.Integration  

    Test :

    Imports System.Windows.Forms.Integration  
    Public Class Form1  
        Dim nImageWidth As Double = 0  
        Dim nImageHeight As Double = 0  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            If True Then  
                Dim nWidth As Double = 600, nHeight As Double = 400  
                Dim gridMain As System.Windows.Controls.Grid = New System.Windows.Controls.Grid()  
                gridMain.Background = System.Windows.Media.Brushes.White  
                Dim scrollViewer1 As New System.Windows.Controls.ScrollViewer  
                scrollViewer1.HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto  
                Dim grid1 As New System.Windows.Controls.Grid  
                grid1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left  
                grid1.VerticalAlignment = System.Windows.VerticalAlignment.Top  
                Dim sTempPath As String = System.IO.Path.GetTempPath()  
                sTempPath = String.Concat(sTempPath, "Hulk.jpg")  
                Using wc As New System.Net.WebClient()  
                    wc.DownloadFile(New Uri(""), sTempPath)  
                End Using  
                Dim sImageFileName As String = sTempPath  
                Dim bmp1 As System.Windows.Media.Imaging.BitmapImage = New System.Windows.Media.Imaging.BitmapImage(New Uri(sImageFileName, UriKind.Absolute))  
                sTempPath = System.IO.Path.GetTempPath()  
                sTempPath = String.Concat(sTempPath, "Schtroumpfette.jpg")  
                Using wc As New System.Net.WebClient()  
                    wc.DownloadFile(New Uri(""), sTempPath)  
                End Using  
                sImageFileName = sTempPath  
                Dim bmp2 As System.Windows.Media.Imaging.BitmapImage = New System.Windows.Media.Imaging.BitmapImage(New Uri(sImageFileName, UriKind.Absolute))  
                Dim imgArray() As System.Windows.Controls.Image = New System.Windows.Controls.Image(400 - 1) {}  
                nImageWidth = Math.Max(bmp1.PixelWidth, bmp2.PixelWidth)  
                nImageHeight = Math.Max(bmp1.PixelHeight, bmp2.PixelHeight)  
                Dim i As Integer = 0  
                Dim nX As Double = 0, nY As Double = 0  
                Do While (i < imgArray.Length)  
                    Dim img1 As System.Windows.Media.Imaging.BitmapImage  
                    If (i Mod 2 = 0) Then  
                        img1 = bmp1  
                        img1 = bmp2  
                    End If  
                    imgArray(i) = New System.Windows.Controls.Image()  
                    imgArray(i).Source = img1  
                    imgArray(i).Width = nImageWidth  
                    imgArray(i).Height = nImageHeight  
                    imgArray(i).Margin = New System.Windows.Thickness(nX, nY, 0, 0)  
                    imgArray(i).HorizontalAlignment = System.Windows.HorizontalAlignment.Left  
                    imgArray(i).VerticalAlignment = System.Windows.VerticalAlignment.Top  
                    nX += nImageWidth  
                    AddHandler imgArray(i).MouseDown, AddressOf Image_MouseDown  
                    i += 1  
                    If (i Mod 10 = 0) Then  
                        nY += nImageHeight  
                        nX = 0  
                    End If  
                scrollViewer1.Content = grid1  
                Dim elementHost1 As ElementHost = New ElementHost() With {  
                    .Child = gridMain,  
                    .Location = New System.Drawing.Point(10, 10),  
                    .Size = New System.Drawing.Size(CInt(nWidth), CInt(nHeight)),  
                    .BackColor = System.Drawing.Color.Yellow  
            End If  
        End Sub  
        Private Sub Image_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)  
            Dim img As System.Windows.Controls.Image = TryCast(sender, System.Windows.Controls.Image)  
            If (e.LeftButton = 1) Then  
                Dim nX As Double = img.Margin.Left / nImageWidth  
                Dim nY As Double = img.Margin.Top / nImageHeight  
                MessageBox.Show(String.Format("Image ({0} - {1}) clicked", nX.ToString(), nY.ToString()), "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)  
            End If  
        End Sub  
    End Class  
    0 comments No comments

0 additional answers

Sort by: Most helpful