Home > Net >  Winfom project, encapsulated class called form1 controls. Excuse me, how to implement.
Winfom project, encapsulated class called form1 controls. Excuse me, how to implement.

Time:09-15

My project, is also a novice, project has been finished, the.net framework winform project,
On the main interface (form1) have a richtextbox for monitoring operation, output when running the various data,

Himself. Cs file encapsulates several classes. For different operations, wants to run these classes in the process of data (such as I/O read/write failure), also reported in form1 richtextbox, excuse me how to do? At present are implemented using messagebox,

Thank you very much,

CodePudding user response:

Monitoring state output, should be managed by it's module and unified,
If you have to be in their encapsulation class called directly controls, you can use the way of passing parameters to a method to use,

In addition:
Should pay attention to your own modules in the thread,

CodePudding user response:

First the main interface (form1) object instance as a parameter to encapsulate class, then RichTextBox accessibility to the public, it is ok to use the,

CodePudding user response:

Write data sharing an object, the main form to subscribe to events can


 
Public class RunLog
{
Public static event Action MessageEvent;

Public static void WriteLine (string text)
{
If (MessageEvent!=null)
{
MessageEvent (text);
}
}
}

CodePudding user response:

This kind of problem a better solution is to:
1, design a special processing messages in the middle of the class, used to receive, store, forward, and open the corresponding event
2, all the news component, send a message to the middle class
3, all the components to extract information, to register corresponding to entrust the intermediate components, used to receive notifications

Dependencies are:
Produce the message component - & gt; Middleware
Extract the message component - & gt; Middleware

Produce the message component is not dependent on with extract messages between components,

CodePudding user response:

Richtextbox. The BeginInvoke (your class. Read the IO)

CodePudding user response:

 private void button1_Click (object sender, EventArgs e) 
{
The Test t=new Test ();
//events
T.a ct=new Action (()=& gt; {
Var STR=t.G etText ();
This. The textBox1. Text=STR;
});
//subscribe event
This. The textBox1. The BeginInvoke (t.a ct);
}

Public class Test
{
Public Action act;
//analog information
Public string GetText ()
{
Return "123";
}
}

CodePudding user response:

4th floor, such as, define an intermediate structure,

 
The class MessageHolder
{
Private MessageHolder ()
{

}
Private System. Collections. Concurrent. ConcurrentQueue MSGS=new System. Collections. Concurrent. ConcurrentQueue (a);

The public System. Collections. Concurrent. ConcurrentQueue Messages=& gt; MSGS.
Private static MessageHolder _instance.
Public static MessageHolder Instance
{
The get
{
If (_instance==null)
{
_instance=new MessageHolder ();
}
Return _instance.
}
}
The public event Action MessagePushed;

Public void Push (string MSG)
{
MSGS. The Enqueue (MSG);
If (MessagePushed!=null)
{
MessagePushed (MSG);
}
}
}

The static void Main (string [] args)
{

MessageHolder. Instance. MessagePushed +=new Action (MSG=& gt;
{

Console. WriteLine (MSG);

while (! MessageHolder. Instance. Messages. IsEmpty)
{
Var msg1="";
MessageHolder. Instance. Messages. TryDequeue (out msg1);
if (! String. IsNullOrEmpty (msg1))
Console. WriteLine (msg1);
}
});

MessageHolder. Instance. Push (" Show something.. ");
}

CodePudding user response:

Using an interface called https://www.cnblogs.com/xbingyou/p/12370909.html
  •  Tags:  
  • C#
  • Related