VB控件在C#中的使用

简介: 第一步:在C#的工具条右击,在右键菜单上单击“选择项”选项,如下图第二步:在COM组件中,选中所需的控件即可(这点和VB中完全一样) 这时候,COM控件和C#控件就一样使用了。

第一步:在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)
  {
 
  }

 }
}

相关文章
|
6月前
|
人工智能 算法 Java
后端程序员逆袭之路:巧用 AI 工具,拿下高薪 offer
在技术职场中,后端程序员面临诸多挑战,如复杂业务逻辑、繁琐代码编写与调试及持续学习压力。然而,AI 工具的兴起为后端开发带来了全新机遇。智能代码生成工具如飞算 JavaAI 可高效完成需求分析、设计与代码生成;智能调试工具如 CodeGuru 能快速定位问题;知识学习工具如 ChatGPT 助力技术提升。借助这些工具,后端程序员不仅能显著提高项目质量和效率,还能展示技术前瞻性与学习能力,拓展技能边界,从而在求职市场中脱颖而出,顺利拿下高薪 offer。
|
4月前
|
SQL 分布式计算 编译器
流批一体向量化引擎Flex
本文整理自蚂蚁集团技术专家刘勇在Flink Forward Asia 2024上的分享,聚焦流批一体向量化引擎的背景、架构及未来规划。内容涵盖向量化计算的基础原理(如SIMD指令)、现有技术现状,以及蚂蚁在Flink 1.18中引入的C++开发向量化计算实践。通过Flex引擎(基于Velox构建),实现比原生执行引擎更高的吞吐量和更低的成本。文章还详细介绍了功能性优化、正确性验证、易用性和稳定性建设,并展示了线上作业性能提升的具体数据(平均提升75%,最佳达14倍)。最后展望了未来规划,包括全新数据转换层、与Paimon结合及支持更多算子和SIMD函数。
275 9
流批一体向量化引擎Flex
|
前端开发 Java API
Java并发基础:CompletableFuture全面解析
CompletableFuture类使得并发任务的处理变得简单而高效,通过简洁的API,开发者能轻松创建、组合和链式调用异步操作,无需关心底层线程管理,这不仅提升了程序的响应速度,还优化了资源利用率,让复杂的并发逻辑变得易于掌控。
356 1
Java并发基础:CompletableFuture全面解析
|
存储 安全 数据挖掘
性能30%↑|阿里云AnalyticDB X AMD EPYC,数据分析步入Next Level
阿里云原生数仓 AnalyticDB for PostgreSQL 与 AMD 新一代硬件深度优化,结合全自研计算引擎及行列混合存储实现性能升级,综合性能提升30%。结合丰富的企业级能力帮助企业构建离在线一体、流批一体综合数据分析平台,采用同一引擎即可满足离线批处理、流式加工,交互式分析三种场景,在开发运维、时效性及成本上具备更高的性价比。
684 0
|
存储 Ubuntu Linux
linux系统中如何制作rootfs?详细教程
linux系统中如何制作rootfs?详细教程
401 0
|
数据采集 开发框架 搜索推荐
开题报告-基于SpringBoot的“遇见”婚恋交友平台的设计与实现
开题报告-基于SpringBoot的“遇见”婚恋交友平台的设计与实现
515 0
|
存储 Dragonfly 弹性计算
阿里云服务器8核16G配置不同实例最新收费标准价格及选择参考
阿里云8核16G配置的云服务器有多款实例规格可选,不同实例规格云服务器收费标准是不同的,目前8核16G配置收费标准最低的是经济型 e实例,收费标准为432.0/月,如果通过目前阿里云官网的活动购买8核16G配置云服务器,有经济型e、通用算力型u1、计算型c7和计算型c8y实例可选,最低价格为3084.36元1年。本文为大家详细介绍目前阿里云服务器8核16G配置的最新收费标准与活动价格和选择参考。
阿里云服务器8核16G配置不同实例最新收费标准价格及选择参考
【线性代数-基础理解】对换行列式两行(列),行列式变号
【线性代数-基础理解】对换行列式两行(列),行列式变号
1217 1
【线性代数-基础理解】对换行列式两行(列),行列式变号
|
数据建模 关系型数据库 MySQL
数据库原理及MySQL应用 | 实体联系模型
ER模型是数据库设计中被广泛用作数据建模的工具,通常用在“自顶向下”的设计方法中,是数据库应用的第一步。
610 2
数据库原理及MySQL应用 | 实体联系模型
|
人工智能 自然语言处理 机器人
阿里云智能对话机器人控制台部署与发布详细说明
智能对话机器人(Intelligent Robot)是一款基于自然语言处理(NLP)和人工智能(AI)技术,面向开发者提供智能会话能力的云服务。开发者可以使用智能对话机器人创建会话机器人,为机器人配置知识库以实现智能问答,使用对话工厂配置意图实现多轮对话与自助服务(如订单查询、物流跟踪、自助退货等),并将机器人部署在不同终端上(如网站、移动APP、智能硬件等)。本文简述智能对话机器人控制台部署与发布的详细说明。
1274 0
阿里云智能对话机器人控制台部署与发布详细说明