当选定索引 == 空时清除文本框

本文关键字:清除 文本 索引 | 更新日期: 2023-09-27 17:55:29

我正在编写股票程序 - 只是为了学习一点 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 中选择某些内容时,

当它存在于数据库中时,它会自动将内容插入文本框,但是当我擦除我在 combobox5 中选择的这个东西时,文本框中的文本仍然存在。当组合框5 == 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
      }
}