⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 用C#写的一个OPC CLIENT的工具
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using RFC1157;

namespace Snmp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Button Kids;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button Next;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button ClearErr;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.Button Subtree;
		private System.Windows.Forms.Button Go;
		private System.Windows.Forms.Button Get;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox Agent;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Button Up;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

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

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.Go = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.Kids = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.Subtree = new System.Windows.Forms.Button();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.Get = new System.Windows.Forms.Button();
			this.Next = new System.Windows.Forms.Button();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.Agent = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.ClearErr = new System.Windows.Forms.Button();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.label6 = new System.Windows.Forms.Label();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.Up = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(712, 20);
			this.textBox1.TabIndex = 14;
			this.textBox1.Text = "mgmt";
			// 
			// Go
			// 
			this.Go.Location = new System.Drawing.Point(96, 168);
			this.Go.Name = "Go";
			this.Go.Size = new System.Drawing.Size(48, 24);
			this.Go.TabIndex = 15;
			this.Go.Text = "Go";
			this.Go.Click += new System.EventHandler(this.Go_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 232);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(712, 96);
			this.label1.TabIndex = 16;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 208);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(720, 16);
			this.label2.TabIndex = 17;
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(8, 48);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(136, 82);
			this.listBox1.TabIndex = 18;
			this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
			this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_DoubleClick);
			// 
			// Kids
			// 
			this.Kids.Location = new System.Drawing.Point(160, 72);
			this.Kids.Name = "Kids";
			this.Kids.Size = new System.Drawing.Size(48, 24);
			this.Kids.TabIndex = 19;
			this.Kids.Text = "Kids";
			this.Kids.Click += new System.EventHandler(this.Kids_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 32);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(712, 16);
			this.label3.TabIndex = 20;
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(232, 48);
			this.textBox2.Multiline = true;
			this.textBox2.Name = "textBox2";
			this.textBox2.ReadOnly = true;
			this.textBox2.Size = new System.Drawing.Size(496, 96);
			this.textBox2.TabIndex = 21;
			this.textBox2.Text = "<help string>";
			// 
			// Subtree
			// 
			this.Subtree.Location = new System.Drawing.Point(152, 168);
			this.Subtree.Name = "Subtree";
			this.Subtree.Size = new System.Drawing.Size(64, 24);
			this.Subtree.TabIndex = 22;
			this.Subtree.Text = "Subtree";
			this.Subtree.Click += new System.EventHandler(this.Subtree_Click);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(328, 168);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(40, 20);
			this.textBox3.TabIndex = 23;
			this.textBox3.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(224, 168);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 24);
			this.label4.TabIndex = 24;
			// 
			// Get
			// 
			this.Get.Location = new System.Drawing.Point(280, 168);
			this.Get.Name = "Get";
			this.Get.Size = new System.Drawing.Size(40, 24);
			this.Get.TabIndex = 25;
			this.Get.Text = "Get";
			this.Get.Click += new System.EventHandler(this.Get_Click);
			// 
			// Next
			// 
			this.Next.Location = new System.Drawing.Point(384, 168);
			this.Next.Name = "Next";
			this.Next.Size = new System.Drawing.Size(40, 24);
			this.Next.TabIndex = 26;
			this.Next.Text = "Next";
			this.Next.Click += new System.EventHandler(this.Next_Click);
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 336);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(744, 22);
			this.statusBar1.TabIndex = 27;
			this.statusBar1.Text = "In the top textbox enter mgmt,  private or some other  mib section to get started" +
				"";
			// 
			// Agent
			// 
			this.Agent.Location = new System.Drawing.Point(96, 144);
			this.Agent.Name = "Agent";
			this.Agent.Size = new System.Drawing.Size(336, 20);
			this.Agent.TabIndex = 28;
			this.Agent.Text = "ict-main-s.msroot.student.paisley.ac.uk";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 144);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 16);
			this.label5.TabIndex = 29;
			this.label5.Text = "Agent:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// ClearErr
			// 
			this.ClearErr.Location = new System.Drawing.Point(440, 168);
			this.ClearErr.Name = "ClearErr";
			this.ClearErr.Size = new System.Drawing.Size(56, 24);
			this.ClearErr.TabIndex = 32;
			this.ClearErr.Text = "Clear";
			this.ClearErr.Visible = false;
			this.ClearErr.Click += new System.EventHandler(this.ClearErr_Click);
			// 
			// timer1
			// 
			this.timer1.Interval = 3000;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(440, 152);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(64, 16);
			this.label6.TabIndex = 33;
			this.label6.Text = "Community";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(504, 144);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(144, 20);
			this.textBox4.TabIndex = 34;
			this.textBox4.Text = "public";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(152, 56);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(72, 16);
			this.label7.TabIndex = 35;
			this.label7.Text = "From local..";
			// 
			// Up
			// 
			this.Up.Location = new System.Drawing.Point(160, 104);
			this.Up.Name = "Up";
			this.Up.Size = new System.Drawing.Size(48, 24);
			this.Up.TabIndex = 36;
			this.Up.Text = "Up";
			this.Up.Click += new System.EventHandler(this.Up_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(744, 358);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.Up,
																		  this.label7,
																		  this.textBox4,
																		  this.label6,
																		  this.ClearErr,
																		  this.label5,
																		  this.Agent,
																		  this.statusBar1,
																		  this.Next,
																		  this.Get,
																		  this.label4,
																		  this.textBox3,
																		  this.Subtree,
																		  this.textBox2,
																		  this.label3,
																		  this.Kids,
																		  this.listBox1,
																		  this.label2,
																		  this.label1,
																		  this.Go,
																		  this.textBox1});
			this.Name = "Form1";
			this.Text = "Simple Mib browser";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		ManagerSession sess = null;
		void GetSess()
		{
			timer1.Enabled = true;
			sess = new ManagerSession(Agent.Text,"public");
		}
		void LoseSess()
		{
			timer1.Enabled = false;
			sess = null;
		}
		void CatchSess(Exception x)
		{
				ClearErr.Tag = statusBar1.Text;
				ClearErr.Visible = true;
				Go.Enabled = false;
				Subtree.Enabled = false;
				statusBar1.Text = x.Message;
		}

		private void Go_Click(object sender, System.EventArgs e)
		{
			try 
			{
				GetSess();
				ManagerItem mi = new ManagerItem(sess,mib.OID(textBox1.Text));
				label2.Text = mib.OID(mi.Oid);
				label1.Text = mi.Value.ToString();
				LoseSess();
			} 
			catch (Exception x) 
			{
				CatchSess(x);
			}
		}

		Mgmt mib = new Mgmt();

		private void Kids_Click(object sender, System.EventArgs e)
		{
			string[] oid = textBox1.Text.Split('.');
			int j=0;
			string name = "";
			Def mo = mib.def;
			for (j=0;j<oid.Length;j++) 
			{
				Def mn = mo[oid[j]];
				if (mn==null)
					break;
				mo = mn;
				if (j!=0)
					name += ".";
				name += oid[j];
			}
			label3.Text = name;
			string[] kids = mo.Kids();
			listBox1.Items.Clear();
			for (j=0;j<kids.Length;j++)
				listBox1.Items.Add(kids[j]);
			listBox1.Tag = mo;
			listBox1.Enabled = true;
		}

		private void listBox1_DoubleClick(object sender, System.EventArgs e)
		{
			Def mo = (Def)listBox1.Tag;
			string s = listBox1.SelectedItem.ToString();
			textBox2.Text = "";
			if (mo!=null) 
			{
				mo = mo[s];
				if (mo!=null && mo.help!=null)
					textBox2.Text = mo.help.Replace("\n"," ");
			}
			textBox1.Text = label3.Text+"."+s+".0";
		}

		ManagerSubTree ms = null;

		private void Subtree_Click(object sender, System.EventArgs e)
		{
			try 
			{
				GetSess();
				if (textBox1.Text.EndsWith(".0"))
					textBox1.Text = textBox1.Text.Substring(0,textBox1.Text.Length-2);
				ms = new ManagerSubTree(sess,mib.OID(textBox1.Text));
				label4.Text = ""+ms.Length;
				textBox3.Text = "0";
				if (ms.Length>0) 
				{
					Get.Enabled = true;
					Next.Enabled = true;
				}
				LoseSess();
			} 
			catch (Exception x) 
			{
				CatchSess(x);
			}
		}

		private void Get_Click(object sender, System.EventArgs e)
		{
			int k = int.Parse(textBox3.Text);
			if (k<ms.Length)
			{
				ManagerItem mi = ms[k];
				label2.Text = mib.OID(mi.Oid);
				label1.Text = mi.Value.ToString();
			}
			else 
			{
				Get.Enabled=false;
				Next.Enabled = false;
			}
		}

		private void Next_Click(object sender, System.EventArgs e)
		{
			int k = int.Parse(textBox3.Text);
			textBox3.Text = ""+(k+1);
			Get_Click(sender,e);
		}

		private void ClearErr_Click(object sender, System.EventArgs e)
		{
			statusBar1.Text = (string)ClearErr.Tag;
			ClearErr.Visible = false;
			Go.Enabled = true;
			Subtree.Enabled = true;
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			if (sess!=null)
				sess.Close();
			timer1.Enabled = false;
		}

		private void Up_Click(object sender, System.EventArgs e)
		{
			int k = textBox1.Text.LastIndexOf('.');
			if (k>0)
				textBox1.Text = textBox1.Text.Substring(0,k);
			listBox1.Enabled = false;
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -