2016-04-27 28 views
1

我是新來的編碼在C#中,我的項目使用Winsock控件連接服務器和客戶端。我正在完成這個程序http://www.go4expert.com/articles/winsock-c-sharp-t3312/來連接服務器和客戶端。Winsock with C#嚴重性t代碼錯誤:名稱'DataInput'在當前上下文中不存在

形式: enter image description here

代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Winsock 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      this.w1.Error += new AxMSWinsockLib.DMSWinsockControlEvents_ErrorEventHandler(this.w1_Error); 
      this.w1.ConnectEvent += new System.EventHandler(this.w1_ConnectEvent); 
      this.w1.DataArrival += new AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEventHandler(this.w1_DataArrival); 
     } 
     Boolean isConnect = false; 
     private void w1_ConnectEvent(object sender, EventArgs e) 
     { 
      DataInput.Text += "\n - Connect Event : " + w1.RemoteHostIP; 
      isConnect = true; 
     } 

     public void w1_Error(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e) 
     { 
      DataInput.Text += "\n- Error : " + e.description; 
      isConnect = false; 
     } 

     private void w1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e) 
     { 
      String data = "";  
      Object dat = (object)data; 
      w1.GetData(ref dat); 
      data = (String)dat; 
      DataInput.Text += "\nServer - " + w1.RemoteHostIP + " : " + data; 
     } 

     private void send_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       if (isConnect) 
       { 
        w1.SendData(SendText.Text); 

        DataInput.Text += "\nClent(You ;-) : " + SendText.Text; 

        SendText.Text = ""; 
       } 
       else 
        MessageBox.Show("You are not connect to any host "); 
      } 
      catch (AxMSWinsockLib.AxWinsock.InvalidActiveXStateException g) 
      { 
       DataInput.Text += "\n" + g.ToString(); 
      } 
      catch (Exception ex) 
      { 
       DataInput.Text += "\n" + ex.Message; 
      } 
     } 

     private void disconnect_Click(object sender, EventArgs e) 
     { 
      w1.Close(); 
      w1.LocalPort = Int32.Parse(portText.Text); 
      w1.Listen(); 
      DataInput.Text += "\n - Disconnected"; 
     } 

     private void Connect_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       w1.Close(); 
       w1.Connect(IPText.Text, portText.Text); 

      } 
      catch (System.Windows.Forms.AxHost.InvalidActiveXStateException g) 
      { 
       DataInput.Text += "\n" + g.ToString(); 
      } 
     } 

     private void w1_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e) 
     { 
      if (isConnect == true) 
      { 
       w1.Close(); 
      } 
      w1.Accept(e.requestID); 
      isConnect = true; 
      DataInput.Text += "\n - Client Connected :" + w1.RemoteHostIP; 
     } 
    } 
} 

錯誤

The name 'DataInput' does not exist in the current context

我似乎無法在網上搜索,請幫我找到一個解決這個問題:(

+0

看來DataInput是用於發送數據的編輯控件的名稱。您可能會在您的表單上將其命名爲其他內容。 – JamieMeyer

+0

@JamieMeyer抱歉,我無法理解「其他」意味着我需要使用而不是DataInput。 – Divi

+0

我再次查看了代碼。標籤「獲取數據」下方的大控件應命名爲DataInput。 – JamieMeyer

回答

0

在窗體上創建一個名爲「DataInput」的文本框。

相關問題