Increasing performance of windows forms with multiple controls

Sindhu H 201 Reputation points
2021-09-21T11:18:04.767+00:00

Hello,

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,823 questions
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2021-09-22T11:23:11.077+00:00

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

    134333-scrollviewer-test.jpg

    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("https://i.ibb.co/WVfdcJb/hulk.jpg"), 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("https://i.ibb.co/52Lz2mk/Schtroumpfette.jpg"), 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  
                    Else  
                        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  
                    grid1.Children.Add(imgArray(i))  
                    AddHandler imgArray(i).MouseDown, AddressOf Image_MouseDown  
                    i += 1  
                    If (i Mod 10 = 0) Then  
                        nY += nImageHeight  
                        nX = 0  
                    End If  
                Loop  
      
                scrollViewer1.Content = grid1  
                gridMain.Children.Add(scrollViewer1)  
      
                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  
                }  
                Me.Controls.Add(elementHost1)  
                Return  
            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