简体   繁体   English

c#TextBox没有清除

[英]c# TextBox not clearing

I have a program which writes to an Access data table, and then the information is displayed in the data grid. 我有一个程序写入Access数据表,然后信息显示在数据网格中。 this is triggered when a 6 digit number occurs in the textbox followed by an "L" . 当文本框中出现一个6位数后跟一个"L"时,会触发此操作。 I have "TextBox1.Clear();" 我有"TextBox1.Clear();" in the code which clears the 6 numbers away, but not the "L" . 在清除6个数字的代码中,但不是"L"

Here is my code: 这是我的代码:

  private void Registration_Main(object sender, KeyPressEventArgs e)
    {
        SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        Object returnValue;
        Object returnName;
        string txtend = textBox1.Text;


        if (e.KeyChar == 'L')
        {
            DBConnection.Open();
        }


        if (DBConnection.State == ConnectionState.Open)
        {
            if (textBox1.Text.Length != 6) return;
            {
                cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
                cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
                cmd.CommandType = CommandType.Text;
                cmd.Connection = DBConnection;
                returnValue = cmd.ExecuteScalar() + "\t " + textBox1.Text.Replace(@"L", "");
                returnName = cmd.ExecuteScalar();

                DBConnection.Close();

                AccessDB_Connection(returnName);
            }

            textBox1.Clear();
        }
    }

    private void AccessDB_Connection(object returnName)
    {
        String varID;
        Object varName;

        varID = textBox1.Text.Replace(@"L", "").ToString();
        varName = returnName;

        OleDbConnection OLEDB_Connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Test Applications\\Tablet Registration - Access Database\\Tablet Registration - Access\\Tablet Registration - Access\\Registration.accdb"); 
        OleDbCommand updateCmd = new OleDbCommand();

        try
        {
            updateCmd.CommandText = "INSERT INTO TestDB ([Name], [ID]) VALUES (@NAME, @ID)";
            updateCmd.Parameters.AddWithValue("@NAME", varName);
            updateCmd.Parameters.AddWithValue("@ID", varID);
            OLEDB_Connection.Open();
            updateCmd.Connection = OLEDB_Connection;
            updateCmd.ExecuteNonQuery();
            this.testDBTableAdapter.Fill(this.registrationDataSet1.TestDB);
            OLEDB_Connection.Close();
        }
        catch 
        {
        }
    }

Any help is much appreciated ! 任何帮助深表感谢 !

Try updating your code from: 尝试更新您的代码:

if (e.KeyChar == 'L')
    {
        DBConnection.Open();
    }

to

 if (e.KeyChar == 'L')
    {
        e.Handled = true;
        DBConnection.Open();
    }

This should stop the event propagating and populating your textbox. 这应该会阻止事件传播和填充文本框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM