How to save code separated from graphical user interface in C #?

I developed a C# script that opens an XLS file, parses it, and creates a list of XML files that validate them.
All the main steps of the program are recorded using the following:

 Console.WriteLine("Step Creating Xml 1... DONE!) Console.WriteLine("Step Validating Xml 1... DONE!) 

The path to the XLS file is currently hardcoded, and I am creating a tiny GUI with Windows Forms so that the user can select the XLS file and read the steps taken by the program in the TextBox .

I had no problem creating a button to open a file dialog box to select an XSL file, but then, after selecting it, I am puzzled by how to encode a part to show information about the program steps to the user.

What is the most common method for accomplishing this task while keeping the agnostic of the main GUI program?

+4
source share
3 answers

As the other answers say, raise an event that your GUI handles by showing the text of your log. This is a complete example:

1) First of all, think about what data is carried with the event, and extend the EventArgs class to define your class of event arguments. I suppose to output a line that is your log text:

 public class LogEventArgs : EventArgs { public string messageEvent {get;set;} } 

2) For semplicity, suppose your MyApplication class exposes the business method that you want to use. We will define and raise the event from here. Logging will be a private method that will raise our journal 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) The implementation of the event. To handle the event, we use a delegate, which is a description of which method declaration should be implemented in the receiver (your GUI) to consume the event and a pointer to the receiver method. We are declaring the OnLogging event. Inside the logging method, we raise an event by setting an argument with a log message. We should check the handler of the non-null event, because if there is no listener for the event, the handle will be null (a null pointer to the method of using the receiver's event).

  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) Now we need to implement an event listener and a method that uses it. Suppose you have a window form with a button that launches your business application method and a text box where the log messages are displayed. This is our form without an event listener and consumer.

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MyApplication myApp = new MyApplication(); myApp.BusinessMethod(); } } 

5) We define a consumer method that processes an event that writes in our text field log messages received by the called event. Of course, the method has the same delegate signature.

 private void MyApplication_OnLogging(object sender, LogEventArgs e) { this.textBox1.AppendText(e.messageEvent + "\n"); } 

6) Using our own C # operator, we bind the OnLogging event to the event user.

 private void button1_Click(object sender, EventArgs e) { MyApplication myApp = new MyApplication(); myApp.OnLogging += new MyApplication.OnLoggingEventHandler(MyApplication_OnLogging); myApp.BusinessMethod(); } 
+3
source

Instead of writing Console.WriteLine when logging, simply raise an event on the object the graphical interface responds to:

 public delegate void LogDelegate(String _data); public class Logger { public event LogDelegate OnLog; public void Log(String _data) { if(OnLog != null) OnLog(_data); } } 

Then wherever your application uses Console.WriteLine, it simply calls this log method in this class. Your GUI OnLog event and does what it wants with it.

+2
source

I would create a private method in your form class called WriteLog or WriteOutputMessage, and inside this method I would touch the user interface, for example, by adding a new line of text to a multiline text field. thus, if in the future you want to use another control to display output, such as a grid or list, you only need to change one method.

0
source

Source: https://habr.com/ru/post/1339688/


All Articles