C# 选择dex==null时清除文本框

C# 选择dex==null时清除文本框,c#,.net,C#,.net,我在写股票程序-只是为了学习一点c语言,我遇到了一些问题 这是我的一部分代码 private void comboBox5_SelectedIndexChanged(object sender, EventArgs e) { string conString = "Data Source=192.168.0.195;" + "Initial Catalog=test;" + "User id=sa;" +

我在写股票程序-只是为了学习一点c语言,我遇到了一些问题

这是我的一部分代码

    private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
    {

        string conString =
         "Data Source=192.168.0.195;" +
         "Initial Catalog=test;" +
         "User id=sa;" +
         "Password=12345678;";

        string query = "Select * from dokumenty where symbol='" + comboBox_symbol.Text + "' ;   ";
        SqlConnection conDB = new SqlConnection(conString);
        SqlCommand cmdDB = new SqlCommand(query, conDB);
        SqlDataReader sqlReader;

        try
        {
            conDB.Open();
            sqlReader = cmdDB.ExecuteReader();

            while (sqlReader.Read())
            {
                var s_Typ_dok = sqlReader.GetString(1);
                var s_Symbol = sqlReader.GetString(2);
                var s_Delivery_date = sqlReader.GetString(3);
                var s_Invoice_date = sqlReader.GetString(4);
                var s_Invoice_nr = sqlReader.GetInt32(5).ToString();
                var s_Sybtype = sqlReader.GetString(6);
                var s_Produkt_index = sqlReader.GetString(7);
                var s_Produkt_name = sqlReader.GetString(8);
                var s_Quantity = sqlReader.GetInt32(9).ToString();
                var s_Price = sqlReader.GetString(10);
                var s_From_warehouse = sqlReader.GetString(12);
                var s_To_warehouse = sqlReader.GetString(13);
                var s_Currency = sqlReader.GetString(14);
                var s_Supplier_reciever = sqlReader.GetString(15);

                comboBox_Type.Text = s_Typ_dok;
                textBox_symbol.Text = s_Symbol;
                textBox_deliveryDate.Text = s_Delivery_date;
                textBox_invoiceDate.Text = s_Invoice_date;
                textBox_invoice.Text = s_Invoice_nr;
                textBox_subtype.Text = s_Sybtype;
                textBox_produkt_index.Text = s_Produkt_index;
                textBox_name.Text = s_Produkt_name;
                textBox_quantity.Text = s_Quantity;
                textBox_price.Text = s_Price;
                comboBox_from_warehouse.Text = s_From_warehouse;
                comboBox_to_warehouse.Text = s_To_warehouse;
                comboBox_currency.Text = s_Currency;
                textBox_supplier.Text = s_Supplier_reciever;    

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
这很好,当我在combobox5中选择某个内容时,它会在DB中存在时自动将内容插入文本框,但当我删除我在Combox5中选择的内容时,文本框中的文本仍然存在。当combobox5==null时是否有可能将其删除?

在SelectedIndexChanged事件中,添加一个条件以检查SelectedIndex==0,如果所选索引为零,则清除文本框的文本

如果您不想编辑组合框中的文本,则可以通过设置将组合框设置为不可编辑

comboBox5.DropDownStyle = ComboBoxStyle.DropDownList;
这将不允许用户编辑组合框中的文本

private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
{
      if(comboBox5.SelectedIndex==0)
      {
          TextBoxId.Text=String.Empty;
      }
      else
      {
         //Rest of your code here
      }
}

你怎么填套餐?combo的DropDownStyle属性的值是多少?可以准确描述为什么这不适用于您?当我在combobox中更正文本时,它不会清除textBox。当您在combobox中编辑文本时,在这种情况下,您的事件不会被触发,这就是为什么在删除文本时它不会清除文本非常感谢,我只是设置了不可编辑的组合框,然后添加了一个空项目,当它为空时,我只是清除文本框