在后面的代码中调用类方法。 [英] call class method in code behind.

查看:72
本文介绍了在后面的代码中调用类方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班级沟通经理。我有这个功能。



  public   void  SetPortNameValues( object  obj)
{
< span class =code-keyword> foreach ( string str in SerialPort.GetPortNames( ))
{
((ComboBox)obj).Items.Add(str);
}
}



在form1.cs [design]中我有 combobox1 。< br $>


我想调用 SetPortNameValues 方法i form1_load 事件。



有人可以帮帮我吗?

解决方案

嗨Bunty,



我不知道如何在windows应用程序中,

但是通过使用类然后它对所有应用程序来说都很常见

只需创建一个对象类通信管理器(使用你的命名约定)。
在form1_load()中这样的


 communication_manager commMgr = new communication_manager(); //已添加因为我不想取消命名约定



并且使用该obj你可以访问这样的方法

 commMgr.SetPortNameValues( object  obj); 



希望这会对你有所帮助。



问候,

RK


首先,在表单Load事件中添加一个事件处理程序(最简单的方法:在设计视图中双击表单)



这会创建代码看起来像这样:



  public   void  form1_load( object  sender,EventArgs e){

}





此外,您需要创建 communicationmanager 的实例,并且需要将该调用添加到事件中:

  public   void  form1_load( object  sender,EventArgs e){
var manager = new communicationmanager();
manager.SetPortNameValues(combobox1);
}


I have a class communication manager.in that i have this function.

public void SetPortNameValues(object obj)
{
   foreach (string str in SerialPort.GetPortNames())
   {
      ((ComboBox)obj).Items.Add(str);
   }
}


In form1.cs[design] I have combobox1.

I want to call SetPortNameValues method i form1_load event.

can anyone help me?

解决方案

Hi Bunty,

I don't know how in windows application,
but by using class then its common for all application so
just create an object for the class communication manager(used your naming convention).
like this in form1_load()

communication_manager commMgr = new communication_manager();//added underscore since i dont want to void the naming convention


and with that obj you can access the method like this

commMgr.SetPortNameValues(object obj);


Hope this helps you a bit.

Regards,
RK


Firstly, add an event handler to the form Load event (easiest way to do this: Double Click on the Form in the design view)

This creates code which looks like that:

public void form1_load(object sender, EventArgs e) {

}



In addition you need to create an instance of your communicationmanager and you need to add that call to the event:

public void form1_load(object sender, EventArgs e) {
   var manager = new communicationmanager();
   manager.SetPortNameValues(combobox1);
}


这篇关于在后面的代码中调用类方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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