winforms - How to keep the code decoupled from the GUI in C#? -


i have developed c# script opens xls file, parses , creates list of xml files validating them.
every main steps of program logged this:

console.writeline("step creating xml 1... done!) console.writeline("step validating xml 1... done!) 

the xls file path hard-coded , i'm creating tiny gui windows forms allow user select xls file , read steps made program in textbox.

i had no problem in creating button open file dialog select xsl file then, once selected, i'm puzzled on how code part show program's steps information user.

which common method accomplish task keeping core program gui agnostic?

as said in other answers, raise event gui handles showing log text. complete examples:

1) first of think informations carry event , extend eventargs class define argument event class. suppose expose string log text:

public class logeventargs : eventargs {     public string messageevent {get;set;} }         

2) semplicity suppose have myapplication class expose business method want log. define , raise event here. logging private method raise our log event.

public class myapplication {     public void businessmethod(){         this.logging("first");         system.threading.thread.sleep(1000);         this.logging("second");         system.threading.thread.sleep(3000);         this.logging("third");      } } 

3) implement event raise. handle event use delegate, both description of method declaration must implemented in receiver (your gui) consume event , pointer receiver method. declare event onlogging. inside logging method raise event setting argument log message. have check not null event handler because if there no listener event, handle null (null pointer receiver event consumer method).

    public delegate void onloggingeventhandler(object sender, logeventargs e);     public event onloggingeventhandler onlogging;      private void logging(string message)     {         logeventargs logmessage = new logeventargs();         logmessage.messageevent = message;         if (onlogging != null)         {             onlogging(this, logmessage);         }     } 

4) have implement listener of event , method consume it. suppose have windows form button launch application business method , textbox show log messages. our form without event listener , consumer.

public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void button1_click(object sender, eventargs e)     {         myapplication myapp = new myapplication();         myapp.businessmethod();     } } 

5) define consumer method handle event writing on our textbox log messages received event raised. of course method has same signature of delegate.

private void myapplication_onlogging(object sender, logeventargs e) {     this.textbox1.appendtext(e.messageevent + "\n"); } 

6) c# native operator bind our onlogging event event consumer.

private void button1_click(object sender, eventargs e)     {         myapplication myapp = new myapplication();          myapp.onlogging += new myapplication.onloggingeventhandler(myapplication_onlogging);         myapp.businessmethod();     } 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -