2009-12-17 180 views
0

如何在點擊發送後清除我的文本框?如何清除文本框?

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

namespace SocketClient 
{ 

    public partial class SocketClient : Form 
    { 
     System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient(); 
     NetworkStream serverStream = default(NetworkStream); 
     string readData = null; 


     public SocketClient() 
     { 
      InitializeComponent(); 
     } 


     private void getMessage() 
     { 
      while (true) 
      { 
       serverStream = clientSocket.GetStream(); 
       int buffSize = 0; 
       byte[] inStream = new byte[10025]; 
       buffSize = clientSocket.ReceiveBufferSize; 
       serverStream.Read(inStream, 0, buffSize); 
       string returndata = System.Text.Encoding.ASCII.GetString(inStream); 
       readData = "" + returndata; 
       msg(); 
      } 
     } 


     private void msg() 
     { 
      if (this.InvokeRequired) 
       this.Invoke(new MethodInvoker(msg)); 
      else 
       textDisplay.Text = textDisplay.Text + Environment.NewLine + " >> " + readData; 
     } 


     private void buttonConnect_Click(object sender, EventArgs e) 
     { 
      readData = "Conected to NYP Chat Server ..."; 
      msg(); 
      clientSocket.Connect("127.0.0.1", 8888); 
      serverStream = clientSocket.GetStream(); 

      byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textName.Text + "$"); 
      serverStream.Write(outStream, 0, outStream.Length); 
      serverStream.Flush(); 

      Thread ctThread = new Thread(getMessage); 
      ctThread.Start(); 
     } 


     private void buttonSend_Click(object sender, EventArgs e) 
     { 
      byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textSend.Text + "$"); 
      serverStream.Write(outStream, 0, outStream.Length); 
      serverStream.Flush(); 

      Send(textSend.Text); 
      textSend.Text = ""; 

     } 

     private void textDisplay_TextChanged(object sender, EventArgs e) 
     { 
      textDisplay.SelectionStart = textDisplay.Text.Length; 
      textDisplay.ScrollToCaret(); 
      textDisplay.Refresh(); 
     } 

     private void textSend_TextChanged(object sender, EventArgs e) 
     { 
      Send(String text); 
     } 
    } 
} 
+5

你將需要描述你的情況多。 – 2009-12-17 02:15:18

+1

你究竟在說什麼? – SLaks 2009-12-17 02:23:09

+1

好的我的應用程序就像MSN。 在您按下發送文本框中的文本後會自動清除。 – 2009-12-17 03:12:13

回答

7
private void buttonSend_Click(object sender, EventArgs e) 
{ 
    // send text 
    byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textSend.Text + "$"); 
    serverStream.Write(outStream, 0, outStream.Length); 
    serverStream.Flush(); 

    // clear textbox 
    textSend.Text = ""; 
} 
+2

string.Empty! lolz – 2009-12-17 02:15:53

+0

您應鎖定空字符串分配,因爲它不是安全的 – Pierreten 2009-12-17 02:30:00

+2

+1似乎滿足所有要求。 – 2009-12-17 02:57:24