c# -列表框事件不再工作了

本文关键字:不再 工作 事件 列表 | 更新日期: 2023-09-27 18:04:50

我不知道该怎么说,这对我来说很奇怪!直到几天前,这段代码对我来说还很好,但现在它不再工作了:

private void lst_CustomerName_DoubleClick(object sender, EventArgs e)
{
    if (ds3.Tables["T"].Rows.Count > 0)
    {
        string str = lst_CustomerName.SelectedItems[0].ToString();
        txt_CustomerID.Text = ds3.Tables["T"].Rows[lst_CustomerName.SelectedIndex]["Id"].ToString();
        txt_CustomerName.Text = str;
        lst_CustomerName.Visible = false;
    }
}

不仅是这个事件,而且我测试了这些事件,它们也不起作用:

lst_ProductName_SelectedValueChanged(object sender, EventArgs e)

lst_ProductName_Click(object sender, EventArgs e)

我在里面放了一些断点,看看它是否去那里(我的意思是在我的if之前),但它根本没有去那里!有什么问题吗?我是不是做了什么蠢事?

编辑:

from designer.cs:

this.lst_ProductName.FormattingEnabled = true;
            this.lst_ProductName.ItemHeight = 19;
            this.lst_ProductName.Location = new System.Drawing.Point(888, 374);
            this.lst_ProductName.Name = "lst_ProductName";
            this.lst_ProductName.Size = new System.Drawing.Size(259, 99);
            this.lst_ProductName.TabIndex = 29;
            this.lst_ProductName.DoubleClick += new System.EventHandler(this.lst_ProductName_DoubleClick);

from my code:

private void lst_ProductName_DoubleClick(object sender, EventArgs e)
{
    if (ds6.Tables["T"].Rows.Count > 0)
    {
        string str = lst_ProductName.SelectedItems[0].ToString();
        txt_ID_product.Text = ds6.Tables["T"].Rows[lst_ProductName.SelectedIndex]["Id"].ToString();
        txt_product_name.Text = str;
        lst_ProductName.Visible = false;
    }
}

c# -列表框事件不再工作了

我试图重新创建您的问题,但它看起来像它适用于我。下面的代码你有同样的问题吗?如果不是,你可以修改,所以问题发生了吗?也许你正在做的事情和导致它的原因还有别的原因。

整体计划:

using System;
using System.Windows.Forms;
namespace ListBoxNotWorking
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.ListBox lst_ProductName;
        private System.Windows.Forms.TextBox txt_product_name;
        public Form1()
        {
            this.lst_ProductName = new System.Windows.Forms.ListBox();
            this.txt_product_name = new System.Windows.Forms.TextBox();
            this.lst_ProductName.FormattingEnabled = true;
            this.lst_ProductName.Items.AddRange(new object[] {
            "item1",
            "item2",
            "item3"});
            this.lst_ProductName.Location = new System.Drawing.Point(81, 50);
            this.lst_ProductName.Name = "lst_ProductName";
            this.lst_ProductName.Size = new System.Drawing.Size(120, 95);
            this.lst_ProductName.TabIndex = 0;
            this.lst_ProductName.DoubleClick += new System.EventHandler(this.lst_ProductName_DoubleClick);
            this.txt_product_name.Location = new System.Drawing.Point(86, 189);
            this.txt_product_name.Name = "txt_product_name";
            this.txt_product_name.Size = new System.Drawing.Size(100, 20);
            this.txt_product_name.TabIndex = 1;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.txt_product_name);
            this.Controls.Add(this.lst_ProductName);
        }
        private void lst_ProductName_DoubleClick(object sender, EventArgs e)
        {
            string str = lst_ProductName.SelectedItems[0].ToString();
            //  txt_ID_product.Text = ds6.Tables["T"].Rows[lst_ProductName.SelectedIndex]["Id"].ToString();
            txt_product_name.Text = str;
            lst_ProductName.Visible = false;
        }
    }
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}