i need to make a theme form, chossing two colors, and make them in all of the project forms using this code maybe:

Sub RepaintForm()
grbrush = New System.Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), _
New Point(Me.Width, Me.Height), x, y)
Me.CreateGraphics.FillRectangle(grbrush, New Rectangle(0, 0, _
Me.Width, Me.Height))
End Sub
Private Sub frmThemes_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
RepaintForm()
End Sub

Dani AI

Generated

A few practical tips to make a two-color gradient “theme” apply cleanly across an app without the flicker and maintenance pain of wiring the same Paint handler into every form.

  • Avoid calling CreateGraphics or doing one-off drawing in events that don't persist. That approach can flicker and doesn't survive invalidation. Instead centralize the background paint in a common base form or a small theme manager and paint using the PaintEventArgs.Graphics path (override OnPaintBackground or handle Paint), with double buffering enabled.
  • A small shared Theme object plus a BaseForm that all designer forms inherit from is the simplest, most maintainable pattern. When the theme changes raise an event that makes open forms Invalidate() so they redraw.

Example pattern (VB.NET):

' Theme + manager
Public Class Theme
    Public Property Color1 As Color
    Public Property Color2 As Color
    Public Property Mode As Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Vertical
End Class

Public Class ThemeManager
    Public Shared Property Current As Theme = New Theme()
    Public Shared Event ThemeChanged As EventHandler
    Public Shared Sub Apply(t As Theme)
        Current = t
        RaiseEvent ThemeChanged(Nothing, EventArgs.Empty)
    End Sub
End Class

' BaseForm
Public Class BaseForm
    Inherits Form
    Public Sub New()
        SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer, True)
        UpdateStyles()
        AddHandler ThemeManager.ThemeChanged, AddressOf AddressOfThemeChanged
    End Sub
    Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
        Using b As New Drawing2D.LinearGradientBrush(ClientRectangle, ThemeManager.Current.Color1, ThemeManager.Current.Color2, ThemeManager.Current.Mode)
            e.Graphics.FillRectangle(b, ClientRectangle)
        End Using
    End Sub
    Private Sub AddressOfThemeChanged(s As Object, e As EventArgs) Me.Invalidate(True) End Sub
End Class

Performance/edge notes: cache a bitmap of the gradient when size or colors change to avoid rebuilding the brush every frame for large forms; call Invalidate(True) to force child controls to redraw; many WinForms controls do not support true transparency—set control BackColor explicitly if needed. For API details see the .NET docs for LinearGradientBrush and Control.SetStyle/double-buffering guidance (see Control.SetStyle and OnPaintBackground).

This keeps the theme code centralized (answers 's class idea) while avoiding the redraw and bloat problems warned about.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

I don't think you need themes. The whole dotnet thing is bloated enough, no need to add to that by having each frame rendered as your own theme.

You can just type that code into a class file and whenever a new form is created just call the class file in the load event. This should do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.