VB控件在C#中的使用

简介: VB控件在C#中的使用

第一步:在C#的工具条右击,在右键菜单上单击“选择项”选项,如下图
image.png
第二步:在COM组件中,选中所需的控件即可(这点和VB中完全一样)
image.png
这时候,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)
  {
  
  }

 }
}
相关文章
|
资源调度 分布式计算 安全
YARN Capacity Scheduler容量调度器(超详细解读)
YARN Capacity Scheduler容量调度器(超详细解读)
2389 0
YARN Capacity Scheduler容量调度器(超详细解读)
|
前端开发 关系型数据库 MySQL
【实训项目】“优教”APP设计
【实训项目】“优教”APP设计
222 0
|
Linux 数据处理 数据库
探索Linux的`infocmp`命令:终端配置的得力助手
`infocmp`是Linux下用于查看和比较`terminfo`数据库中终端类型描述的命令。它显示终端的控制序列、按键映射等信息,并能找出不同终端的配置差异。通过参数如`-c`(以termcap格式输出)、`-d`(比较差异)和`-1`(单列显示)等可定制输出。例如,`infocmp $TERM`显示当前终端配置,`infocmp xterm vt100`比较两者差异。了解终端类型和选择合适输出格式是有效使用的关键。
|
人工智能 机器人 数据库
字节推出「Coze扣子」AI Bot 开发平台,无需编程,让 Chatbot 搭建更快捷、低门槛!
字节推出「Coze扣子」AI Bot 开发平台,无需编程,让 Chatbot 搭建更快捷、低门槛!
639 0
|
机器学习/深度学习 算法
【机器学习】简单解释贝叶斯公式和朴素贝叶斯分类?(面试回答)
简要解释了贝叶斯公式及其在朴素贝叶斯分类算法中的应用,包括算法的基本原理和步骤。
181 1
|
机器学习/深度学习 监控 安全
基于YOLOv5的疲劳驾驶检测系统(Python+清新界面+数据集)
基于YOLOv5的疲劳驾驶检测系统(Python+清新界面+数据集)
1617 0
基于YOLOv5的疲劳驾驶检测系统(Python+清新界面+数据集)
|
敏捷开发 Devops 测试技术
CTO干货分享:来YesDev搭建你的软件研发指标体系
YesDev是一个怎样的项目管理工具? YesDev是一站式企业研发管理、项目管理与协同办公平台,支持敏捷开发、DevOps、Scrum、硬件项目等多种迭代方式,能为企业管理者智能生成项目投入产出的数据模型,真正实现项目研发全流程数字化管理。通过YesDev可以准确掌握项目研发过程的每一个环节,覆盖从需求设计、研发进度、到上线和缺陷反馈的整个过程。
|
消息中间件 前端开发 JavaScript
SpringBoot超大文件上传,实现秒传
SpringBoot超大文件上传,实现秒传
|
SQL Oracle 关系型数据库
MySQL读取的记录和我想象的不一致——事物隔离级别和MVCC
并发的事务在运行过程中会出现一些可能引发一致性问题的现象,本篇将详细分析一下。
152 0
MySQL读取的记录和我想象的不一致——事物隔离级别和MVCC
混沌引力搜索算法(CGSA)解决三个机械工程设计问题(Matlab代码实现)
混沌引力搜索算法(CGSA)解决三个机械工程设计问题(Matlab代码实现)
104 0