国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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)
  {
 
  }

 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丽水市| 天全县| 富民县| 迁安市| 余干县| 沙洋县| 义马市| 贵溪市| 黄大仙区| 章丘市| 龙陵县| 永修县| 东阳市| 荥阳市| 壤塘县| 阳信县| 天镇县| 彭阳县| 安阳县| 喀喇沁旗| 平安县| 泰顺县| 枣庄市| 梁河县| 隆化县| 余庆县| 青冈县| 定结县| 蒙自县| 湾仔区| 陇南市| 旬邑县| 会理县| 孝义市| 宜丰县| 青铜峡市| 正宁县| 张家口市| 无锡市| 汝州市| 三门县|