473,387 Members | 3,684 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,387 software developers and data experts.

Call richtextbox from class

I have a class that Form1 calls to do some work. I would like to report back
progress from the class to a richtextbox, will call it m_report, on the form.
Like in the class,

Form1.m_report.Text= "Calculating variable 10";

How would I do that.

Jun 27 '08 #1
1 1670
On Fri, 25 Apr 2008 10:03:00 -0700, Dave <Da**@noWeb.comwrote:
I have a class that Form1 calls to do some work. I would like to report
back
progress from the class to a richtextbox, will call it m_report, on the
form.
Like in the class,

Form1.m_report.Text= "Calculating variable 10";
A common pattern in .NET is to declare an event in your class that does
work, to which your form class subscribes an event handler. The current
progess would be passed as an argument to the event handler; usually this
is via a class derived from EventArgs and which includes the specific
value appropriate.

If your worker class would raise the event on a thread other than the main
GUI thread, the other thing you'll need to do is, in your form's event
handler method, use the Control.Invoke() method to execute the actual
assignment to your control on the main GUI thread.

Here's the basic elements you'll need to put together:

Anywhere:

class CalculationProgressEventArgs : EventArgs
{
// poor-man's property :)
public readonly int Variable;

public CalculationProgressEventArgs(int variable)
{
Variable = variable;
}
}

delegate void CalculationProgressEventHandler(object sender,
CalculationProgressEventArgs e);

In your worker class:

event CalculationProgressEventHandler CalculationProgress;

void RaiseCalculationProgress(int variable)
{
CalculationProgressEventHandler handler = CalculationProgress;

if (handler != null)
{
handler(this, new CalculationProgressEventArgs(variable);
}
}

In your form class, wherever you initialize your worker class:

Worker worker = ...;

worker.CalculationProgress += CalculationProgressHandler;

Then the actual event handler method:

void CalculationProgressHandler(object sender,
CalculationProgressEventArgs e)
{
Invoke((MethodInvoker)delegate { m_report.Text = "Calculation
variable " + e.Variable; });
}

Notes:

-- I've followed the .NET pattern for declaring events. There is no
actual requirement that you do so. Your event can have whatever method
signature you like. If you just want to pass a single integer parameter,
you could do that if you want, and eliminate the "EventArgs"-derived class
altogether.

-- I have deviated from the .NET pattern with respect to the
cross-thread invocation. Specifically, (almost?) all of the MSDN samples
will show you checking InvokeRequired and then either performing some
operation if it's false, or calling Invoke on the same method if it's
true. I think this is a waste and overly complicates the method. As you
can see, I prefer to simply assume that InvokeRequired is true (it's not
harmful for this assumption to be wrong), and use an anonymous method as
my invoked method.

-- The method that your worker class will call when it wants to raise
the event is, of course, RaiseCalculationProgress(), passing the specific
integer value appropriate at that time. This will check to see if any
delegate has been subscribed to the event, and if so it will invoke that
delegate. Since the delegate type is actually a multi-cast delegate, at
this point all subscribed handlers will be called in sequence, on the
current thread. Often there is only one subscribed delegate, but there
could be arbitrarily many.

Hope that helps.

Pete
Jun 27 '08 #2

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

Similar topics

1
by: Henry | last post by:
I inherited a richtextbox class and tried to add the double click even handler. Unfortunatly, when I check the properties of rtbText, it doesn't contain a double click event handler. Any ideas on...
1
by: Bill Menees | last post by:
I've got a RichTextBoxEx inherited from RichTextBox, and I want to overload the TextLength property to use TextBoxBase's implementation of TextLength. But since TextBoxBase isn't my immediate base...
2
by: Martin Dew | last post by:
I have a RichTextBox called rtbOutput. I am adding lines of text using the AppendText method. What I want to do is immediately after adding this line of text make sure that this line of text is...
10
by: D Steward | last post by:
I can't seem to add a newline (/n) to get a richtextbox control to display text on successive lines. The text that I type is overwritten. How do I remedy this My example richTextBox->Multiline...
1
by: Eric | last post by:
Using the 1.1 framework. We are using a newly created instance of a RichTextBox Class in our server code to manipulate RTF that is stored in the database. To clarify, we are not using or...
6
by: andreas | last post by:
A selected text has fontstyles like bold and italic and underscore I want to remove one of them without changing the other fontstyles. How to do that? Thanks for any response
0
by: Vincent | last post by:
Dear all, I have implemented a class to export the content of RichTextBox to image in WYSISYG mode so that line breaks on the screen are the same as exported. C# Code: public struct...
9
by: James Wong | last post by:
Hi, I use the RichTextBox in my program. It will use different language in this RichTextBox (chinese and english characters), and it set the "DualFont" and use different fonts. By the way, how...
5
by: Andrus | last post by:
I use Winforms RichTextBox control to edit scripts. Scripts are plain ascii texts. When error occurs, script engine returns character position of error in code as integer. How to position...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.