【问题标题】:How to add an item from a listbox into a textbox如何将列表框中的项目添加到文本框中
【发布时间】:2020-06-16 07:24:35
【问题描述】:

我正在使用 Visual Studio 中的 Windows 窗体。用 C# 编码

我想帮助我将列表框项添加到文本框。代码如下:

//*当一个按钮被点击时,它会将每个客户的详细信息显示到一个列表框中。每行都有一个客户的名字、姓氏、电话和电子邮件地址。客户的详细信息来自数据库。

    string customerFirstname = "";
    string customerLastname = "";
    string customerPhone = "";
    string customerEmail = "";

private void buttonSearch_Click(object sender, EventArgs e)
    {



        SQL.selectQuery("SELECT * FROM Customer");






        if (SQL.read.HasRows)
        {
            //this goes through each table row in the database
            while (SQL.read.Read())
            {
                //the values are obtained from the database and assigned to the variables.


                customerFirstname = SQL.read[0].ToString();
                customerLastname = SQL.read[1].ToString();
                customerPhone = SQL.read[2].ToString();
                customerEmail = SQL.read[3].ToString();



                //Each row is displayed in the listbox
                listBoxCustomers.Items.Add(
                customerFirstname 
                + customerLastname  + customerPhone +  customerEmail);




            }


        }
    }

选择一行后,我想单击一个按钮并在文本框中显示该特定客户的详细信息。

这是我使用的代码:

private void buttonSelectCustomerFromSearch_Click(object sender, EventArgs e) {
        textboxCustomerFirstName.Text = customerFirstname;
        textboxCustomerLastname.Text = customerLastname;
        textboxCustomerPhone.Text = customerPhone;
        textboxCustomerEmail.Text = customerEmail;  }

我在将所选列表框项中的值添加到文本框时遇到问题。当我运行它时,文本框会显示数据库中最后一个客户的详细信息,它会忽略任何选定的行。你能帮忙吗?谢谢。

【问题讨论】:

  • 您使用的是什么 UI 技术:例如 WPF、WinForms 等?
  • 欢迎。阅读this 教程以获得更好的方法。

标签: c#


【解决方案1】:

在您的点击处理程序中,您正在使用“customerFirtName”、“customerLastname”等。这些是您用来从数据库中读取数据的变量。这就是为什么您的文本框会显示数据库中最后一个客户的数据的原因——它是使用该变量传输的最后一个数据,并且这些变量仍然保存其数据。

您需要列表框“所选项目”的数据。您可以从此项文本中解析数据,也可以创建一个“人”对象并将其存储在列表框“标签”中。因此,当按钮被点击时,您可以将 listbox.SelectedItem.Tag 转换为 person 类型并使用它来填充文本框。

【讨论】:

【解决方案2】:

我认为这是一个简单的演示开始

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private readonly List<Customer> customers = GetCustomers();

        private static List<Customer> GetCustomers()
    {
        var customers = new List<Customer>();

        using (var con = new SqlConnection(connectionString))
        {
            string sql = "SELECT Id, FirstName, LastName, Email, Phone FROM Customers";
            using (var cmd = new SqlCommand(sql, con))
            {
                cmd.CommandType = CommandType.Text;
                try
                {
                    con.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var customer = new Customer
                            {
                                Id = Convert.ToInt32(reader["Id"]),
                                FirstName = reader["FirstName"].ToString(),
                                LastName = reader["FirstName"].ToString(),
                                Email = reader["Email"].ToString(),
                                Phone = Convert.ToInt32(reader["Phone"]),

                            };

                            customers.Add(customer);
                        }
                    }
                }
                catch (SqlException e)
                {
                    MessageBox.Show(e.Message);
                }
            }


        }
        return customers;
    }    
        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.DataSource = customers;
            listBox1.DisplayMember = "FirstName";
            listBox1.ValueMember = "Id";
        }



        private void button1_Click(object sender, EventArgs e)
        {
            int Id = (int)listBox1.SelectedValue;

            Customer customerSelected = customers.Find(el => el.Id == Id);
            textBox1.Text = customerSelected.ToString();
        }

    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Phone { get; set; }
        public string Email { get; set; }
        public int Id { get; set; }



public override string ToString()
    {
        return $"Id: {Id},FirstName: {FirstName},LastName: { LastName},Phone: {Phone},Email: {Email}";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多