P
Paul Ishak
Guest
Hello, Im probably overlooking something simple, but maybe not...
I have made an inherited panel that draws a multi color gradient as a background color.
Here is the faulty code that the designer generates
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
NOTE: The following procedure is required by the Windows Form Designer
It can be modified using the Windows Form Designer.
Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.MultiColorGradientPanel1 = New WindowsApplication2.MultiColorGradientPanel()
Me.SuspendLayout()
MultiColorGradientPanel1
Me.MultiColorGradientPanel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
These three lines should not be generated by the designer?
New WindowsApplication2.MultiColorGradientPanel.ColorCollection().Add(System.Drawing.Color.Red)
New WindowsApplication2.MultiColorGradientPanel.ColorCollection().Add(System.Drawing.Color.Blue)
New WindowsApplication2.MultiColorGradientPanel.ColorCollection().Add(System.Drawing.Color.Orange)
Me.MultiColorGradientPanel1.Location = New System.Drawing.Point(43, 23)
Me.MultiColorGradientPanel1.Name = "MultiColorGradientPanel1"
Me.MultiColorGradientPanel1.Size = New System.Drawing.Size(264, 38)
Me.MultiColorGradientPanel1.TabIndex = 0
Form1
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.Color.Black
Me.ClientSize = New System.Drawing.Size(504, 382)
Me.Controls.Add(Me.MultiColorGradientPanel1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents MultiColorGradientPanel1 As WindowsApplication2.MultiColorGradientPanel
End Class
Here is the MultiColorGradientPanel class
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Drawing.Drawing2D
Public Class MultiColorGradientPanel
Inherits Panel
Public Property GradientColors As ColorCollection
Public Sub GradientColorsChanged(sender As Object, e As ColorChangedEventArgs)
Me.Invalidate()
End Sub
Protected Overrides Sub OnSizeChanged(e As EventArgs)
Me.Invalidate()
MyBase.OnSizeChanged(e)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
If GradientColors.Count = 1 Then Me.BackColor = GradientColors(0) : Exit Sub
If GradientColors.Count = 0 Then Exit Sub
Dim Blend As New Drawing2D.ColorBlend()
Blend.Colors = GradientColors.ToArray
Dim Positions As New List(Of Single)
Dim StepSize As Single = 1S / CSng(Blend.Colors.Count - 1), I As Single
For I = 0 To 1 Step StepSize
If Positions.Count = GradientColors.Count - 1 Then Exit For
Positions.Add(I)
Next
Positions.Add(1S)
Dim LGB As New LinearGradientBrush(New Point(0, 0), _
New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height), _
Color.Black, _
Color.Black)
Blend.Positions = Positions.ToArray
LGB.InterpolationColors = Blend
e.Graphics.FillRectangle(LGB, Me.ClientRectangle)
MyBase.OnPaint(e)
End Sub
Sub New()
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.DoubleBuffered = True
Me.GradientColors = DefaultColors()
AddHandler GradientColors.GradientColorsChanged, AddressOf GradientColorsChanged
End Sub
Function DefaultColors() As ColorCollection
Dim ColorCollection As New ColorCollection
Set Default colors here(in order left to right)
ColorCollection.Add(Color.Red)
ColorCollection.Add(Color.Blue)
ColorCollection.Add(Color.Orange)
Return ColorCollection
End Function
Public Class ColorCollection
Inherits Collection(Of Color)
Public Event GradientColorsChanged As EventHandler(Of ColorChangedEventArgs)
Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal newItem As Color)
MyBase.InsertItem(index, newItem)
RaiseEvent GradientColorsChanged(Me, New ColorChangedEventArgs(ChangeType.Added, newItem, Nothing))
End Sub
Protected Overrides Sub SetItem(ByVal index As Integer, ByVal newItem As Color)
Dim replaced As Color = Items(index)
MyBase.SetItem(index, newItem)
RaiseEvent GradientColorsChanged(Me, New ColorChangedEventArgs(ChangeType.Replaced, replaced, newItem))
End Sub
Protected Overrides Sub RemoveItem(ByVal index As Integer)
Dim removedItem As Color = Items(index)
MyBase.RemoveItem(index)
RaiseEvent GradientColorsChanged(Me, New ColorChangedEventArgs(ChangeType.Removed, removedItem, Nothing))
End Sub
Protected Overrides Sub ClearItems()
MyBase.ClearItems()
RaiseEvent GradientColorsChanged(Me, New ColorChangedEventArgs(ChangeType.Cleared, Nothing, Nothing))
End Sub
Sub New()
End Sub
End Class
Public Class ColorChangedEventArgs
Inherits EventArgs
Public ReadOnly OldColor As Color
Public ReadOnly ChangeType As ChangeType
Public ReadOnly NewColor As Color
Public Sub New(ByVal change As ChangeType, ByVal OldColor As Color, ByVal NewColor As Color)
ChangeType = change
Me.OldColor = OldColor
Me.NewColor = NewColor
End Sub
End Class
Public Enum ChangeType
Added
Removed
Replaced
Cleared
End Enum
End Class
“If you want something youve never had, you need to do something youve never done.”
Dont forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
*This post does not reflect the opinion of Microsoft, or its employees.
Continue reading...