首页 > 开发 > 综合 > 正文

VB控件在C#中的使用

2024-07-21 02:26:17
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,

第一步:在c#的工具条右击,在右键菜单上单击“选择项”选项,如下图

第二步:在com组件中,选中所需的控件即可(这点和vb中完全一样)

 

这时候,com控件和c#控件就一样使用了。但是,vb中的一些特有的命令,如动态数组和c#有很大不同,

请参考如下代码

vb代码:

  dim lngdata( ) as long

call cp243client1.readdata(1, lngdata(), 3,cp_b, cp_v)

c#代码:

   system.array lngdata=new int32[1024];

   cp243tcps.cp241len  a=cp243tcps.cp241len.cp_b;
   cp243tcps.cp241type b=cp243tcps.cp241type.cp_v;
      
   cp243.readdata(1, ref lngdata, 3, ref a,ref b) ;

---------------------------------

附:c#使用cp243控件的源码

---------------------------------

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;


namespace cp243
{
 /// <summary>
 /// form1 的摘要说明。
 /// </summary>
 public class frmtest : system.windows.forms.form
 {
  private system.windows.forms.button cmdconn;
  private system.windows.forms.picturebox picflag;
  private axcp243tcps.axcp243client cp243;
  private system.windows.forms.button cmdclose;
  private system.windows.forms.textbox txtdata;
  private system.windows.forms.checkbox chkauto;
  private system.windows.forms.timer timer1;
  private system.windows.forms.button cmdread;
  private system.windows.forms.label label1;
  private system.windows.forms.textbox txtip;
  private system.windows.forms.label label2;
  private system.windows.forms.textbox txtaddr;
  private system.windows.forms.button cmdwrite;
  private system.componentmodel.icontainer components;

  public frmtest()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();

   //
   // todo: 在 initializecomponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {
   this.components = new system.componentmodel.container();
   system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(frmtest));
   this.cp243 = new axcp243tcps.axcp243client();
   this.cmdconn = new system.windows.forms.button();
   this.picflag = new system.windows.forms.picturebox();
   this.cmdclose = new system.windows.forms.button();
   this.txtdata = new system.windows.forms.textbox();
   this.chkauto = new system.windows.forms.checkbox();
   this.timer1 = new system.windows.forms.timer(this.components);
   this.cmdread = new system.windows.forms.button();
   this.label1 = new system.windows.forms.label();
   this.txtip = new system.windows.forms.textbox();
   this.label2 = new system.windows.forms.label();
   this.txtaddr = new system.windows.forms.textbox();
   this.cmdwrite = new system.windows.forms.button();
   ((system.componentmodel.isupportinitialize)(this.cp243)).begininit();
   this.suspendlayout();
   //
   // cp243
   //
   this.cp243.enabled = true;
   this.cp243.location = new system.drawing.point(168, 40);
   this.cp243.name = "cp243";
   this.cp243.ocxstate = ((system.windows.forms.axhost.state)(resources.getobject("cp243.ocxstate")));
   this.cp243.size = new system.drawing.size(143, 101);
   this.cp243.tabindex = 0;
   //
   // cmdconn
   //
   this.cmdconn.location = new system.drawing.point(240, 8);
   this.cmdconn.name = "cmdconn";
   this.cmdconn.size = new system.drawing.size(56, 24);
   this.cmdconn.tabindex = 1;
   this.cmdconn.text = "连接";
   this.cmdconn.click += new system.eventhandler(this.cmdconn_click);
   //
   // picflag
   //
   this.picflag.backcolor = system.drawing.color.red;
   this.picflag.borderstyle = system.windows.forms.borderstyle.fixedsingle;
   this.picflag.location = new system.drawing.point(8, 8);
   this.picflag.name = "picflag";
   this.picflag.size = new system.drawing.size(24, 24);
   this.picflag.tabindex = 2;
   this.picflag.tabstop = false;
   //
   // cmdclose
   //
   this.cmdclose.location = new system.drawing.point(304, 8);
   this.cmdclose.name = "cmdclose";
   this.cmdclose.size = new system.drawing.size(56, 24);
   this.cmdclose.tabindex = 3;
   this.cmdclose.text = "断开";
   this.cmdclose.click += new system.eventhandler(this.cmdclose_click);
   //
   // txtdata
   //
   this.txtdata.location = new system.drawing.point(88, 176);
   this.txtdata.name = "txtdata";
   this.txtdata.size = new system.drawing.size(272, 21);
   this.txtdata.tabindex = 4;
   this.txtdata.text = "";
   //
   // chkauto
   //
   this.chkauto.location = new system.drawing.point(304, 144);
   this.chkauto.name = "chkauto";
   this.chkauto.size = new system.drawing.size(64, 24);
   this.chkauto.tabindex = 5;
   this.chkauto.text = "连续读";
   this.chkauto.checkedchanged += new system.eventhandler(this.chkauto_checkedchanged);
   //
   // timer1
   //
   this.timer1.interval = 500;
   this.timer1.tick += new system.eventhandler(this.timer1_tick);
   //
   // cmdread
   //
   this.cmdread.location = new system.drawing.point(16, 144);
   this.cmdread.name = "cmdread";
   this.cmdread.size = new system.drawing.size(64, 24);
   this.cmdread.tabindex = 6;
   this.cmdread.text = "读";
   this.cmdread.click += new system.eventhandler(this.cmdread_click);
   //
   // label1
   //
   this.label1.location = new system.drawing.point(40, 13);
   this.label1.name = "label1";
   this.label1.size = new system.drawing.size(112, 16);
   this.label1.tabindex = 7;
   this.label1.text = "ip";
   //
   // txtip
   //
   this.txtip.location = new system.drawing.point(64, 10);
   this.txtip.name = "txtip";
   this.txtip.size = new system.drawing.size(152, 21);
   this.txtip.tabindex = 8;
   this.txtip.text = "192.168.0.166";
   //
   // label2
   //
   this.label2.location = new system.drawing.point(16, 181);
   this.label2.name = "label2";
   this.label2.size = new system.drawing.size(40, 16);
   this.label2.tabindex = 9;
   this.label2.text = "地址";
   //
   // txtaddr
   //
   this.txtaddr.location = new system.drawing.point(48, 176);
   this.txtaddr.name = "txtaddr";
   this.txtaddr.size = new system.drawing.size(32, 21);
   this.txtaddr.tabindex = 10;
   this.txtaddr.text = "0";
   //
   // cmdwrite
   //
   this.cmdwrite.location = new system.drawing.point(88, 144);
   this.cmdwrite.name = "cmdwrite";
   this.cmdwrite.size = new system.drawing.size(64, 24);
   this.cmdwrite.tabindex = 11;
   this.cmdwrite.text = "写";
   this.cmdwrite.click += new system.eventhandler(this.cmdwrite_click);
   //
   // frmtest
   //
   this.autoscale = false;
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(376, 214);
   this.controls.add(this.cmdwrite);
   this.controls.add(this.txtaddr);
   this.controls.add(this.label2);
   this.controls.add(this.txtip);
   this.controls.add(this.label1);
   this.controls.add(this.cmdread);
   this.controls.add(this.chkauto);
   this.controls.add(this.txtdata);
   this.controls.add(this.cmdclose);
   this.controls.add(this.picflag);
   this.controls.add(this.cmdconn);
   this.controls.add(this.cp243);
   this.name = "frmtest";
   this.startposition = system.windows.forms.formstartposition.centerscreen;
   this.text = "cp243测试";
   this.topmost = true;
   this.load += new system.eventhandler(this.frmtest_load);
   ((system.componentmodel.isupportinitialize)(this.cp243)).endinit();
   this.resumelayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [stathread]
  static void main()
  {
   application.run(new frmtest());
  }

  private void cmdconn_click(object sender, system.eventargs e)
  {
   int lngret;
   string strip=txtip.text;
   int intport=102;
            //cp243.remotehost=str;
            //cp243.remoteport =intport;
   lngret=cp243.openconnect(ref(strip),ref(intport));
   if(lngret==0)
   {   
    picflag.backcolor=system.drawing.color.green;
   }
   else
   {
    picflag.backcolor=system.drawing.color.red ;
   }
 
  }

  private void cmdclose_click(object sender, system.eventargs e)
  {
    cp243.closeconnect();  
    picflag.backcolor=system.drawing.color.red ;
  }

  private void chkauto_checkedchanged(object sender, system.eventargs e)
  {
     timer1.enabled= chkauto.text=="0"? false:true;

  }

  private void cmdread_click(object sender, system.eventargs e)
  {
   system.array lngdata=new int32[1024];

   cp243tcps.cp241len  a=cp243tcps.cp241len.cp_b;
   cp243tcps.cp241type b=cp243tcps.cp241type.cp_v;
      
   if (cp243.readdata(convert.toint32(txtaddr.text), ref lngdata, 3, ref a,ref b) == 0)
   {
    txtdata.text =lngdata.getvalue(0).tostring();
   }
 
  }
  private void cmdwrite_click(object sender, system.eventargs e)
  {
   system.array lngdata=new int32[1024];

   cp243tcps.cp241len  a=cp243tcps.cp241len.cp_b;
   cp243tcps.cp241type b=cp243tcps.cp241type.cp_v;
        
   lngdata.setvalue(convert.toint32(txtdata.text),0);
   if (cp243.writedata(convert.toint32(txtaddr.text), ref lngdata, 1, ref a,ref b) != 0)
   {
    txtdata.text ="error";
   }
  }

  private void timer1_tick(object sender, system.eventargs e)
  {
     cmdread_click(sender,e);
  }

  private void frmtest_load(object sender, system.eventargs e)
  {
 
  }

 }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表