VB控件在C#中的使用

时间:2024-03-28 22:41:44

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

VB控件在C#中的使用

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

VB控件在C#中的使用

这时候,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)
{

}

}
}