C#联网Windows窗体 [英] c# networking windows form

查看:103
本文介绍了C#联网Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#Windows窗体创建程序,该窗体从一台计算机获取输入,并在另一台计算机上分别输出.
例子.
我有4台计算机,即.. com1,com2,com3,com4 ..
com1是服务器,其他是客户端..
当com2输入时,输入将显示在com1上,并单独说出textbox1.
当com3输入时,该输入然后分别显示在com1上,例如textbox2
当com4输入时,输入将显示在com1上,并单独说出textbox3.

感谢您的友好答复.
可以通过[DELETED] @ yahoo.com向我发送电子邮件
希望能有一个快速的响应.. :)

[edit]除非您真的喜欢垃圾邮件,否则请不要在任何论坛中发布您的电子邮件地址!如果有人回复您,您将收到一封电子邮件,通知您-OriginalGriff [/edit]

how to create a program using c# windows form that takes input from one computer and separately ouputs on another computer..
example.
i have 4 computers namely.. com1, com2, com3, com4..
com1 is the server and the others the client..
when com2 inputs, the input is then shown on com1 separately say textbox1.
when com3 inputs, the input is then shown on com1 separately say textbox2
when com4 inputs, the input is then shown on com1 separately say textbox3.

thank you for your kind reply..
can email me at [DELETED]@yahoo.com
hoping for a quick response.. :)

[edit]Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know - OriginalGriff[/edit]

推荐答案

尝试以下操作: ^ ]

它可能不完全是您想要的,但它涵盖了大多数基本需求和功能.
Try this: Multithreaded Chat Server[^]

It probably isn''t exactly what you want, but it covers most of the basic needs and functions.


[在注释中回答后续问题,以
请参阅我最近关于此主题的答案:
由于我们有多播委托,所以为什么我们需要事件吗? [ ^ ].

以类System.Windows.Forms.Control为例.它具有方法InvokeOnClick,该方法实际上会调用Click事件,请参见:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick.aspx [ ^ ].

这样,就可以直接在基类中调用该事件,但这仅是因为该方法公开给派生类(并且仅通过protected访问修饰符用于它们)为仿真该类提供了一些后门.点击.要使用它,应该从任何控件类中创建一个派生类.

从概念上讲,这是可能的,但却是多余的.在实际的开发中,您不需要事件本身,也不需要实际的点击.您确实需要调用在调用事件时通常会调用的某些方法.因此,这正是您可以做的(而不是笨拙的子分类和使用System.Windows.Forms.Control.InvokeOnClick:
[Answering a follow-up question in the comments to Solution 3]

The question was about simulation of an event like a click event.

Technically, a event can be invoked only in a class where the event is declared. It is not possible to do anywhere else, not even it the derived class. This is a one of the limitations of event instances, in contrast to "regular" delegate instances, an important fool-proof feature of events.

Please see my recent answer on this topic:
Since we have multicast delegates, why do we need events?[^].

Take for example the class System.Windows.Forms.Control. It has the method InvokeOnClick which actually invokes the Click event, please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick.aspx[^].

This way, one can directly invoke the event in the base class, but only because of this method which was exposed to the derived classes (and only for them, through the protected access modifier) to provide some back door to simulation of the click. To use it, one should create a derived class from any of the control classes.

Conceptually, this is possible but redundant. In real development, you don''t need an event itself, not an actual click. You really need to call some method normally called when an event is invoked. So, this is exactly what you could do (instead of awkward sub-classing and using System.Windows.Forms.Control.InvokeOnClick:
void ProcessMyButtonClick() { /* you don't need any parameters ... */ }
//...

myButton.Click += (sender, eventArgs) => {
   ProcessMyButtonClick();
   // note that you don't need event handler parameters;
   // in other cases, you need only one of them or both...
}

//...

// and you can have the exact same effect if you call it elsewhere:
ProcessMyButtonClick(); //that's all; this is a regular method, call it a will!



上面显示的代码还以lambda形式显示了匿名事件处理程序的好处,该事件处理程序还允许进行类型推断(请注意,未指定参数的类型,但编译器从委托类型中获取它们)事件实例Click的实例,这是委托System.EventHandler<System.EventArgs>).

对于C#v.2,当既不引入lambda也不进行类型推断时,添加事件处理程序的语法会更长一些,但仍使用匿名事件处理程序:



The code shown above also shows the benefits of anonymous event handler, in the lambda form, which also allowed for type inference (note that the types of parameters are not specified, but compiler takes them from the delegate type of the event instance Click, this is the delegate System.EventHandler<System.EventArgs>).

For C# v.2, when neither lambda nor type inference were introduced, the syntax of adding an event handler would be a bit longer, but still using anonymous event handler:

myButton.Click += (object sender, System.EventArgs eventArgs) {
   ProcessMyButtonClick();
   // note that you don't need event handler parameters;
   // in other cases, you need only one of them or both...
}



我确定永远不要考虑.NET之前的v.2.0和V#2之前的C#.

现在,最初的问题是关于通过网络链接的不同主机之间的事件的调用.经过以上解释,应该清楚的是,有效地将问题简化为响应通过网络发送的某些命令而调用ProcessMyButtonClick之类的相同方法.

我要指出的是,我认为它没有太多意义.网络通信应在单独的通信层中实现,并与UI隔离.它可以是语义的或非常抽象的,而不交换某些逻辑数据和命令.您需要松散耦合,而不是强耦合.请参阅:
http://en.wikipedia.org/wiki/Loose_coupling [ http://en.wikipedia .org/wiki/Architectural_pattern_(computer_science) [^ ]):

MVVM —模型视图视图模型,
http://en.wikipedia.org/wiki/Model_View_ViewModel [> http://en.wikipedia.org/wiki/Model-view-controller [ ^ ])),

MVA —模型视图适配器,
http://en.wikipedia.org/wiki/Model–view–adapter [ ^ ],

MVP —模型视图呈现器,
> http://en.wikipedia.org/wiki/Model-view-presenter [ ^ ].
请注意这些架构的动机.如果您了解它,就可以提出更好的设计思路.

祝你好运,
—SA



I''m sure that .NET prior v.2.0 and C# prior to v.2 should not be ever considered.

Now, the original question was about invocation of event between different host linked via network. After the above explanations, is should be clear that effectively the problem is reduced to calling the same method like ProcessMyButtonClick in response to some command sent via network.

I would note, I don''t see a whole lot of sense in it. The network communication should be implemented in a separate communication layer and isolated from the UI. It could be semantic or very abstract, not exchanging some logical data and commands. You need loose coupling, as opposed to strong coupling; please see:
http://en.wikipedia.org/wiki/Loose_coupling[^].

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

Good luck,
—SA


请参阅我过去的回答以获取一些重要想法:
来自同一端口号的多个客户端 [ 解决方案4

—SA
Please see my past answer for some important ideas:
Multple clients from same port Number[^].



Please see my reply in response to the follow-up discussion:

Solution 4

—SA


这篇关于C#联网Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆