日期:2011-06-29 浏览次数:20376 次
第一步:在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.Wind