【问题标题】:Same SelectedIndexChanged function for 2 comboboxes c#2个组合框c#的相同SelectedIndexChanged函数
【发布时间】:2020-11-17 00:52:07
【问题描述】:

我为 2 个组合框创建了一个相同的事件来填充我的输入控件,以便用户可以看到他们将要删除或更新的内容。当我制作单独的事件时,它起作用了,但在制作相同的事件时它不起作用。我应该在 if 语句中添加什么条件

如果条件 error 我得到 id "Index was outside the bounds of the array"

private void BoardComboBo(object sender, EventArgs e)
        {
            if (comboBox12.SelectedIndex!=0)//error is here
            {
                con.Open();
                cmd = new SqlCommand("Select * from Users where user_Id='" + comboBox12.Text + "';", con);
                SqlDataReader DR1 = cmd.ExecuteReader();
                if (DR1.Read())
                {
                    //code to fill textboxes
                }
                con.Close();
            }
            if(comboBox1.SelectedIndex!=0)//error is here
            {
                //to split combobox values
                string selectedvalue = comboBox1.Text;
                split = selectedvalue.Split();
                //add values on fields 
                con.Open();
                cmd = new SqlCommand("Select * from Users where firstName='" + split[0] + "' and lastName='" + split[1] + "';", con);
                SqlDataReader DR1 = cmd.ExecuteReader();
                if (DR1.Read())
                {
                    //code to fill textboxes
                }
                con.Close();
            }
        }

我也改变了第二个 if 条件:

if (comboBox12.Text!=" ")// this works
            {
                //to split combobox values
                string selectedvalue = comboBox1.Text;
                split = selectedvalue.Split();
                //add values on fields 
                con.Open();
                cmd = new SqlCommand("Select * from Users where firstName='" + split[0] + "' and lastName='" + split[1] + "';", con);
                SqlDataReader DR1 = cmd.ExecuteReader();
                if (DR1.Read())
                {
                    //code to fill textboxes
                }
                con.Close();
}

当我尝试按 user_Id

进行选择时,我现在遇到了同样的错误

【问题讨论】:

  • “错误在这里” 错误是什么?
  • 在这种情况下,我得到“索引超出了数组的范围”
  • 既然你想让它做一些完全不同的事情,具体取决于哪个组合,你为什么要把它结合起来?此外,这从来都不是为 NET 应用程序构建 SQL 的正确方法——它非常容易出错,甚至很危险。
  • 它填写相同的表格,一个按 id 填写,另一个按名字和姓氏填写,这就是为什么我认为合并事件会更好

标签: c# combobox selectedindexchanged


【解决方案1】:

在方法签名中,sender 对象是触发事件的对象,因此我们可以将其转换为 ComboBox 并检查它是否是我们关心的对象:

private void BoardComboBo(object sender, EventArgs e)
{
    var comboBox = sender as ComboBox;
        
    if (comboBox == comboBox1)
    {
        // comboBox1 code here
    }
    else if (comboBox == comboBox12)
    {
        // comboBox12 code here
    }
}

但是,此时,您还可以有两个单独的事件。由于没有特定于 ComboBox 的大量重复代码,因此将它们重构为一个事件只会使代码更加繁琐。


关于"Index was outside the bounds of the array" 错误,您引用索引的唯一位置是拆分comboBox1Text 属性时。最有可能的是,Text 中没有任何空格,因此Split 返回一个单项数组。那么问题是当您尝试访问此处不存在的索引时:

split[1]  // Here you're attempting to access an index that doesn't exist

要解决此问题,请在访问可能不存在的索引之前检查数组的 Length,可能类似于:

// Set the last name to an empty string if it didn't exist
var lastName = split.Length > 1 ? split[1] : string.Empty;

请注意,您应该使用 SQL 命令参数来构建查询字符串。你这样做的方式很容易受到 SQL 注入的攻击。


另外,正如@Streamline 所提到的,您的原始代码有两个if 块而不是if / else if。这意味着无论哪个控件触发了事件,都将评估 if 条件。这也意味着如果两个 ComboBox 都有一个非零的 SelectedIndex,那么 both if 块体将运行。这可能是您问题代码中出现错误的原因,并且可能不是预期的行为。


最后,正如@OlivierJacot-Descombes 所提到的,如果没有选择任何项目,那么SelectedIndex 将是-1,而不是0。这意味着当您检查comboBox1.SelectedIndex!=0时,您不仅会忽略第一个(0)位置的值,而且如果没有选择任何值,那么它将通过这个条件(因为这种情况下的值是@ 987654342@).

【讨论】:

  • 如果没有选择任何项目,则返回负一 (-1) 的值。所以你应该测试if (comboBox == comboBox1) && comboBox1.SelectedIndex >= 0)。第二个也是一样。错误不能出现在您放置//error is here 注释的位置,因为那里没有数组访问。
  • @OlivierJacot-Descombes 这在SelectedIndexChanged 事件中可能吗?
  • 也许这可以通过设置SelectedItem = null;SelectedIndex = -1来触发。但是原代码comboBox1.SelectedIndex!=0很可能是错误的。
  • 而不是写我自己的回答:也许您可以在答案的第二部分中添加,两个 if 块的代码可以在调用事件处理程序时运行,因为他使用两个 @987654350 @s 而不是 if (...) { } else if (...) { }。这可能是异常的原因,因为您已经提到过文本无效。
猜你喜欢
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多