我不知道该怎么说,这对我来说很奇怪! 直到几天,这个代码对我来说很好,但现在它不再起作用了:
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)
我在其中加入了一些断点,看它是否存在(我的意思是在我之前)但它根本不会去那里! 有什么问题?我做了些蠢事吗?
编辑
来自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);
来自我的代码:
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;
}
}
答案 0 :(得分:1)
I tried to recreate your problem but it looks like it works for me. Do you have the same issue with code below? If not then can you modify so the problem occurs? There may be something more what you are doing and what is causing it.
Whole program:
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());
}
}
}