ComboBox 选择返回 IndexOutofBounds 错误

问题描述 投票:0回答:4

我正在设置,第一个组合框(名称=组合3)的选择决定第二个组合框(名称=组合4)将显示的内容。这很好用。

现在我试图让第二个组合框确定我的第三个组合框将显示的内容。这是行不通的。当我在第二个组合框上做出选择时,它会卡住并返回错误。但是如果我对第二个组合框的值进行硬编码,它就可以工作。

我收到索引越界异常。问题似乎出在方法 private void ComboBox_SelectionChanged2() 上。有什么问题请指教。谢谢。

主窗口

string[] tableArray = { "Agent_Name", "Agent_Age", "Agent_Gender", "Agent_Alias", "Security_Clearance", "Dept_ID", "Job_ID", "Mission_ID" };
string[] attributeArray = { "Mission_Name", "Mission_Description", "Mission_Country", "Mission_City" };

private void ComboBox_SelectionChanged1(object sender, SelectionChangedEventArgs e)  
        {
            if (((ComboBoxItem)combo3.SelectedItem).Content.ToString() == "Agents")
            {
                combo4.Items.Clear();
                foreach (string x in tableArray)
                {
                    combo4.Items.Add(x);
                }
            }
            else
            {
                combo4.Items.Clear();
                foreach (string x in attributeArray)
                {
                    combo4.Items.Add(x);
                }
            }
        }

private void ComboBox_SelectionChanged2(object sender, SelectionChangedEventArgs e)
        {
            combo5.Items.Clear();
            MessageBox.Show(combo4.Text); //debugging line returns empty which shouldn't be the case. I chose Mission_Name thus it should show Mission_Name. 
            //fillCombo("SELECT * FROM " + combo3.Text + ";", "Mission_Name", combo5); //works   
            fillCombo("SELECT * FROM " + combo3.Text + ";", combo4.Text, combo5); // not working
        } 

private void fillCombo(string query, string name, ComboBox c)
        {
            MySqlCommand cmdReader = new MySqlCommand(query, conn);
            MySqlDataReader myReader;

            myReader = cmdReader.ExecuteReader();

            while (myReader.Read())
            {
                string temp = myReader.GetString(name);
                c.Items.Add(temp);
            }
            myReader.Close();
        }

XAML:

<ComboBox x:Name="combo3" Width="120" SelectionChanged="ComboBox_SelectionChanged1">
                <ComboBoxItem x:Name="box3" Content="Agents"/>
                <ComboBoxItem x:Name="box4" Content="Missions"/>
            </ComboBox>
            <ComboBox x:Name="combo4" Width="120" SelectionChanged="ComboBox_SelectionChanged2">
            </ComboBox>
<ComboBox x:Name="combo5" Width="120" Canvas.Left="818" Canvas.Top="588"/>

错误信息:

“System.IndexOutOfRangeException”类型的未处理异常 发生在MySql.Data.dll中

附加信息:在结果中找不到指定列:

组合框的外观。

enter image description here

c# wpf xaml
4个回答
1
投票

更换这个

MessageBox.Show(combo4.Text);

有了这个:

MessageBox.Show(combo4.SelectedText);

或者用这个:

MessageBox.Show(combo4.SelectedItem.ToString());

替换你使用的所有combo4.Text以及combo1 2 3等等


0
投票

正如我在评论中所说,生成用于查询该数据库的字符串存在问题。

尝试调试您的应用程序并对您的combobox4.Text属性设置断点,然后查看它生成的内容。


0
投票

问题是您正在使用

ComboBox_SelectionChanged2
事件运行代码来填写组合框,但您没有验证组合框在运行代码之前是否已做出选择。当在表单上绘制组合框并且在其他一些组合框中未选择任何值时,将触发此事件。您只需在事件中添加一个条件,以确保您的组合框在尝试访问它们之前具有值。

private void ComboBox_SelectionChanged2(object sender, SelectionChangedEventArgs e) { if (combo3.Text.Length > 0 && combo4.Text.Length > 0) { combo5.Items.Clear(); MessageBox.Show(combo4.Text); //debugging line returns empty which shouldn't be the case. I chose Mission_Name thus it should show Mission_Name. //fillCombo("SELECT * FROM " + combo3.Text + ";", "Mission_Name", combo5); //works fillCombo("SELECT * FROM " + combo3.Text + ";", combo4.Text, combo5); // not working } }
    

0
投票
检查 SelectionChanged 中的 e.AddedItems.Count > 0。

© www.soinside.com 2019 - 2024. All rights reserved.