How to: class library write back to form

Hi, I=92m new to programming and I have a 2-tier app.  A form and a
class library.  I know to call the class library I need to add a
reference to the class library project, add a Using statement and
instantiate the CL.  In the CL I want to do some processing and update
a text box back in the form.  How to update the form from the class
library?

Do I need to add a reference from the CL to the EXE project?  And do I
need to instantiate the form to be able to update the textbox?

Thanks much!
MP
0
mickieparis
12/18/2009 11:29:31 AM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

16 Replies
891 Views

Similar Articles

[PageSpeed] 55

Also, just to be a little clearer, I'm not talking about returning a
value, I mean, in the class library in a loop I want to update a text
box on the form for each iteration thru the loop.  Thanks!

0
mickieparis
12/18/2009 11:48:07 AM

"mickieparis" wrote:

> Hi, I’m new to programming and I have a 2-tier app.  A form and a
> class library.  I know to call the class library I need to add a
> reference to the class library project, add a Using statement and
> instantiate the CL.  In the CL I want to do some processing and update
> a text box back in the form.  How to update the form from the class
> library?
> 
> Do I need to add a reference from the CL to the EXE project?  And do I
> need to instantiate the form to be able to update the textbox?
> 
> Thanks much!
> MP
> .
> 

Your class should raise an event with messages as it processes things.  What 
ever the client is then, if it desires, can handle the events as needed.  
This way, if you have a command line client, it can write the messages to a 
console.out.  If the client is a form, it can put the message in a label.  If 
the client is a service, then it may log the message to some log file.

In other words, never tie your class to a single client.

Mike
0
Utf
12/18/2009 1:34:01 PM
does it matter how frequently the event is raised?  what if I need to
raise the event every second or even less??

0
mickieparis
12/18/2009 2:20:25 PM
"mickieparis" <mickieparis@gmail.com> wrote in message 
news:16e1dca0-27f8-4cf8-b561-c3aa64f2c658@o28g2000yqh.googlegroups.com...

> does it matter how frequently the event is raised?  what if I need to
> raise the event every second or even less??

Raising an event is really little more than calling a method. 


0
Jeff
12/18/2009 2:40:01 PM
On 18/12/2009 in message 
<16e1dca0-27f8-4cf8-b561-c3aa64f2c658@o28g2000yqh.googlegroups.com> 
mickieparis wrote:

>does it matter how frequently the event is raised?  what if I need to
>raise the event every second or even less??

If you write to a label or text box when handling the event you will need 
to call Refresh() on the label/text box or it won't get updated until the 
processing is finished.
It's probably best to experiment with frequency of calling the event.

-- 
Jeff Gaines Dorset UK
By the time you can make ends meet they move the ends
0
Jeff
12/18/2009 2:51:26 PM

"mickieparis" wrote:

> does it matter how frequently the event is raised?  what if I need to
> raise the event every second or even less??
> 
> .
> 

It does not matter how frequently the class raises the event.  The client, 
if swamped with messages can choose to ignore some (ie, not update the 
display).  The class should be in charge of how frequently the updates are 
sent, not the client.

By the way, I frequently have updates to a client on the same cpu going 
every 250 ms, so 1 second or less updates is not overwhelming.

Mike
0
Utf
12/18/2009 3:06:01 PM
can someone point me to some code examples where a class library (dll)
is sending events to a form.  I understand better when I read code.
Thanks!

MP

0
mickieparis
12/18/2009 4:29:06 PM
mickieparis <mickieparis@gmail.com> wrote in news:4005a9d1-c974-474f-
9b8c-a2a4f3ff920b@n35g2000yqm.googlegroups.com:

> Hi, I'm new to programming and I have a 2-tier app.  A form and a
> class library.  I know to call the class library I need to add a
> reference to the class library project, add a Using statement and
> instantiate the CL.  In the CL I want to do some processing and update
> a text box back in the form.  How to update the form from the class
> library?
> 
> Do I need to add a reference from the CL to the EXE project?  And do I
> need to instantiate the form to be able to update the textbox?


The form controls the library. Don't try to create a loop where both are 
controlling each other (if you can accomplish it at all). 

You are going to one of two things:

If synchronous in nature, you call a CL routine, from the form, and then 
take the answer and update in your form code.

If you want an asynchronous operations (so the class library can "tell 
the form it is ready") then use events.

Peace and Grace,

-- 
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
|      Think outside the box!             |
*******************************************
0
Gregory
12/18/2009 4:38:34 PM
mickieparis <mickieparis@gmail.com> wrote in news:f3d2ab2d-2d86-44aa-
97c0-e1da39284b0d@s20g2000yqd.googlegroups.com:

> Also, just to be a little clearer, I'm not talking about returning a
> value, I mean, in the class library in a loop I want to update a text
> box on the form for each iteration thru the loop.  Thanks!

No, you don't want to do this. You think you do, but this is a disaster 
waiting to happen.

You can add an event handler to the form that updates and then have the 
CL fire off an event, but you don't want the library controlling the 
form.

Peace and Grace,

-- 
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
|      Think outside the box!             |
*******************************************
0
Gregory
12/18/2009 4:40:41 PM
Gregory,
just to make sure we're on the same page...in the class library, while
in a loop, I want to fire off an event each time thru the loop and I
want the Form class to subscribe to that event and update the UIs on
the form.  The CL won't be updating the UIs directly.  This is ok
right?

0
mickieparis
12/18/2009 5:31:12 PM

"mickieparis" wrote:

> can someone point me to some code examples where a class library (dll)
> is sending events to a form.  I understand better when I read code.
> Thanks!
> 
> MP
> 
> .
> 

You need to stop thinking about the class sending events to a form.  The 
class is simply broadcasting an event.  This is similar to a radio/tv station 
broadcasting the signal.  The radio or tv set somewhere else sets its intent 
to listen for the event.  The tv/radio station does not need to know that at 
this moment, they need to send a program out to 57 homes.  They just 
broadcast it.  That's how csharp events work.

Here is a link to a simple example that should get you started.

http://www.codeproject.com/KB/cs/simplesteventexample.aspx

Mike

0
Utf
12/18/2009 5:51:01 PM
Jeff Gaines wrote:
> On 18/12/2009 in message 
> <16e1dca0-27f8-4cf8-b561-c3aa64f2c658@o28g2000yqh.googlegroups.com> 
> mickieparis wrote:
> 
>> does it matter how frequently the event is raised?  what if I need to
>> raise the event every second or even less??
> 
> If you write to a label or text box when handling the event you will 
> need to call Refresh() on the label/text box or it won't get updated 
> until the processing is finished.

This is only true if one inappropriately runs the processing on the same 
thread that owns the form.

Assuming a task that takes long enough to run that simply setting the 
text of a Label won't be immediately apparent to the user, that task 
should be run in a different thread (e.g. by using BackgroundWorker), so 
that the UI remains responsive (including being able to immediately show 
changes to controls in the UI).

> It's probably best to experiment with frequency of calling the event.

I would say as a general rule, if the event happens less frequently than 
ten or twenty times a second, it's probably fine to just update every 
time the event occurs.  At some point, however, the frequency of the 
updates is not only faster than the updates can occur on-screen, they 
are also faster than the user could even perceive them anyway.

Ten to twenty times per second is already in that ballpark, but not 
frequent enough to cause a serious performance issue in most cases. 
Updating much more frequently than that will cause a serious slowdown in 
throughput and should be avoided for that reason.

Pete
0
Peter
12/18/2009 5:57:39 PM
"mickieparis" <mickieparis@gmail.com> wrote in message 
news:cf7d9b24-00a4-43a4-8b35-c77262fc9450@k19g2000yqc.googlegroups.com...

> just to make sure we're on the same page...in the class library, while
> in a loop, I want to fire off an event each time thru the loop and I
> want the Form class to subscribe to that event and update the UIs on
> the form.  The CL won't be updating the UIs directly.  This is ok
> right?

It's not only okay, it's EXACTLY what you want.


0
Jeff
12/18/2009 6:01:04 PM
mickieparis wrote:
> can someone point me to some code examples where a class library (dll)
> is sending events to a form.  I understand better when I read code.

It will look just like any other class raising an event.  See MSDN for 
example:
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx

The main thing that will be "special" about your particular 
implementation is that, since the event handler is trying to update a 
control, if you run the processing in a thread other than the main GUI 
thread that owns that control (and you should do that if it's in any way 
time-consuming), _and_ you don't use BackgroundWorker to run that 
process, you will need to use the Control.Invoke() method to invoke the 
code that will actually update the GUI.

If you do use BackgroundWorker, and you use the ProgressChanged event on 
BackgroundWorker rather than writing your own (which of course would be 
the main reason to use BackgroundWorker), then the event handler will 
already be executing on the correct thread, and you can update the GUI 
as you normally would.

Here's a short example, showing both the use of an event in a worker 
class to pass strings back to the client as well as the use of 
BackgroundWorker to simplify the cross-thread issues:

   class Worker
   {
     // A dummy task that will take some time to present a sequence
     // of strings to the client code
     public void TimeConsumingTask()
     {
       string[] rgstr = { "One", "Two", "Three", "Four", "Five" };

       foreach (string str in rgstr)
       {
         Thread.Sleep(1000);
         ReportMessage(str);
       }
     }

     // An event in the Worker class that client code can
     // subscribed to in order to receive the strings
     public event Action<string> ReportMessage = delegate { };
   }

   class Form1 : Form
   {
     private void button1_Click(object sender, EventArgs e)
     {
       Worker worker = new Worker();
       BackgroundWorker bw = new BackgroundWorker();

       // This event handler is executed on the background thread,
       // when we finally call BackgroundWorker.RunWorkerAsync()
       bw.DoWork += (sender, e) =>
       {
         worker.TimeConsumingTask();
       };

       // This event handler is raised by the TimeConsumingTask()
       // method and so is also executed on the background thread.
       worker.ReportMessage += (str) =>
       {
         // The second argument passed to ReportProgress() will
         // show up in the ProgressChangedEventArgs.UserState
         // property.
         bw.ReportProgress(0, str);
       }

       // This event handler is raised when the ReportProgress()
       // method is called, with BackgroundWorker automatically
       // ensuring that it's raised in the same thread where
       // the BackgroundWorker instance was created (i.e. the
       // thread that owns the Form1 instance and its controls,
       // such as the "label1" Label instance).
       bw.ProgressChanged += (sender, e) =>
       {
         label1.Text = (string)e.UserState;
       }

       // Start the background task running
       bw.RunWorkerAsync();

       // ...and then this method exits, allowing the main GUI
       // thread to keep pumping messages, responding to user
       // input and changes to the UI, such as changes to the
       // Text property of a Label control
     }
   }

Note that while the above looks lengthy, most of that length is in comments.

One alternative to the above is to have the worker class manage its own 
thread, in which case your "ReportMessage" event handler would need to 
explicitly update the "label1.Text" property, using the Control.Invoke() 
method:

   worker.ReportMessage += (str) =>
   {
     label1.Invoke((MethodInvoker)delegate
     {
       label1.Text = str;
     });
   }

Another alternative would be for the worker thread code to actually be 
in the Form1 class, rather than in a separate class.  In this case, you 
probably wouldn't bother with the ReportMessage event itself, instead 
just directly doing the work the event handler for that event would have 
done.

IMHO, the latter alternative is inferior and should be avoided.  But it 
can be done.

Pete
0
Peter
12/18/2009 6:21:26 PM
Thanks to everyone for their help!!  Much appreciated.
Michelle
0
mickieparis
12/20/2009 5:33:55 PM
mickieparis <mickieparis@gmail.com> wrote in news:cf7d9b24-00a4-43a4-
8b35-c77262fc9450@k19g2000yqc.googlegroups.com:

> just to make sure we're on the same page...in the class library, while
> in a loop, I want to fire off an event each time thru the loop and I
> want the Form class to subscribe to that event and update the UIs on
> the form.  The CL won't be updating the UIs directly.  This is ok
> right?
> 

That is what you want. The UI, since it drives the process, should be 
alerted when something changes and handle the changes itself. if you do 
it any other way, you end up with too many chiefs and not enough indians 
- so to speak.

Peace and Grace,

-- 
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
|      Think outside the box!             |
*******************************************
0
Gregory
12/21/2009 10:26:36 PM
Reply:

Similar Artilces:

cmbo box on subform fills fields on form when chkbx is yes
I have a multi-tabbed form that details bid data ranging from pricing to contact info to departmental notes. Page two has a subform listing general contractors to whom we bid. One bid may have 10-15 contractors listed. When a purchase order is issued from one of these contractors I select a checkbox indicating that GC. I would like the opening page on this tabbed form to show the address and contact info for that GC when the checkbox is yes. Is this possible? -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-formscoding/200803/1 On Mar 17, 3:04=A0p...

how to hide standardnavigation in form
Hi, How can I hide the standard recordnavigation in the bottom of a form? Susan Open the form in design view. Right-click the small square in the upper left corner of the form and select Properties. On the Format tab find "Navigation Buttons". Change that option to "No". "susan" wrote: > Hi, > > How can I hide the standard recordnavigation in the bottom of a form? > > Susan > > > On Feb 27, 7:43=A0am, "susan" <duff...@hetnet.nl> wrote: > Hi, > > How can I hide the standard recordnavigation in the bo...

Calculated Field in form to table
Can you take a calculated field from a form and pull it in to a table?? ...

Visual Basic forms
Please Help, There must be a million references to what I'm asking but I can't seem to find it. All I'd like to do is create a form in Visual Basics which contains a combo box, aka a dropdown list, but I am having a difficult time trying to figure out how to tie in the data into the combo box which would of course display the list. Can someone please help. Your help will be greatly appreciated. Is there a way to call the information from a current list on a worksheet? If so how? But it needs to be in VBA. Set the Combo's RowSource property to the address of the wo...

table and form not calculated in synch
Help says to open a piviot table but I am having trouble making my columns add up to total for each indivudual ID. The totals are being calculated only in th e form view which does not up date the original table and does not transfer to a report. How can I get a feild to calculate specific feilds so that I can run these reports? ...

moving contacts and email back to Express 6
Completed a clean install on an XP. Would like to move some messages and contact list from my Vista. So far no luck and haven't found anything googling, any suggestions/ Drag these messages into Windows folders you create on the Desktop from the WinMail folder tree folders. Copy these folders to the XP Desktop. Open the folders | Select All and drag them into an OE folder in the folder tree. No need to mess with the dbx files. -- Bruce Hagen MS-MVP [Mail] Imperial Beach, CA "Jeff D." <jddieterle@hughes.net> wrote in message n...

PowerPoint crashing while using Slide Library
What: I wanted a way to have many individuals owning and updating content, as well as one place to display all of the content so I decided a PowerPoint presentation built off a slide library would be an effective way to facilitate this. Whats the issue: I wanted one slide to be the overview slide showing all the content within, and links to the individual slides with the actual content. Creating these links and hitting save crashes powerpoint. (Note: I tried the same steps with a PowerPoint presentation that was not created from a slide library and it did not crash.) He...

writing a service
Hello NG, I have a server application running in the background. Now I want my programme to start when rebooting the pc. I don't want the user to log in so that the programme starts. I use Windows XP or Windows 2000. I think I must programme my application as a service. Do you know how to write a service and install it in Windows? Thanks for help, Guido THere are numerous good articles in the MSDN on how to write a service. You should start there. joe On Tue, 22 May 2007 11:47:45 +0200, "Guido Franzke" <guidof73@yahoo.de> wrote: >Hello NG, > >I have a ...

post to web forms from within a program
Does anybody know of a way to post data collected from within my vc++ app to a web from on my web site? It's just a basic html form that saves the data to a csv file. I've been looking all over the web for articles on this and haven't found anything. I may just not be looking in the right place. thanks in advance, ed You can WinInet to achieve it. MFC provides some relevant classes like CHTTPFile (CInternetFile based) classes. Another alternative might be to use IE/IWebBrowser2 object model to post the data. -- Ajay Kalra [MVP - VC++] ajaykalra@yahoo.com "ed"...

Fill Form And Get $5000 To Your Home.
Fill Form And Get $5000 To Your Home At http://lifeisbeatiful.co.cc Due to high security risks,i have hidden the cheque link in an image. in that website on left side below search box, click on image and enter your name and address where you want to receive your cheque. please don't tell to anyone. ...

How do I limit the repositioning of FORM
HI ALL CAN ANY ONE TELL ME THAT "HOW DO I LIMIT THE REPOSITIONING OF A FORM. Arbind ...

Strange barcode like lines appearing in all forms
All, This morning I went to update a form and when I opened the form in Design mode I noticed what looked like a bunch of small black squares containing barcode images of some sort. Even when I go to create a new form from scratch, these images show up. Anyone have any idea what is causing this? I am using Access 2002 on a Terminal Server runing Windows 2000. Even if I open reports in design mode I get these strange characters. Thank you, Thomas Kroljic It appears that a virus (trojan.bredolab) was on the server. I removed this virus and the small little black squa...

input mask for proper case on form
What's the input mask on the form to force proper case? If user type LASTNAME, FIRSTNAME or they type lastname, firstname I want to convert to Lastname, Firstname Thanks. On Jun 17, 9:04 pm, "Song Su" <csit...@hotmail.com> wrote: > What's the input mask on the form to force proper case? > > If user type LASTNAME, FIRSTNAME > or they type lastname, firstname > > I want to convert to Lastname, Firstname > > Thanks. Private Sub Text1_AfterUpdate() Me.Text1 = StrConv(Me.Text1, vbUpperCase) End Sub On Jun 17, 9:27 pm, zions...@gmail.com wr...

Attach/upload/link files to a form
I'd like to create an upload button in an access form that allows the user to upload a file, but I have a few questions: 1. Is this efficient? I'm assuming that if you upload multiple files on Access, there wouldn't be a lot of space available. 2. The reason I would prefer an upload is that I don't want to use the hyperlink option because hyperlinks can get broken either by a file being accidently moved to another directory or folder or simply changing the file name. 3. If uploading files onto an Access form -is- efficient and feasible, what kind of code would ...

updating form contents in a multiuser environment
I have a little database that keeps tracks of bids at Charity auctions... sometimes, when the organisation closes a couple of silent auction tables at a time (we stagger the closings over a period of time) there gets to be a data entry bottleneck.. I decided to try adding a second laptop to the system so that we could have two data entry operators, and so far, so good.. I split the database, so that the second computer has only the front end and links to the tables in the copy of the ap on the first computer.. I link the machines with a cross over cable and all the data entry is go...

front-end/back-end VS. back-end only topology question
I have a 2003 ADS server on box1. I have a 2003 exchange server on box2 exchange (box2) is not a GC (it is a member server). can box1 be an RPC over HTTP proxy without it being an exchange server? Does the term Front-End only refer to an exchange server? Can box2 be a Back-End in a back-end only environment and not be a GC? I have been through technet for days and don't feel I'm getting a clear answer. I would like to avoid making my Exchange Server a domain controller. And I would like to avoid adding another exchange server. Thoughts? Thank you. Steve Hi, Inline: >I hav...

registration form template
I am looking for a template for a registration form--either in Word or in Publisher. Something that has Name of Organization, Participant's Name, address, phone numbers, email, parents' names, emergency contact, fees list and form of payment, signatures, etc. etc. The kind of form that parents fill out all the time for all the activities their children do! http://office.microsoft.com/en-us/results.aspx?Scope=TC&Query=registration -- Mary Sauer MS MVP http://office.microsoft.com/ http://www.msauer.mvps.org/ news://msnews.microsoft.com "Cian" <Cian@discussions...

how can i write an e-mail in outlook2003 and send it later as in .
How can I write an e-mail in outlook 2003 and send it later like in outlook express > How can I write an e-mail in outlook 2003 and send it later like in > outlook > express Start Creating your new email. In the email window click on the options button on the toolbar This brings up a 'message options' window. click on the do not deliver before check box. Set the date and time. Click on the close button. Send your email in the normal way. Make sure whatever time it's set to send the email, Outlook is open, and ready to run. Otherwise it will not send the email til...

can u add fill-in form text box to existing word docx?
I created a document and want to make it a form with fill-in boxes for text information (e.g., name, product selection, date) - i did not start it as a template, just a plain word doc. Seem to remember old Word had capability to insert form fill-in text boxes into Word docs ... thanks for any help ktapman See http://www.computorcompanion.com/LPMArticle.asp?ID=22. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. ktapman wrote: > I created a ...

error message when backing up
I am currently running Money 2003. When I close Money and try to do a back up I get an error message that this operation cannot be performed. I am running Vista Home and I have tried to opoen Money as administrator. I get the error message no matter what. I can back up to the hard disk. Any suggestions. In microsoft.public.money, LAnne wrote: >I am currently running Money 2003. When I close Money and try to do a back >up I get an error message that this operation cannot be performed. I am >running Vista Home and I have tried to opoen Money as administrator. I get ...

I need a form with 5 buttons that correspond with values 1-5
I need to create a form with 20 questions. For each question I'd like the user to check one of five buttons corresponding to the values 1-5. Then I'd like the form to total the score from 20 (1x20) to 100 (5x20). If possible I'd like to weight some questions heavier then others. Thanks for your help. Stevereddy@aol.com Hi Have the form ask one question at the time. Then you need a form with a question and 5 score buttons plus a "next" button and maybe even a "previous" button, an invisible question storage area and somewhere to collect and calculate the r...

Using list box to open forms
I have a list box that lists all the forms in my database. I would like a user to be able to double click on something in the list box, and have it open the corresponding form. Any help on how to do this would be appreciated. Thanks. On Wed, 24 Oct 2007 16:35:22 -0000, lecoughlin@gmail.com wrote: > I have a list box that lists all the forms in my database. I would > like a user to be able to double click on something in the list box, > and have it open the corresponding form. Any help on how to do this > would be appreciated. > > Thanks. Code the list box Double-Cli...

Re: OWA - SSL
for "Basic Authentication(clear text) I typed domain.com not DOMAIN not mail.domain.com ...

Main form won't stay maximized after a report closes
Greetings all~ I have a main form that I want to open maximized and stay maximized until I close the database. The problem is that every time I open a report or another form from this main page, after I close the report or form, my main form is no longer maximized. I placed this code: DoCmd.Maximize In the "On Open" event so it opens maximized. How can I KEEP it maximized? On Wed, 01 Aug 2007 22:51:18 -0000, EvilSewingMachine wrote: > Greetings all~ > I have a main form that I want to open maximized and stay > maximized until I close the database. The probl...

row and col ref are all num how can I get it back to letters?
in excel 2007 my column and row references are all numbers all of a sudden. How can I get the column references back to letters? To turn R1C1 referencing on or off in Excel 2007: 1. Click the Office Button. 2. Click Excel Options. 3. Switch to the Formulas tab. 4. Click the R1C1 reference style check box. A filled box will enable R1C1, a cleared box will disable it. 5. Click OK. "Julio" wrote: > in excel 2007 my column and row references are all numbers all of a sudden. > How can I get the column references back to letters? ...