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

 }
}
相关文章
|
11月前
|
人工智能 前端开发 机器人
10+热门 AI Agent 框架深度解析:谁更适合你的项目?
一个合适的 Agent 框架,决定了你AI应用落地的速度与质量。选框架 ≠ 选最火! 真正能跑起来、跑得稳、跑得远的 Agent 框架,才是你的最优解。
|
18天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
6837 30
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
3天前
|
数据采集 人工智能 前端开发
让 Coding Agent 从黑盒到透明:阿里云 Agent 观测审计数据采集实践
AI Agent 规模化落地带来执行黑盒、行为难追溯、成本难度量三大难题。阿里云基于 OTel 标准,面向 Coding Agent、个人通用助理和框架型 Agent,推出 LoongSuite Pilot、插件及探针等无侵入采集方案,让 Agent 实现可看见、可分析、可审计、可治理。
605 138
|
3天前
|
人工智能 弹性计算 运维
阿里云发布堡垒机智能运维Agent,运维交互进入自然语言新时代
支持自然语言运维,提升效率与安全双保障。
1145 0
|
10天前
|
人工智能 安全 定位技术
CodeGraph深度解析 让Claude Code工具调用直降七成的核心原理与实操教程
如今以Claude Code为代表的AI编程智能体已经成为开发者日常编码、项目重构、漏洞修复的必备工具。但在长期使用过程中,几乎所有开发者都会遇到同一个明显痛点:AI虽然具备强大的代码生成与分析能力,却常常陷入盲目探索的循环中。
1171 1
|
13天前
|
存储 定位技术 数据库
CodeGraph 如何让 Claude Code减少 7 成工具调用?
CodeGraph 为 Coding Agent 提供本地代码知识图谱,把函数、类、调用链和框架路由提前整理成“项目地图”,减少盲目搜索和文件读取。它不是新 Agent,而是上下文基础设施,让 Agent 更快找到正确代码路径,平均减少 7 成工具调用。
1273 3
|
11天前
|
人工智能 弹性计算 安全
阿里云618活动时间、活动入口、优惠活动详细解读
2026年阿里云618创新加速季已全面开启,作为年度力度最大的云产品促销活动,本次大促覆盖轻量应用服务器、ECS云服务器、GPU云服务器、数据库、AI算力、安全服务、CDN等全品类产品,推出5亿元算力补贴、新用户限时秒杀、普惠满减、企业专享、免费试用、云大使返佣等多重福利,个人开发者、中小企业、AI团队均可享受专属低价。本文将系统梳理2026年阿里云618活动的完整时间节点、官方参与入口、各类优惠细则、使用规则、热门产品推荐及实操代码,帮助用户精准参与、高效省钱,以最低成本完成上云部署。
982 5