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

📄 client.cs

📁 this is a good book for the visual c#
💻 CS
📖 第 1 页 / 共 2 页
字号:
// Fig. 22.8: Client.cs
// Client for the TicTacToe program.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace TicTacToeClient
{
   /// <summary>
   /// represents a tic-tac-toe player
   /// </summary>
   public class Client : System.Windows.Forms.Form
   {
      private System.Windows.Forms.Label idLabel;

      private System.Windows.Forms.TextBox displayTextBox;

      private System.Windows.Forms.Panel panel1;
      private System.Windows.Forms.Panel panel2;
      private System.Windows.Forms.Panel panel3;
      private System.Windows.Forms.Panel panel5;
      private System.Windows.Forms.Panel panel6;
      private System.Windows.Forms.Panel panel4;
      private System.Windows.Forms.Panel panel7;
      private System.Windows.Forms.Panel panel8;
      private System.Windows.Forms.Panel panel9;

      private Square[ , ] board;
      private Square currentSquare;

      private Thread outputThread;

      private TcpClient connection;
      private NetworkStream stream;
      private BinaryWriter writer;
      private BinaryReader reader;

      private char myMark;
      private bool myTurn;

      private SolidBrush brush;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      bool done = false;

      // default constructor
      public Client()
      {
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

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

         board = new Square[ 3, 3 ];

         // create 9 Square objects and place them on the 
         // board
         board[ 0, 0 ] = new Square( panel1, ' ', 0 );
         board[ 0, 1 ] = new Square( panel2, ' ', 1 );
         board[ 0, 2 ] = new Square( panel3, ' ', 2 );
         board[ 1, 0 ] = new Square( panel4, ' ', 3 );
         board[ 1, 1 ] = new Square( panel5, ' ', 4 );
         board[ 1, 2 ] = new Square( panel6, ' ', 5 );
         board[ 2, 0 ] = new Square( panel7, ' ', 6 );
         board[ 2, 1 ] = new Square( panel8, ' ', 7 );
         board[ 2, 2 ] = new Square( panel9, ' ', 8 );

         // create a SolidBrush for writing on the Squares
         brush = new SolidBrush( Color.Black );
  
         // Make connection to sever and get the associated 
         // network stream. Start separate thread to allow this
         // program to continually update its output in textbox.      
         connection = new TcpClient( "localhost", 5000 );
         stream = connection.GetStream();

         writer = new BinaryWriter( stream );
         reader = new BinaryReader( stream );

         // start a new thread for sending and receiving messages
         outputThread = new Thread( new ThreadStart( Run ) );
         outputThread.Start();
      }

      /// <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.panel8 = new System.Windows.Forms.Panel();
         this.panel9 = new System.Windows.Forms.Panel();
         this.panel4 = new System.Windows.Forms.Panel();
         this.panel5 = new System.Windows.Forms.Panel();
         this.panel6 = new System.Windows.Forms.Panel();
         this.panel7 = new System.Windows.Forms.Panel();
         this.panel1 = new System.Windows.Forms.Panel();
         this.panel2 = new System.Windows.Forms.Panel();
         this.panel3 = new System.Windows.Forms.Panel();
         this.displayTextBox = new System.Windows.Forms.TextBox();
         this.idLabel = new System.Windows.Forms.Label();
         this.SuspendLayout();
         // 
         // panel8
         // 
         this.panel8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel8.Location = new System.Drawing.Point(136, 112);
         this.panel8.Name = "panel8";
         this.panel8.Size = new System.Drawing.Size(32, 32);
         this.panel8.TabIndex = 9;
         this.panel8.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel9
         // 
         this.panel9.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel9.Location = new System.Drawing.Point(168, 112);
         this.panel9.Name = "panel9";
         this.panel9.Size = new System.Drawing.Size(32, 32);
         this.panel9.TabIndex = 10;
         this.panel9.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel4
         // 
         this.panel4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel4.Location = new System.Drawing.Point(104, 80);
         this.panel4.Name = "panel4";
         this.panel4.Size = new System.Drawing.Size(32, 32);
         this.panel4.TabIndex = 5;
         this.panel4.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel5
         // 
         this.panel5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel5.Location = new System.Drawing.Point(136, 80);
         this.panel5.Name = "panel5";
         this.panel5.Size = new System.Drawing.Size(32, 32);
         this.panel5.TabIndex = 6;
         this.panel5.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel6
         // 
         this.panel6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel6.Location = new System.Drawing.Point(168, 80);
         this.panel6.Name = "panel6";
         this.panel6.Size = new System.Drawing.Size(32, 32);
         this.panel6.TabIndex = 7;
         this.panel6.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel7
         // 
         this.panel7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel7.Location = new System.Drawing.Point(104, 112);
         this.panel7.Name = "panel7";
         this.panel7.Size = new System.Drawing.Size(32, 32);
         this.panel7.TabIndex = 8;
         this.panel7.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel1
         // 
         this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel1.Location = new System.Drawing.Point(104, 48);
         this.panel1.Name = "panel1";
         this.panel1.Size = new System.Drawing.Size(32, 32);
         this.panel1.TabIndex = 2;
         this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel2
         // 
         this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel2.Location = new System.Drawing.Point(136, 48);
         this.panel2.Name = "panel2";
         this.panel2.Size = new System.Drawing.Size(32, 32);
         this.panel2.TabIndex = 3;
         this.panel2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // panel3
         // 
         this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.panel3.Location = new System.Drawing.Point(168, 48);
         this.panel3.Name = "panel3";
         this.panel3.Size = new System.Drawing.Size(32, 32);
         this.panel3.TabIndex = 4;

⌨️ 快捷键说明

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