VIBlend
Home Products Downloads Purchase Support Forum About Blog

Implementing multi-threading in WinForms using the BackgroundWorker class and the VIBlend WinForms ProgressBar.

by viblend 31. January 2012 04:24
In this post we’ll create a simple multi-threaded WinForms application and will use the VIBlend WinForms ProgressBar control to report the progress to the user. The application will loop through numbers from 0 to 999. The progress bar will be filled during the process.

Let’s start by creating a Windows Application. In designer of the form, add the BackgroundWorker component from the “Components” category. The component is added to the bottom section of the designer.  Click it and set the WorkerReportsProgress property to true - this allows us to send progress notifications. Subscribe to the BackgroundWorker's DoWork and ProgressChanged events. The background task is handled in the DoWork event. The handler for this event will be called from a new worker thread. The ProgressBar will be updated in the ProgressChanged event handler. In order to notify about the progress you should call the ReportProgress method from the DoWork event handler. Please note that the ReportProgress method will work only when the WorkerReportsProgress property of the BackgroundWorker class is set to true.  

The next step is to create the User Interface. Drag and drop vButton and vProgressBar controls from the Visual Studio toolbox. Subscribe to the Button's Click event. To start doing the work we will call the RunWorkerAsync method of the BackgroundWorker in the Button's Click event handler.

In the ProgressChanged event handler, we will update the ProgressBar's value.

C#

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   // update the progress bar
    this.vProgressBar1.Value = e.ProgressPercentage;
    this.vProgressBar1.Refresh();
}

VB .NET

Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
   ' update the progress bar
Me.vProgressBar1.Value = e.ProgressPercentage
Me.vProgressBar1.Refresh()
End Sub

Report the Progress in the DoWork event handler.

C#

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
              int limit = 999;
for (int currNumber = 0; currNumber < 999; currNumber++)
{
       int progressPercentage = (100 * currNumber) / (limit);
                   worker.ReportProgress(progressPercentage);
}
}

VB .NET

Private Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
 Dim limit As Integer = 999
For currNumber As Integer = 0 To 998
  Dim progressPercentage As Integer = (100 * currNumber) / (limit)
  worker.ReportProgress(progressPercentage)
Next currNumber
End Sub
 
 

WinForms Button

by viblend 14. January 2012 21:24

To add a WinForms Button to your application you need to drag a new instance of the vButton Control from the Visual Studio Toolbox and drop it into your winforms application's form. Some of the frequently used properties such as TextAlign, ImageAlign and TextImageRelation allow you to easily change the layout of the Button's Text and Image. The VIBlend Button Control comes with 25 built-in Themes. It supports the new Metro Blue, Green and Orange themes and the popular Office Themes including the themes from Microsoft Office 2003, Office 2007 and Office 2010. You can easily change the winforms button theme. What you need to do is to just set the VIBlendTheme property and the winforms button will automatically change its visual appearance. Sometimes users want to customize a theme settings such as backcolor or forecolor. In this post we will create a custom Theme using the VIBlend Controls APIs and will apply it to the WinForms Button control.

- Define the theme colors. The colors array defines the default state colors, the highlightcolors array define the colors used when the mouse is over the button. The pressedcolors array defines colors used when the winforms button is pressed.

C#

Color[] colors = new Color[]{Color.FromArgb(255,39,94,176),
Color.FromArgb(255, 37, 92, 175), Color.FromArgb(255,51,105,206),
Color.FromArgb(255,53,135,226)};

Color[] highlightColors = new Color[]{Color.FromArgb(255,59,114,196),
Color.FromArgb(255, 57, 112, 185), Color.FromArgb(255,71,125,226),
Color.FromArgb(255,73,155,246)};

Color[] pressedColors = new Color[]{Color.FromArgb(255,79,134,216),
Color.FromArgb(255, 77, 132, 215), Color.FromArgb(255,91,145,246),
Color.FromArgb(255,93,175,255)};

VB .NET

Dim colors() As Color = {Color.FromArgb(255,39,94,176), Color.FromArgb(255, 37, 92, 175), Color.FromArgb(255,51,105,206), Color.FromArgb(255,53,135,226)}

Dim highlightColors() As Color = {Color.FromArgb(255,59,114,196), Color.FromArgb(255, 57, 112, 185), Color.FromArgb(255,71,125,226), Color.FromArgb(255,73,155,246)}

Dim pressedColors() As Color = {Color.FromArgb(255,79,134,216), Color.FromArgb(255, 77, 132, 215), Color.FromArgb(255,91,145,246), Color.FromArgb(255,93,175,255)}

- Define the default, highlight and pressed states. The following code illustrates how to create the styles for the winforms button states. The 90 number is for the gradient angle. This means that the button's gradient will be vertical. The 0.7f and 0.9f define the offset between each gradient color starting from 0 to 1. You can think about this as: 0f - first color, 0.7f - second color, 0.9f third color and 1.0f fourth gradient color.

C#

FillStyleGradientEx normalStyle = new FillStyleGradientEx(colors[0], colors[1],
    colors[2], colors[3], 90, 0.7f, 0.9f);

FillStyleGradientEx highlightStyle = new FillStyleGradientEx(highlightColors[0], highlightColors[1],
    highlightColors[2], highlightColors[3], 90, 0.7f, 0.9f);

FillStyleGradientEx pressedStyle = new FillStyleGradientEx(pressedColors[0], pressedColors[1],
    pressedColors[2], pressedColors[3], 90, 0.7f, 0.9f);

Color borderColor = Color.FromArgb(31, 72, 161);

VB .NET

Dim normalStyle As New FillStyleGradientEx(colors(0), colors(1), colors(2), colors(3), 90, 0.7f, 0.9f)

Dim highlightStyle As New FillStyleGradientEx(highlightColors(0), highlightColors(1), highlightColors(2), highlightColors(3), 90, 0.7f, 0.9f)

Dim pressedStyle As New FillStyleGradientEx(pressedColors(0), pressedColors(1), pressedColors(2), pressedColors(3), 90, 0.7f, 0.9f)

Dim borderColor As Color = Color.FromArgb(31, 72, 161)

- Create a ControlTheme instance and initialize its StyleNormal, StyleHighlight and StylePressed properties. The FillStyle member defines the background, BorderColor defines the border's color and TextColor defines the displayed text's color.

C#

ControlTheme theme = ControlTheme.GetDefaultTheme(VIBLEND_THEME.OFFICEBLUE).CreateCopy();

theme.StyleNormal.FillStyle = normalStyle;
theme.StyleNormal.BorderColor = borderColor;
theme.StyleNormal.TextColor = Color.White;

theme.StyleHighlight.FillStyle = highlightStyle;
theme.StyleHighlight.BorderColor = borderColor;
theme.StyleHighlight.TextColor = Color.White;

theme.StylePressed.FillStyle = pressedStyle;
theme.StylePressed.BorderColor = borderColor;
theme.StylePressed.TextColor = Color.White;

VB .NET

Dim theme As ControlTheme = ControlTheme.GetDefaultTheme(VIBLEND_THEME.OFFICEBLUE).CreateCopy()

theme.StyleNormal.FillStyle = normalStyle
theme.StyleNormal.BorderColor = borderColor
theme.StyleNormal.TextColor = Color.White

theme.StyleHighlight.FillStyle = highlightStyle
theme.StyleHighlight.BorderColor = borderColor
theme.StyleHighlight.TextColor = Color.White

theme.StylePressed.FillStyle = pressedStyle
theme.StylePressed.BorderColor = borderColor
theme.StylePressed.TextColor = Color.White

- The final step is to set the button's Theme property to point to the ControlTheme instance.

C#

this.vButton1.StyleKey = "ButtonNewStyle";
this.vButton1.Theme = theme;

VB .NET

Me.vButton1.StyleKey = "ButtonNewStyle"
Me.vButton1.Theme = theme

VIBlend Controls for Windows Forms - ver. 6.0 Released

by viblend 27. February 2011 18:39

We are pleased to announce the availability of the VIBlend Controls for WinForms ver. 6.0.0. the next generation of user interface controls for Windows Forms from VIBlend.
This release features some important changes that you will want to check out.  The release notes provide a full list of all the changes, but we wanted to point out some key features.

Improved DataGridView: The DataGridView delivers extended filtering and localization options, enhanced Pivot-Design panel, Pivot-Table Totals and new cell editors.

Improved List Controls: The ComboBox and ListBox controls feature an improvement in the binding capabilities that will allow you to bind these controls to any data source. Another improvement is the enhanced styling that allows you to customize the look and feel of every list item.

Improved Styling and Appearance: In the continuous effort to make VIBlend WinForms controls meet your specific application needs, you will see improved styling features and API improvements across all controls.



If you want to check out the latest release, it is now available for download.

VIBlend WinForms Controls ver. 4.6 – Released

by viblend 22. April 2010 09:17

We are happy to announce the release of the latest version of VIBlend Controls for WinForms.

VIBlend Windows Forms Controls 4.6.0 - Release Notes

1. Added support for Visual Studio 2010
2. Moved all design time classes of our controls to separate assembly
3. DataGridView changes

In this version we extended the HierarhcyItem mouse click events. Previously, the grid was raising only the ItemClick and ItemDoubleClick events.

These two events are replaced by the following set of events:

 - HierarchyItemMouseUp
 - HierarchyItemMouseDown
 - HierarchyItemMouseClick
 - HierarchyItemMouseDoubleClick

We also added several improvements in the in-place cell editors' infrastructure. Specifically we added the following activation and deactivation events:

 - Click on selected cell (MOUSE_CLICK_SELECTED_CELL)
 - Enter key activation (KEY_PRESS_ENTER
 - Esc key deactivation (KEY_PRESS_ESC)

The default activation and deactivation flags for the built-in cell editors where modified to use the new activation events.

Please, note that these are breaking changes. If you are using an earlier version of VIBlend Controls for Windows Forms you will have to make minor changes to your code.

New in version 4.6 are the HierarchyItems and Cells selection changed events:
 - DataGridView.HierarchyItemSelectionChanged
 - DataGridView.CellSelectionChanged

Pivot Grid - Traditional vs Compact style layout

by viblend 18. April 2010 05:36

VIBlend DataGridView for WinForms offers multiple unique features that cannot be found in other Windows Forms data grid controls. These include multi-level hierarchies on rows and columns, rows grouping, pivot tables, and built-in OLAP capabilities. One of the main drawbacks of the traditional OLAP style rows layout is that it takes a lot of screen space. 

 

This could be improved by using the compact style layout. In this mode the rows of the pivot table are rendered in a tree-like structure.

 


Switching from default to compact style rendering is very easy. It requires setting the value of RowsHierarchy.CompactStyleRendering property to true:

C#
grid.RowsHierarchy.CompactStyleRenderingEnabled = true;
grid.RowsHierarchy.AutoResize();
grid.Invalidate();

VB .Net
grid.RowsHierarchy.CompactStyleRenderingEnabled = True
grid.RowsHierarchy.AutoResize()
grid.Invalidate()
 

You can switch between traditional and compact style rendering at runtime. The feature is also availalbe in VIBlend DataGrid for Silverlight.

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET