在C#中创建窗口句柄时出错

时间:2013-11-25 11:55:22

标签: c# winforms

我一直在使用C#中的tabcontrol。我在所有10个标签中添加了文本框,标签,数据网页,面板。当我尝试在文本框中使用自动完成时,它给我一个例外,说明“创建窗口句柄-Win32异常错误”。以下是我的代码:

    private void textBoxCustomerMNO_Enter(object sender, EventArgs e)
    {

        textBoxCustomerMNO.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        textBoxCustomerMNO.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection CustomerMobileNo = new AutoCompleteStringCollection();

        string Query = "select PATIENT_MNO from MEDICINES_SALE_RECEPIT";
        DataTable CustomerNos = clsConnection.GetDataTable(Query);
        if (CustomerNos.Rows.Count > 0)
        {
            foreach(DataRow row in CustomerNos.Rows)
            {
                CustomerMobileNo.Add(row["PATIENT_MNO"].ToString().Trim());
            }
        }

    }

1。连接类已经创建。

2 个答案:

答案 0 :(得分:0)

当我要在GotFocus事件中设置文本框的AutoCompleteMode属性时,我遇到了类似的问题。这种行为很奇怪,但您可以通过在Enter事件或GotFocus事件之前设置AutoCompleteMode属性(例如在Form_Load事件中)或通过设计模式下的属性窗口(当然,如果您的文本框已创建并且在设计模式下存在。)

//put this in your **Form-Load** event:
textBoxCustomerMNO.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
//.. and remove it from the textBoxCustomerMNO_Enter event!

如果您动态创建textBoxCustomerMNO文本框,则在此处设置AutoCompleteMode属性,然后再定义Enter偶数:

t = new TextBox();
t.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //set this before Enter event! 
t.Enter += new EventHandler(...)

答案 1 :(得分:0)

如果大于0,则释放组件使用的所有资源

        if (this.Handle.ToInt32() > 0)
            {

                if (components != null)
                {
                    components.Dispose();
                }
             }