The use of VB control in C #

Source: Internet
Author: User
Control

Step One: Right-click on the toolbar in C #, and on the right-click menu select the option, as shown below

Step two: In the COM component, select the control you want (this is exactly the same as in VB)

At this point, COM controls and C # controls are used as well. However, some of the unique commands in VB, such as dynamic arrays and C # are very different,

Please refer to the following code

VB code:

Dim Lngdata () As Long

Call Cp243client1.readdata (1, Lngdata (), 3,cp_b, Cp_v)

C # code:

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);

---------------------------------

Attached: C # using the CP243 control of the source code

---------------------------------

Using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

A summary description of the


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 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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 = "Connection";
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 = "Disconnected";
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 = "continuous reading";
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 = "read";
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 = "Address";
//
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 = "Write";
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 test";
This. topmost = true;
This. Load + = new System.EventHandler (this.frmtest_load);
((System.ComponentModel.ISupportInitialize) (this.cp243)). EndInit ();
This. ResumeLayout (FALSE);

}
#endregion

<summary>
The main entry point for the application.
</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)
  {
 & nbsp 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)
{

}

}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.