VB控制項在C#中的使用

來源:互聯網
上載者:User
控制項

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

 }
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.