The point is to notify the user using the memo when a packet is received/sent in a TCP Client. The old code was extra dumb,I used a Timer that used to add text in the memo since the Timer has access to the form members,lol.
关键是在TCP客户端接收/发送数据包时使用备忘录通知用户。旧的代码是额外的愚蠢,我使用了一个用于在备忘录中添加文本的Timer,因为Timer可以访问表单成员,lol。
The old code:
旧代码:
//Memo.Text += txt + "\n";
I played with it today,this is what I've done
我今天玩它,这就是我所做的
In Form1's class
在Form1的类中
public string TextValue
{
get
{
return Memo.Text;
}
set
{
this.Memo.Text += value + "\n";
}
}
I call the code like that:
我这样调用代码:
Form1 myForm = new Form1();
myForm.TextValue = "test asdasd";
The memo modifiers are private,but that's not the problem.
备忘录修饰符是私有的,但这不是问题。
The problem is that no text is displayed on the memo when i call the code.
问题是当我调用代码时,备忘录上没有显示文本。
3 个解决方案
#1
By typing this:
输入以下内容:
Form1 myForm = new Form1();
you create a new instance of your form (Form1), but instead I guess you should use existing instance which most likely has been initialized already.
你创建了一个新的表单实例(Form1),但我想你应该使用很可能已经初始化的现有实例。
One of the ways to do it:
其中一种方法:
var form = Form.ActiveForm as Form1;
if (form != null)
{
form.TextValue = "test asdasd";
}
Though this is not very good design. Try to use custom events
instead.
虽然这不是很好的设计。请尝试使用自定义事件。
#2
Maybe you should consider publishing an event in your tcpclient. Then your form will be able to listen to this event and display proper information.
也许您应该考虑在tcpclient中发布一个事件。然后,您的表单将能够收听此事件并显示正确的信息。
#3
Assuming Memo inherits from Control and assuming you set it with the proper modifier, the problem you may be going through is that you're likely trying to set the text from a worker thread (the one that's used to run the TCP client). If that's the case, then you need to check the InvokeRequired field of your control and if true invoke a delegate that will set the text for you. Below is a short and easy C# snippet.
假设Memo继承自Control并假设您使用适当的修饰符设置它,您可能遇到的问题是您可能尝试从工作线程(用于运行TCP客户端的线程)设置文本。如果是这种情况,那么您需要检查控件的InvokeRequired字段,如果为true,则调用将为您设置文本的委托。下面是一个简短易用的C#代码段。
private void SetTextOnMemo(string txt){
if(Memo.InvokeRequired){
Memo.Invoke(SetTextOnMemo, txt);
}
else{
Memo.Text = txt;
}
}
#1
By typing this:
输入以下内容:
Form1 myForm = new Form1();
you create a new instance of your form (Form1), but instead I guess you should use existing instance which most likely has been initialized already.
你创建了一个新的表单实例(Form1),但我想你应该使用很可能已经初始化的现有实例。
One of the ways to do it:
其中一种方法:
var form = Form.ActiveForm as Form1;
if (form != null)
{
form.TextValue = "test asdasd";
}
Though this is not very good design. Try to use custom events
instead.
虽然这不是很好的设计。请尝试使用自定义事件。
#2
Maybe you should consider publishing an event in your tcpclient. Then your form will be able to listen to this event and display proper information.
也许您应该考虑在tcpclient中发布一个事件。然后,您的表单将能够收听此事件并显示正确的信息。
#3
Assuming Memo inherits from Control and assuming you set it with the proper modifier, the problem you may be going through is that you're likely trying to set the text from a worker thread (the one that's used to run the TCP client). If that's the case, then you need to check the InvokeRequired field of your control and if true invoke a delegate that will set the text for you. Below is a short and easy C# snippet.
假设Memo继承自Control并假设您使用适当的修饰符设置它,您可能遇到的问题是您可能尝试从工作线程(用于运行TCP客户端的线程)设置文本。如果是这种情况,那么您需要检查控件的InvokeRequired字段,如果为true,则调用将为您设置文本的委托。下面是一个简短易用的C#代码段。
private void SetTextOnMemo(string txt){
if(Memo.InvokeRequired){
Memo.Invoke(SetTextOnMemo, txt);
}
else{
Memo.Text = txt;
}
}