我正在做一个继承 ComboBox 类的组件 为MyComboBox
实现通过在文本框输入数据下拉列表框自动过滤数据的功能;
为此,我在类里声明了一个ListBox 作为存储数据用;在MyComboBox_TextChanged事件中负有代码。但是当运行时 当出发MyComboBox的事件用到 ListBox 的时候 都有为将对象引用实例的错误提示。
请问:将ListBox 实例化在那个位置?怎么解决这个问题?
部分代码如下:
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
public class MyComboBox :ComboBox
{
private System.Windows.Forms.ListBox lbMain;
public MyComboBox ()
{ InitializeComponent();
lbMain = new ListBox() ;
lbMain.Click+=new EventHandler(lbMain_Click);
lbMain.KeyDown+=new KeyEventHandler(lbMain_KeyDown);
lbMain.Visible = false ;
}/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.Sorted = true;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MyComboBox _KeyDown);
this.DropDown += new System.EventHandler(this.MyComboBox _DropDown);
this.TextChanged += new System.EventHandler(this.MyComboBox _TextChanged);
this.SelectedIndexChanged += new System.EventHandler(this.MyComboBox _SelectedIndexChanged);
this.Leave += new System.EventHandler(this.MyComboBox _Leave); }
//这几个事件都有对lbMain的引用。}
lbMain = new ListBox() ; <- 这个放在的 InitializeComponent(); 之前
以及在生命出直接
private System.Windows.Forms.ListBox lbMain=new ListBox();
在MyComboBox(System.ComponentModel.IContainer container)构造函数中创建
结果都是一样的 请指教
你构造的方式是对的,还是要看你具体出错的代码
有问题短信我