我正在做一个继承 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的引用。}

解决方案 »

  1.   

    你怎么用ListBox 的,代码看来没有错
      

  2.   

    楼上 研究一下 代码编译是没有问题的 但是在Windows Application 应用程序中,从 组件面板 或 编译运行 时(两种情况都出现过)就会弹出为将对象引用实例。
      

  3.   

    InitializeComponent();
    lbMain = new ListBox() ;  <-  这个放在的  InitializeComponent(); 之前
      

  4.   

    试过了 
    以及在生命出直接 
    private System.Windows.Forms.ListBox lbMain=new ListBox();
    在MyComboBox(System.ComponentModel.IContainer container)构造函数中创建
    结果都是一样的 请指教
      

  5.   

    earthgoshawk(追求):
        你构造的方式是对的,还是要看你具体出错的代码
    有问题短信我
      

  6.   

    [email protected] cxx1997(小网虫)(最厉害的妖怪))