且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB控件在C#中的使用

更新时间:2022-09-17 08:22:52

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

 

VB控件在C#中的使用

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

 

VB控件在C#中的使用

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

请参考如下代码

VB代码:

 


  1. dim lngData( ) as long   
  2.  
  3. all CP243Client1.ReadData(1, lngData(), 3,CP_B, CP_V)  

C#代码:

  


  1. System.Array lngData=new Int32[1024];  
  2.  
  3.    CP243TCPS.CP241LEN  a=CP243TCPS.CP241LEN.CP_B;  
  4.    CP243TCPS.CP241TYPE b=CP243TCPS.CP241TYPE.CP_V;  
  5.          
  6.    cp243.ReadData(1, ref lngData, 3, ref a,ref b) ;  
  7.  

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

附:C#使用CP243控件的源码

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


  1. using System;  
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.ComponentModel;  
  5. using System.Windows.Forms;  
  6. using System.Data;  
  7.  
  8.  
  9. namespace CP243  
  10. {  
  11.  /// <summary>  
  12.  /// Form1 的摘要说明。  
  13.  /// </summary>  
  14.  public class frmTest : System.Windows.Forms.Form  
  15.  {  
  16.   private System.Windows.Forms.Button cmdConn;  
  17.   private System.Windows.Forms.PictureBox picFlag;  
  18.   private AxCP243TCPS.AxCP243Client cp243;  
  19.   private System.Windows.Forms.Button cmdClose;  
  20.   private System.Windows.Forms.TextBox txtData;  
  21.   private System.Windows.Forms.CheckBox chkAuto;  
  22.   private System.Windows.Forms.Timer timer1;  
  23.   private System.Windows.Forms.Button cmdRead;  
  24.   private System.Windows.Forms.Label label1;  
  25.   private System.Windows.Forms.TextBox txtIP;  
  26.   private System.Windows.Forms.Label label2;  
  27.   private System.Windows.Forms.TextBox txtAddr;  
  28.   private System.Windows.Forms.Button cmdWrite;  
  29.   private System.ComponentModel.IContainer components;  
  30.  
  31.   public frmTest()  
  32.   {  
  33.    //  
  34.    // Windows 窗体设计器支持所必需的  
  35.    //  
  36.    InitializeComponent();  
  37.  
  38.    //  
  39.    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码  
  40.    //  
  41.   }  
  42.  
  43.   /// <summary>  
  44.   /// 清理所有正在使用的资源。  
  45.   /// </summary>  
  46.   protected override void Dispose( bool disposing )  
  47.   {  
  48.    if( disposing )  
  49.    {  
  50.     if (components != null)   
  51.     {  
  52.      components.Dispose();  
  53.     }  
  54.    }  
  55.    base.Dispose( disposing );  
  56.   }  
  57.  
  58.   #region Windows 窗体设计器生成的代码  
  59.   /// <summary>  
  60.   /// 设计器支持所需的方法 - 不要使用代码编辑器修改  
  61.   /// 此方法的内容。  
  62.   /// </summary>  
  63.   private void InitializeComponent()  
  64.   {  
  65.    this.components = new System.ComponentModel.Container();  
  66.    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmTest));  
  67.    this.cp243 = new AxCP243TCPS.AxCP243Client();  
  68.    this.cmdConn = new System.Windows.Forms.Button();  
  69.    this.picFlag = new System.Windows.Forms.PictureBox();  
  70.    this.cmdClose = new System.Windows.Forms.Button();  
  71.    this.txtData = new System.Windows.Forms.TextBox();  
  72.    this.chkAuto = new System.Windows.Forms.CheckBox();  
  73.    this.timer1 = new System.Windows.Forms.Timer(this.components);  
  74.    this.cmdRead = new System.Windows.Forms.Button();  
  75.    this.label1 = new System.Windows.Forms.Label();  
  76.    this.txtIP = new System.Windows.Forms.TextBox();  
  77.    this.label2 = new System.Windows.Forms.Label();  
  78.    this.txtAddr = new System.Windows.Forms.TextBox();  
  79.    this.cmdWrite = new System.Windows.Forms.Button();  
  80.    ((System.ComponentModel.ISupportInitialize)(this.cp243)).BeginInit();  
  81.    this.SuspendLayout();  
  82.    //   
  83.    // cp243  
  84.    //   
  85.    this.cp243.Enabled = true;  
  86.    this.cp243.Location = new System.Drawing.Point(168, 40);  
  87.    this.cp243.Name = "cp243";  
  88.    this.cp243.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("cp243.OcxState")));  
  89.    this.cp243.Size = new System.Drawing.Size(143, 101);  
  90.    this.cp243.TabIndex = 0;  
  91.    //   
  92.    // cmdConn  
  93.    //   
  94.    this.cmdConn.Location = new System.Drawing.Point(240, 8);  
  95.    this.cmdConn.Name = "cmdConn";  
  96.    this.cmdConn.Size = new System.Drawing.Size(56, 24);  
  97.    this.cmdConn.TabIndex = 1;  
  98.    this.cmdConn.Text = "连接";  
  99.    this.cmdConn.Click += new System.EventHandler(this.cmdConn_Click);  
  100.    //   
  101.    // picFlag  
  102.    //   
  103.    this.picFlag.BackColor = System.Drawing.Color.Red;  
  104.    this.picFlag.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;  
  105.    this.picFlag.Location = new System.Drawing.Point(8, 8);  
  106.    this.picFlag.Name = "picFlag";  
  107.    this.picFlag.Size = new System.Drawing.Size(24, 24);  
  108.    this.picFlag.TabIndex = 2;  
  109.    this.picFlag.TabStop = false;  
  110.    //   
  111.    // cmdClose  
  112.    //   
  113.    this.cmdClose.Location = new System.Drawing.Point(304, 8);  
  114.    this.cmdClose.Name = "cmdClose";  
  115.    this.cmdClose.Size = new System.Drawing.Size(56, 24);  
  116.    this.cmdClose.TabIndex = 3;  
  117.    this.cmdClose.Text = "断开";  
  118.    this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);  
  119.    //   
  120.    // txtData  
  121.    //   
  122.    this.txtData.Location = new System.Drawing.Point(88, 176);  
  123.    this.txtData.Name = "txtData";  
  124.    this.txtData.Size = new System.Drawing.Size(272, 21);  
  125.    this.txtData.TabIndex = 4;  
  126.    this.txtData.Text = "";  
  127.    //   
  128.    // chkAuto  
  129.    //   
  130.    this.chkAuto.Location = new System.Drawing.Point(304, 144);  
  131.    this.chkAuto.Name = "chkAuto";  
  132.    this.chkAuto.Size = new System.Drawing.Size(64, 24);  
  133.    this.chkAuto.TabIndex = 5;  
  134.    this.chkAuto.Text = "连续读";  
  135.    this.chkAuto.CheckedChanged += new System.EventHandler(this.chkAuto_CheckedChanged);  
  136.    //   
  137.    // timer1  
  138.    //   
  139.    this.timer1.Interval = 500;  
  140.    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);  
  141.    //   
  142.    // cmdRead  
  143.    //   
  144.    this.cmdRead.Location = new System.Drawing.Point(16, 144);  
  145.    this.cmdRead.Name = "cmdRead";  
  146.    this.cmdRead.Size = new System.Drawing.Size(64, 24);  
  147.    this.cmdRead.TabIndex = 6;  
  148.    this.cmdRead.Text = "读";  
  149.    this.cmdRead.Click += new System.EventHandler(this.cmdRead_Click);  
  150.    //   
  151.    // label1  
  152.    //   
  153.    this.label1.Location = new System.Drawing.Point(40, 13);  
  154.    this.label1.Name = "label1";  
  155.    this.label1.Size = new System.Drawing.Size(112, 16);  
  156.    this.label1.TabIndex = 7;  
  157.    this.label1.Text = "IP";  
  158.    //   
  159.    // txtIP  
  160.    //   
  161.    this.txtIP.Location = new System.Drawing.Point(64, 10);  
  162.    this.txtIP.Name = "txtIP";  
  163.    this.txtIP.Size = new System.Drawing.Size(152, 21);  
  164.    this.txtIP.TabIndex = 8;  
  165.    this.txtIP.Text = "192.168.0.166";  
  166.    //   
  167.    // label2  
  168.    //   
  169.    this.label2.Location = new System.Drawing.Point(16, 181);  
  170.    this.label2.Name = "label2";  
  171.    this.label2.Size = new System.Drawing.Size(40, 16);  
  172.    this.label2.TabIndex = 9;  
  173.    this.label2.Text = "地址";  
  174.    //   
  175.    // txtAddr  
  176.    //   
  177.    this.txtAddr.Location = new System.Drawing.Point(48, 176);  
  178.    this.txtAddr.Name = "txtAddr";  
  179.    this.txtAddr.Size = new System.Drawing.Size(32, 21);  
  180.    this.txtAddr.TabIndex = 10;  
  181.    this.txtAddr.Text = "0";  
  182.    //   
  183.    // cmdWrite  
  184.    //   
  185.    this.cmdWrite.Location = new System.Drawing.Point(88, 144);  
  186.    this.cmdWrite.Name = "cmdWrite";  
  187.    this.cmdWrite.Size = new System.Drawing.Size(64, 24);  
  188.    this.cmdWrite.TabIndex = 11;  
  189.    this.cmdWrite.Text = "写";  
  190.    this.cmdWrite.Click += new System.EventHandler(this.cmdWrite_Click);  
  191.    //   
  192.    // frmTest  
  193.    //   
  194.    this.AutoScale = false;  
  195.    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  
  196.    this.ClientSize = new System.Drawing.Size(376, 214);  
  197.    this.Controls.Add(this.cmdWrite);  
  198.    this.Controls.Add(this.txtAddr);  
  199.    this.Controls.Add(this.label2);  
  200.    this.Controls.Add(this.txtIP);  
  201.    this.Controls.Add(this.label1);  
  202.    this.Controls.Add(this.cmdRead);  
  203.    this.Controls.Add(this.chkAuto);  
  204.    this.Controls.Add(this.txtData);  
  205.    this.Controls.Add(this.cmdClose);  
  206.    this.Controls.Add(this.picFlag);  
  207.    this.Controls.Add(this.cmdConn);  
  208.    this.Controls.Add(this.cp243);  
  209.    this.Name = "frmTest";  
  210.    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;  
  211.    this.Text = "CP243测试";  
  212.    this.TopMost = true;  
  213.    this.Load += new System.EventHandler(this.frmTest_Load);  
  214.    ((System.ComponentModel.ISupportInitialize)(this.cp243)).EndInit();  
  215.    this.ResumeLayout(false);  
  216.  
  217.   }  
  218.   #endregion  
  219.  
  220.   /// <summary>  
  221.   /// 应用程序的主入口点。  
  222.   /// </summary>  
  223.   [STAThread]  
  224.   static void Main()   
  225.   {  
  226.    Application.Run(new frmTest());  
  227.   }  
  228.  
  229.   private void cmdConn_Click(object sender, System.EventArgs e)  
  230.   {  
  231.    int lngRet;  
  232.    string strIP=txtIP.Text;  
  233.    int intPort=102;  
  234.             //cp243.RemoteHost=str;  
  235.             //cp243.RemotePort =intPort;  
  236.    lngRet=cp243.OpenConnect(ref(strIP),ref(intPort));  
  237.    if(lngRet==0)  
  238.    {      
  239.     picFlag.BackColor=System.Drawing.Color.Green;   
  240.    }  
  241.    else 
  242.    {  
  243.     picFlag.BackColor=System.Drawing.Color.Red ;  
  244.    }  
  245.    
  246.   }  
  247.  
  248.   private void cmdClose_Click(object sender, System.EventArgs e)  
  249.   {  
  250.     cp243.CloseConnect();     
  251.     picFlag.BackColor=System.Drawing.Color.Red ;  
  252.   }  
  253.  
  254.   private void chkAuto_CheckedChanged(object sender, System.EventArgs e)  
  255.   {  
  256.      timer1.Enabled= chkAuto.Text=="0"false:true;  
  257.  
  258.   }  
  259.  
  260.   private void cmdRead_Click(object sender, System.EventArgs e)  
  261.   {  
  262.    System.Array lngData=new Int32[1024];  
  263.  
  264.    CP243TCPS.CP241LEN  a=CP243TCPS.CP241LEN.CP_B;  
  265.    CP243TCPS.CP241TYPE b=CP243TCPS.CP241TYPE.CP_V;  
  266.          
  267.    if (cp243.ReadData(Convert.ToInt32(txtAddr.Text), ref lngData, 3, ref a,ref b) == 0)  
  268.    {  
  269.     txtData.Text =lngData.GetValue(0).ToString();   
  270.    }  
  271.     
  272.   }  
  273.   private void cmdWrite_Click(object sender, System.EventArgs e)  
  274.   {  
  275.    System.Array lngData=new Int32[1024];  
  276.  
  277.    CP243TCPS.CP241LEN  a=CP243TCPS.CP241LEN.CP_B;  
  278.    CP243TCPS.CP241TYPE b=CP243TCPS.CP241TYPE.CP_V;  
  279.            
  280.    lngData.SetValue(Convert.ToInt32(txtData.Text),0);   
  281.    if (cp243.WriteData(Convert.ToInt32(txtAddr.Text), ref lngData, 1, ref a,ref b) != 0)  
  282.    {  
  283.     txtData.Text ="ERROR";   
  284.    }  
  285.   }  
  286.  
  287.   private void timer1_Tick(object sender, System.EventArgs e)  
  288.   {  
  289.      cmdRead_Click(sender,e);  
  290.   }  
  291.  
  292.   private void frmTest_Load(object sender, System.EventArgs e)  
  293.   {  
  294.     
  295.   }  
  296.  
  297.  }  
  298. }  
  299.  
  300.  
  301.  

 
















本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/323440,如需转载请自行联系原作者