在c#中多次使用文本框

时间:2015-10-03 17:37:49

标签: c# sockets

这是一个用c#编写的小型客户端GUI程序。这个想法是:首先客户端将一个id(在文本框中键入)发送到服务器(按下检查按钮),服务器将检查该ID,如果它有效,服务器将返回值0否则为-1。现在,如果返回值为-1,则客户端将再次在复选框中输入一个输入并将其发送到服务器以进行另一次检查(通过按下检查按钮)。此过程将继续,直到客户端从服务器收到0。但是,在我的代码中,当我在文本框中输入无效输入时,它会像检查按钮一样挂起。所以,我不能重复向服务器发送有效身份证的过程。这是我的示例客户端代码:

using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
 using System.IO;
using System.Collections.Generic;

class AsyncTcpClient:Form
{

    private TextBox newText;
    private Button check;
    public  TcpClient tcpClient;


   NetworkStream ns;
    StreamReader sr;
    StreamWriter sw;  
    string data;

public AsyncTcpClient()
{

    Size = new Size(400, 380);

    newText = new TextBox();
    newText.Parent = this;
    newText.Size = new Size(200, 2 * Font.Height);
    newText.Location = new Point(10, 55);

    check = new Button();
     check.Parent = this;
     check.Text = "checkID";
     check.Location = new Point(295, 52);
     check.Size = new Size(6 * Font.Height, 2 * Font.Height);
     check.Click += new EventHandler(checkOnClick);

    }


void checkOnClick(object obj, EventArgs ea)
{
    tcpClient = new TcpClient("127.0.0.1", 1234);
     ns = tcpClient.GetStream();
     sr = new StreamReader(ns);
     sw = new StreamWriter(ns);
    send:
    //sending ID
    sw.WriteLine(newText.Text);
    sw.Flush();
    //receiving validity of ID
    data = sr.ReadLine();
    int validid = int.Parse(data);
    if (validid == 0)
    {            
        newText.Text="Valid data";
        check.Enabled = false;          
    }
    else
    {                        
        //sending ID again            
        goto send;
    }
}

[STAThread]
public static void Main()
{

    Application.Run(new AsyncTcpClient());
}
}

如何持续检查我的身份证是否有效?

2 个答案:

答案 0 :(得分:0)

您应该清除文本框内容并向用户发出警告并等待他/她写入新ID

RewriteEngine On
RewriteRule ^(.*)-hello-world-(.*)$ $1-new-static-string-$2 [R=301,L,NC]

答案 1 :(得分:0)

首先删除GOTO语句然后在构造函数中移动你的tcp Connection,这样你的连接是持久的(你每次单击按钮时都避免tcp三次握手)然后使用label来显示错误信息并等待用户输入有效数据在继续之前

using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
 using System.IO;
using System.Collections.Generic;

class AsyncTcpClient:Form
{

    private TextBox newText;
    private Button check;
    private Label errorMessage;
    public  TcpClient tcpClient;


   NetworkStream ns;
    StreamReader sr;
    StreamWriter sw;  
    string data;

public AsyncTcpClient()
{
tcpClient = new TcpClient("127.0.0.1", 1234);
     ns = tcpClient.GetStream();
    Size = new Size(400, 380);

    newText = new TextBox();
    newText.Parent = this;
    newText.Size = new Size(200, 2 * Font.Height);
    newText.Location = new Point(10, 55);

    errorMessage = new Label();
    errorMessage.Parent = this;
     errorMessage.Size = new Size(200, 2 * Font.Height);
    errorMessage.Location = new Point(20, 55);   

    check = new Button();
     check.Parent = this;
     check.Text = "checkID";
     check.Location = new Point(295, 52);
     check.Size = new Size(6 * Font.Height, 2 * Font.Height);
     check.Click += new EventHandler(checkOnClick);

    }


void checkOnClick(object obj, EventArgs ea)
{

     using(sr = new StreamReader(ns))
{
     using(sw = new StreamWriter(ns))
    {
    //sending ID
    sw.WriteLine(newText.Text);
    sw.Flush();
    //receiving validity of ID
    data = sr.ReadLine();
    int validid = int.Parse(data);
    if (validid == 0)
    {            
        newText.Text="Valid data";
        check.Enabled = false;          
    }
    else
    {                        
   errorMessage.Text="InValid data enter again";   
    }
}
}
}

[STAThread]
public static void Main()
{

    Application.Run(new AsyncTcpClient());
}
}