1

我有一个带有 2 个数据绑定列表框和两个数据绑定组合框的表单。我正在使用类型化的数据集。控件绑定到具有以下架构和来自该架构的数据的一对表。一listbox和一comboBox绑定在bar桌子上;另一个listboxcomboBox绑定到foo表上。

当为 foo 触发 SelectedIndexChanged 事件时,listBox我得到 Bar 中 Selected Text 的当前值listBoxcomboBox.

但是,当我使用 foocomboBox并尝试访问事件barComboBox.SelectedText内部时, FooComboBox_SelectedIndexChanged我会从中获取先前选择的值SelectedText而不是新值。给BarListBox.Selected了我当前的价值。

请注意,我使用FooListBox来进行选择,两个事件处理程序都按预期运行。

谁能解释这里发生了什么以及如何解决这个问题?

带有示例数据的表单屏幕截图:

在此处输入图像描述

数据集设计器:

在此处输入图像描述

form1.cs 代码:

//Standard using statements and namespace info

    public partial class Form1 : Form
    {
        //Loading DataSets and initializing here

        private void FooListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine("The value in the bar ListBox is {0}", barListBox.Text);
        }

        private void FooComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine("The value in the bar comboBox is {0}", barComboBox.Text);
        }
    }
4

1 回答 1

0

正如您解释的那样,我没有发现任何行为。

请检查此代码,请纠正我复制的代码是错误的。

我使用了 Combo Box 项目,并且 ListBox 项目从 Some webServiceMethod 作为 DataSource 添加到 FormLoad

在 Form1.Designer.cs

this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(32, 55);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(188, 21);
            this.comboBox1.TabIndex = 0;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(261, 55);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(255, 95);
            this.listBox1.TabIndex = 1;
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);

*在 Form1.cs 中*

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       MessageBox.Show("The value in the bar comboBox is "+ comboBox1.Text);
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("The value in the bar comboBox is "+ listBox1.Text);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        WebServiceRef.CM_ServiceSoapClient soapClient = new WebServiceRef.CM_ServiceSoapClient();

        comboBox1.DataSource = soapClient.GetAllCategories();

        listBox1.DataSource = soapClient.GetAllCategories();
    }

我已经根据 DataSource 表单 WebService 方法更改了数据,但当您解释您的行为时,我仍然没有发现任何问题。

于 2013-02-13T05:49:20.713 回答