VB.NET change background color or image in windows form application in the taskbar when user running multiple instances of application

Farkler 211 Reputation points
2021-08-25T13:12:45.56+00:00

Thanks for reading. We have a VB.NET windows form application running .NET 4.8 Framework. Users often run multiple instances of the application. When this occurs, all of the same colored minimized windows in the taskbar make it difficult for the user to distinguish the different instances. I would like to implement a background color change based on the users Process ID (System.Diagnostics.Process.GetCurrentProcess). For instance, if the user had 3 instances of the application running with 2 forms open apiece then there would be 6 windows in the taskbar of the same color and/or text. I would like to differentiate between those 3 instances if possible. Thanks for the help.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,610 questions
{count} votes

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,201 Reputation points
    2021-08-25T15:02:35.63+00:00

    Basic logic would go into MyApplication class which is created from project properties, Application tab, View Application events button.

    Something like this which as coded is limited to colors/open instances of an application.

    Imports Microsoft.VisualBasic.ApplicationServices  
      
    Namespace My  
        Partial Friend Class MyApplication  
            Public backColorDictionary As Dictionary(Of Integer, Color) =  
                       New Dictionary(Of Integer, Color) From  
                {  
                    {1, Color.Red},  
                    {2, Color.Green},  
                    {3, Color.Blue},  
                    {4, Color.DarkOrange}  
                }  
      
            Private _color As Color  
            ''' <summary>  
            ''' Form background color  
            ''' </summary>  
            ''' <returns></returns>  
            Public ReadOnly Property BackColor() As Color  
                Get  
                    Return _color  
                End Get  
            End Property  
      
            ''' <summary>  
            ''' Logic to determine background color to use or a default if more instance  
            ''' of the application open than colors in <see cref="backColorDictionary"/>  
            ''' </summary>  
            ''' <param name="sender"></param>  
            ''' <param name="e"></param>  
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup  
      
                Dim currentProcess = Process.GetCurrentProcess().ProcessName  
                Dim counter = Process.GetProcessesByName(currentProcess).Length  
                If counter <= backColorDictionary.Count Then  
                    _color = backColorDictionary(counter)               '  
                Else  
                    _color = Color.White  
                End If  
      
            End Sub  
        End Class  
    End Namespace  
      
    

    Then in a form load event

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
        BackColor = My.Application.BackColor  
    

    Update

    Here we get over 100 colors

    Imports System.Reflection  
    Imports Microsoft.VisualBasic.ApplicationServices  
      
    Namespace My  
        Partial Friend Class MyApplication  
            Public backColorDictionary As Dictionary(Of Integer, Color) = CreateColorsDictionary()  
      
            Public Shared Function ColorStructToList() As List(Of Color)  
                Return GetType(Color).GetProperties(  
                    BindingFlags.Static Or  
                    BindingFlags.DeclaredOnly Or  
                    BindingFlags.Public).Select(  
                        Function(c)  
                            Return CType(c.GetValue(Nothing, Nothing), Color)  
                        End Function).ToList()  
            End Function  
            Private Function CreateColorsDictionary() As Dictionary(Of Integer, Color)  
                Dim colors = ColorStructToList()  
      
                Dim results = colors.Select(Function(item, index) New With  
                                               {  
                                                    .Index = index + 1,  
                                                    .Color = item  
                                               })  
                Dim Dictionary = New Dictionary(Of Integer, Color)  
      
      
                For Each anonymous In results  
      
                    If anonymous.Color = Color.Transparent Then  
                        Dictionary.Add(anonymous.Index, Color.White)  
                    Else  
                        Dictionary.Add(anonymous.Index, anonymous.Color)  
                    End If  
      
                Next  
      
                Return Dictionary  
      
            End Function  
      
            Private _color As Color  
            ''' <summary>  
            ''' Form background color  
            ''' </summary>  
            ''' <returns></returns>  
            Public ReadOnly Property BackColor() As Color  
                Get  
                    Return _color  
                End Get  
            End Property  
      
            ''' <summary>  
            ''' Logic to determine background color to use or a default if more instance  
            ''' of the application open than colors in <see cref="backColorDictionary"/>  
            ''' </summary>  
            ''' <param name="sender"></param>  
            ''' <param name="e"></param>  
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup  
      
                Dim currentProcess = Process.GetCurrentProcess().ProcessName  
                Dim counter = Process.GetProcessesByName(currentProcess).Length  
                If counter <= backColorDictionary.Count Then  
                    _color = backColorDictionary(counter)               '  
                Else  
                    _color = Color.White  
                End If  
      
            End Sub  
        End Class  
    End Namespace  
      
    

    Opening three instances gets
    126441-figure1.png

    0 comments No comments

  2. Farkler 211 Reputation points
    2021-08-25T17:32:03.877+00:00

    very helpful both Castorix31 and Karen. Thanks!

    0 comments No comments