-6

我正在尝试创建客户列表。当用户插入新客户端的名称时,该名称必须添加到列表中并显示在消息框中。预配置的客户端是 Jack、Sandra、Anna、Tom 和 Bob。当我运行以下脚本并输入新的客户端名称时,会弹出一个没有新名称的弹出窗口。为什么是这样?

    private void btnAddClient_Click(object sender, EventArgs e)
    {

        string msg = "";

        List<string> Clients = new List<string>();
        Clients.Add("Jack");
        Clients.Add("Sandra");
        Clients.Add("Anna");
        Clients.Add("Tom");
        Clients.Add("Bob");

        foreach (string val in Clients)
        {
            msg += "- " + val + "\n";
        }

        if (txtAddClient.Text == "")
        {
            MessageBox.Show("No client name has been entered!");
        }
        else
        {
            string newClient = txtAddClient.Text;
            Clients.Add(newClient);
            MessageBox.Show(msg);
        }
    }
4

5 回答 5

3

因为您没有将 msg 变量附加到文本框中的数据。

您遍历现有客户端并附加变量,但一旦您确定文本框不为空,您将内容添加到列表并显示 msg 变量。您需要使用输入的数据附加 msg 变量。

请参阅@Tisho 的代码示例如何实现它 - 不想将他的代码复制为我自己的

于 2012-07-16T12:55:18.673 回答
1
    private void btnAddClient_Click(object sender, EventArgs e)
    {

        string msg = "";

        List<string> Clients = new List<string>();
        Clients.Add("Jack");
        Clients.Add("Sandra");
        Clients.Add("Anna");
        Clients.Add("Tom");
        Clients.Add("Bob");

        if (txtAddClient.Text == "")
        {
            MessageBox.Show("No client name has been entered!");
        }
        else
        {
            string newClient = txtAddClient.Text;
            Clients.Add(newClient);

            // this is where you want to join your string
            // AFTER you add the new client to the list
            foreach (string val in Clients)
            {
                msg += "- " + val + "\n";
            }

            MessageBox.Show(msg);
        }
    }
于 2012-07-16T12:56:51.700 回答
0
public partial class Form1 : Form
{
    List<string> Clients = new List<string>();

    public Form1()
    {
        InitializeComponent();
        Clients.Add("Jack");
        Clients.Add("Sandra");
        Clients.Add("Anna");
        Clients.Add("Tom");
        Clients.Add("Bob");
    }

    private void btnAddClient_Click(object sender, EventArgs e)
    {
        if (txtAddClient.Text == "")
        {
            MessageBox.Show("No client name has been entered!");
        }
        else
        {
            string msg = "";
            string newClient = txtAddClient.Text;
            Clients.Add(newClient);
            foreach (string val in Clients)
            {
                msg += "- " + val + "\n";
            }
            MessageBox.Show(msg);
        }
    }
}
于 2012-07-16T13:55:25.717 回答
0

更新:

private List<string> Clients = new List<string>(){ "Jack", "Sandra", "Anna", "Tom", "Bob"};

private void btnAddClient_Click(object sender, EventArgs e)
{
    string msg = "";

    if (txtAddClient.Text == "")
    {
        MessageBox.Show("No client name has been entered!");
    }
    else
    {
        string newClient = txtAddClient.Text;
        Clients.Add(newClient);
        foreach (string val in Clients)
        {
            msg += "- " + val + "\n";
        }
        MessageBox.Show(msg);
    }
}
于 2012-07-16T12:56:12.053 回答
0

you must do

msg += txtAddClient.Text;

and then

MessageBox.Show(msg);
于 2012-07-16T12:57:01.990 回答