Category / Section
How to specify a gradient or other brush for the GroupBarItem background in WinForms GroupBar?
1 min read
GroupBarItem background
The GroupBar control permits programmatic assignment of any System.Drawing.Brush derived class for drawing the GroupBarItem background region through the GroupBarItem.BackgroundBrush property.
The following code excerpt demonstrates creating and assigning a GradientBrush for one of the GroupBarItem objects.
C#
// Set up the Blend for the GradientBrush Blend blend = new Blend(); blend.Factors = new float[] {0.0f, 0.0f, 1.0f}; blend.Positions = new float[] {0.0F, 0.5f, 1.0F}; // Estimate the GroupBarItem bounds Rectangle rcgroupbaritem = new Rectangle(0, 0, this.groupBar1.Width, this.groupBar1.GroupBarItemHeight); // Create and initialize the LinearGradientBrush LinearGradientBrush lgbrush = new LinearGradientBrush(rcgroupbaritem, Color.White, Color.Red, 0, true); lgbrush.Blend = blend; // Assign the gradientbrush to the GroupBarItem1 of GroupBar1 this.groupBarItem1.BackgroundBrush = lgbrush;
VB
' Set up the Blend for the GradientBrush blend As Blend = New Blend() blend.Factors = New Single() {0.0f, 0.0f, 1.0f} blend.Positions = New Single() {0.0F, 0.5f, 1.0F} ' Estimate the GroupBarItem bounds rcgroupbaritem As Rectangle = New Rectangle(0, 0, Me.groupBar1.Width, Me.groupBar1.GroupBarItemHeight) ' Create and initialize the LinearGradientBrush lgbrush As LinearGradientBrush = New LinearGradientBrush(rcgroupbaritem, Color.White, Color.Red, 0, True) lgbrush.Blend = blend ' Assign the gradientbrush to the GroupBarItem1 of GroupBar1 Me.groupBarItem1.BackgroundBrush = lgbrush