在c#中多次使用文本框

本文关键字:文本 | 更新日期: 2023-09-27 18:16:04

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

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());
}
}

我如何持续检查我的id是否有效?

在c#中多次使用文本框

您应该简单地清除文本框内容并向您的用户写一个警告,并等待他/她写一个新的id

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);
    //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
    {                        
        newText.Text="Invalid data. Please retry";       
        // Now the code will exit and your user could retry
    }
}

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

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());
}
}