473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Update UI controls in Form class from another class

I have two classes:

BulkProcessing.cs
BulkProcessingStatusForm.cs

BulkProcessing uses a background worker to process each row of data.
BulkProcessingStatusForm is a UI form (dialog) that displays the
status of the processing.

Now I have "DoWork" method in BulkProcessing as below:

protected void DoWork(object sender, DoWorkEventArgs args)
{
for (int i = 0; i < mSelectedItems.Count; i++)
{
try
{
//omitted code
}
catch (Exception e)
{
//error handling
}
this.mBackgroundWorker.ReportProgress(i /
mSelectedItems.Count, i);

// How to update the UI controls on the other form
(BulkProcessingStatusFormBase)?
UpdateControls();

}
}

I was advised to use RaiseEvents (to observe events from the UI form).
But I don't know how. Any advice?
Dec 23 '07 #1
2 1968
RaiseEvents is a VB statement.

You can't update controls from your DoWork method because it is not running
on the GUI thread, DoWork runs on a background (threadpool) thread. The
ReportProgress method raises the ProgressChanged event that the form can
subscribe to. You can do whatever you want in your ProgressChanged event
handler because it's guaranteed to be using the GUI thread.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Curious" wrote:
I have two classes:

BulkProcessing.cs
BulkProcessingStatusForm.cs

BulkProcessing uses a background worker to process each row of data.
BulkProcessingStatusForm is a UI form (dialog) that displays the
status of the processing.

Now I have "DoWork" method in BulkProcessing as below:

protected void DoWork(object sender, DoWorkEventArgs args)
{
for (int i = 0; i < mSelectedItems.Count; i++)
{
try
{
//omitted code
}
catch (Exception e)
{
//error handling
}
this.mBackgroundWorker.ReportProgress(i /
mSelectedItems.Count, i);

// How to update the UI controls on the other form
(BulkProcessingStatusFormBase)?
UpdateControls();

}
}

I was advised to use RaiseEvents (to observe events from the UI form).
But I don't know how. Any advice?
Dec 23 '07 #2
Hi Peter,

Thanks for the advice! Could you tell me if the following is what you
have suggested?

// In BulkProcessing
public BulkProcessing(SortableBindingList<IWorkerItemBase >
selectedItems)
{

// Omitted code

// Add event handler to invoke updating UI controls when
there's a change in progress
this.mBackgroundWorker.ProgressChanged += new
ProgressChangedEventHandler(WorkProgressChanged);
}

void WorkProgressChanged(object sender,
ProgressChangedEventArgs e)
{
// Invoke updating UI controls on the form
BulkProcessingStatusForm statusForm = new BulkProcessingStatusForm
();
statusForm.UpdateUIcontrols(sender, e);
}
// In BulkProcessingStatusForm
private void UpdateControls(object sender, EventArgs e)
{

int lFailedReports = 0;
int lPendingReports = 0;
int lSuccessReports = 0;
int lSelectedReports = 0;

GetSummaryData(ref lFailedReports, ref lPendingReports,
ref lSuccessReports, ref lSelectedReports);

this.summaryReportsTextBox.Text =
lSelectedReports.ToString();
this.summaryFailedTextBox.Text =
lFailedReports.ToString();

this.summaryActionLabel.Text = string.Format("{0}{1}:",
Action.Substring(0, 1).ToUpper(), Action.Substring(1));
this.summaryActionTextBox.Text =
lSuccessReports.ToString();
this.summaryActionTextBox.Left = summaryActionLabel.Left +
summaryActionLabel.Width + 6;

}

Dec 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Jason Jacob | last post by:
To all, I have a GUI program (use c#), and I have create a Thread for loading some bulk data, I also arrange the GUI program like this: 1) load a form showing "Wait for loading..." etc 2) a...
9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
2
by: Joe Fetters via .NET 247 | last post by:
Have googled and read the VS.NET documentation can't seem to getthe answer to the following. Environment: Framework 1.1 VB.NET WinForm Access database Using all automagic tools (DataAdapter...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
13
by: Lyners | last post by:
I have a web page writen in ASP.NET that contains some javascript so that when a user presses a button, or edits a certain field in a datagrid, another cell in the datagrid is filled with a value....
1
by: mursyidatun ismail | last post by:
Dear all, database use: Ms Access. platform: .Net i'm trying to update a record/records in a table called t_doctors by clicking da edit link provided in the database. when i ran through da...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.