【问题标题】:How to update a ComboBox from a window when closing another window?关闭另一个窗口时如何从一个窗口更新组合框?
【发布时间】:2013-11-21 02:32:52
【问题描述】:

首先,我要道歉:

  • 该软件为葡萄牙语。
  • 这个软件丑得要命。这是一个学校项目,我们决定更多地关注功能而不是设计(我知道,这是错误的,但我们必须选择......)
  • 我阅读了update combobox from another form in c#,但我不明白发生了什么。

话虽如此,让我们进入正题。

我有这个窗口:

如果我点击红色标记的按钮:

这将打开:

这应该是面向市场的软件。第一个窗口负责向库存订购更多的东西。第二个窗口负责将供应商添加到系统中。

组合框显示系统上的所有供应商。我希望当我点击红色矩形突出显示的按钮后,在第二个窗口上完成添加供应商时,组合框将使用新数据自动更新。

我在这段代码中使用了“更新”按钮:

this.tb_FornecedorTableAdapter.Fill(this.tccDataSet.tb_Fornecedor);

它有效,但我尝试在其他窗口上使用FormClosing、FormClosed 和Deactivate 事件,但它根本不起作用(我将代码上的“this”修改为很多这并没有帮助我)。有没有办法做我想做的事?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    如果 ComboBox 使用来自 SQL Server 的数据进行了更新,那么您可以尝试以下操作:

    // When button Adicionar is clicked
    private void buttonAdd_Click(object sender, EventArgs e)
    {
        using(Form formAdd = new Form()) // This is the Gerenciar Fornecedor form
        {
            formAdd.ShowDialog(this); // Show the form. The next statement will not be executed until formAdd is closed
            // Put the your code to update the ComboBox items here
        }
    }
    

    【讨论】:

    • 我正在测试所有建议,但到目前为止您的代码仍然有效。谢谢。 :D
    • @athosbr99 - 以这种方式使用formAdd 会导致内存泄漏,如果在正常使用过程中多次显示,就会很明显。最好将其包装在 using 子句中,例如 using (var formAdd = new FormAdd()) { ...}。请参阅 MSDN 上关于在 ShowDialog() 之后调用 Dispose() 的说明。
    【解决方案2】:

    在第一个窗口中声明一个公共方法:

    public void RefreshCombo()
    {
    this.tb_FornecedorTableAdapter.Fill(this.tccDataSet.tb_Fornecedor);
    }
    

    然后在第一个窗口中添加按钮点击事件

    WindowB window=new WindowB(this);
    WindowB.Show();
    

    然后在子窗口中添加一个ctor方法:

    private WindowA windowParent;
    
    public WindowB(WindowA parent)
    {
    InitializeComponent();
    this.windowParent=parent;     
    }
    

    在 WindowB FormClosing 事件中

    this.windowParent.RefreshCombo()
    

    【讨论】:

    • 你的第三段代码,你能解释一下它的作用吗?我无法理解
    • 存储一个WindowA实例,方便你在WindowB中使用实例。试试这段代码调试一下,你就明白了。
    • 抱歉,图片打不开,有异常吗?
    • 不,这会出现 msdn.microsoft.com/en-us/library/…。这是预期的吗?您输入的是“parent”而不是“Parent”,所以我认为您指的是变量而不是类
    • 是的,你是对的,我想使用变量。试试this.windowParent.RefreshCombo()
    【解决方案3】:

    在这种情况下,您可以做的是在子窗体上添加一个属性来存储组合框值并在组合框值更改时填充它。此外,在子窗体上创建一个将从父窗体调用的方法。它将显示子窗体并返回组合框值。

    public partial class ChildForm : Form
    {
        public ChildForm()
        {
            InitializeComponent();
        }
    
        private string _comboValue { get; set; }
    
        public string ShowAndGetComboValue()
        {
            this.ShowDialog();
    
            return _comboValue;
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            _comboValue = comboBox1.SelectedItem.ToString();
        }
    }
    

    在父窗体上,您可以这样显示子窗体:

    ChildForm form = new ChildForm();
    string comboValue = form.ShowAndGetComboValue();
    

    【讨论】:

    • 我想我明白了你的逻辑,但什么样的东西会存储在“_combovalue”上?并且 ShowAndGetComboValue() 将在第二个窗口上创建?
    • 你可以看到_combovalue是如何使用的。是的,你是对的,ShowAndGetComboValue() 设置在第二个窗口中。
    • "comboBox1.Items.AddRange(new string[] { "1", "2", "3" });"添加范围?我从 SQL Server 获取数据......所以这会是个问题吗?
    • 是的,对不起,那是为了我的测试。我从上面的代码中删除了它。
    • 第一个代码块全部进入第二个窗口? “private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)”在那里不起作用,因为组合框在第一个屏幕上……我弄错了吗?
    猜你喜欢
    • 2022-01-07
    • 2016-09-24
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多