首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

用 Visual C# 實作 ICMP 協議與 Ping 功能(六):專案建立與 WinForms 介面設計

     編輯:關於C語言

建立「Visual C# 實作 Ping 命令」專案

  1. 啟動 Visual Studio .NET → 檔案 → 新增專案
  2. 專案類型:Visual C# 專案 → 範本:Windows 應用程式
  3. 名稱:Visual C#實現Ping命令
  4. 位置:E:\VS.Net項目

新增專案對話方塊

圖 05:新增專案對話方塊設定

引入必要命名空間

在 Form1.cs 頂部加入:

using System.Net;
using System.Net.Sockets;

拖曳元件並設定屬性

元件名稱用途
TextBoxtextBox1輸入目標主機名/IP
ListBoxlistBox1顯示 Ping 結果 (黑底白字)
Labellabel1提示文字「請輸入主機名:」
Buttonbutton1文字「Ping」,雙擊產生 Click 事件

手寫 InitializeComponent 完整程式碼

private void InitializeComponent()
{
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();

    this.textBox1.Location = new System.Drawing.Point(116, 14);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(148, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

    this.label1.Location = new System.Drawing.Point(12, 14);
    this.label1.Name = "label1";
    this.label1.TabIndex = 1;
    this.label1.Text = "請輸入主機名:";

    this.listBox1.BackColor = System.Drawing.SystemColors.WindowText;
    this.listBox1.ForeColor = System.Drawing.SystemColors.Window;
    this.listBox1.ItemHeight = 12;
    this.listBox1.Location = new System.Drawing.Point(6, 42);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(400, 280);
    this.listBox1.TabIndex = 2;

    this.button1.Location = new System.Drawing.Point(274, 12);
    this.button1.Name = "button1";
    this.button1.TabIndex = 3;
    this.button1.Text = "Ping";
    this.button1.Click += new System.EventHandler(this.button1_Click);

    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(410, 333);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
        this.button1, this.listBox1, this.label1, this.textBox1 });
    this.MaximizeBox = false;
    this.Name = "Form1";
    this.Text = "Visual C#實現Ping";
    this.ResumeLayout(false);
}

設計介面預覽

設計介面

圖 06:Visual C# 實現 Ping 命令 - 設計介面

介面佈局完成,下一篇將實作 IcmpPacket 類別與核心演算法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved