【问题标题】:displaying a combobox selection on a different from in C#在与 C# 不同的地方显示组合框选择
【发布时间】:2018-08-08 07:19:00
【问题描述】:

我有一个主表单、表单 A、表单 B 和表单 C,它们都包含组合框。我希望表单 A 或 B 或 C 中的选择显示在主表单上的文本框或标签上。我怎么做?我想在没有按钮的情况下做到这一点。只需从下拉菜单中选择一个项目并将其显示在主窗体上即可。

请帮忙!!!

非常感谢!

【问题讨论】:

  • 如果没有关于您的项目的更多信息,很难确定,但如果您使用的是 MV*(或类似)方法,那么您可以在组合框更改时发送消息。主要形式可以接受这一点。或者,主窗体可以订阅子窗体上的事件。当组合框更改时,将触发这些事件。然而,这一切听起来很可疑,就像你的设计是错误的。如果您解释为什么要这样做,将会很有帮助。也许我们可以提出更好的方法。
  • 在显示时将标签或文本框实例从主窗体传递给 A、B 或 C 窗体构造函数,并在 selectedchange 方法中使用文本框或标签来显示值。

标签: c# combobox textbox label


【解决方案1】:

您的问题基本上归结为根据另一个表单的事件在一个表单上做某事

在我看来,最好的方法是:

  • Main 自己执行所有操作。
  • FormA(和其他人)自己做所有的事情。
  • 如果您需要根据FormA 上发生的事件在Main 上做某事,让FormA 通知Main 那里发生了一些事情,所以去做你自己的事情吧。
  • 如有必要,将数据从FormA 传递到Main

所以我会为此目的使用delegates

在我的Main 中,我会声明一个带有这样签名的公共代表:

public delegate void NotifyComboBoxChange(string item);

也就是说,这个委托代表了一个方法,它接受一个string参数,并且返回类型为void。这个想法是让FormA“调用”Main 中的一个方法,并且该方法实质上通知组合框项目在FormA 上已更改。因此,如果我们有办法从FormA 调用驻留在Main 中的方法,那么我们就可以通知Main FormA 上发生的事件到目前为止还有我吗?

现在,如果我在Main 中编写这样的方法,并让它从FormA 调用,那应该可以实现我们的目标。这里,txtLabelTextBlock 中的Main

public void ComboBoxValueChanged(string value)
{
    txtLabel.Text = value;
}

要调用这样的方法,我将在Main 中定义一个NotifyComboBoxChange 类型的委托,如下所示,并向其注册ComboBoxValueChanged() 方法。您的 Main 代码应如下所示:

public delegate void NotifyComboBoxChange(string item);

public partial class Form1 : Window
{
    public NotifyComboBoxChange notifyDelegate;
    FormA formA = null;

    public Form1()
    {
        InitializeComponent();
        // This is 'registering' the ComboBoxValueChanged method to the delegate.
        // So, when the delegate is invoked (called), this method gets executed.
        notifyDelegate += new NotifyComboBoxChange(ComboBoxValueChanged);
    }

    public void ComboBoxValueChanged(string value)
    {
        txtLabel.Text = value;
    }

    private void btnOpen_Click(object sender, RoutedEventArgs e)
    {            
        // Passing the delegate to `FormA`
        formA = new FormA(notifyDelegate);
        formA.Show();
    }
}

因此,现在我们需要修改我们的FormA。当组合框选择发生变化时,我们需要告诉我们调用哪个委托。所以要做到这一点,我会像这样在FormA 的构造函数中传递委托:

public partial class FormA : Window
{
    NotifyComboBoxChange notifyDel;

    public FormA(NotifyComboBoxChange notify)
    {
        InitializeComponent();
        notifyDel = notify;
    }

    private void cmbItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // This gets the currently selected value in the CombBox
        var value = cmbItems.SelectedValue.ToString();
        // This invokes the delegate, which in turn calls the ComboBoxValueChanged() method in Main.
        notifyDel?.Invoke(value);
    }
}

【讨论】:

    【解决方案2】:

    Modifier 的主窗体 Control 属性设置为 public 或继承您的 从控制窗口属性中自己选择。

    显示时将 Main-form 对象 (this) 传递给 FormA,FormB,FormC 他们。现在我在按钮单击时显示它们。

    private void showingAllForm_Click(object sender, EventArgs e)
        {
            FormA a = new FormA(this);
            a.Show();
            FormB b = new FormB(this);
            b.Show();
            FormC c = new FormC(this);
            c.Show();
            this.IsMdiContainer = true;
            a.MdiParent = this;
            c.MdiParent = this;
            b.MdiParent = this;
        }
    

    设置FormA,FormB,FormC的构造函数,并在FormA,FormB,FormC上添加Combobox,以便为每个组合框添加以下代码,例如

    FormA 替换为 FormB、FormC 等。

    public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }
        MainForm fm;
        public FormA(MainForm fm)
        {
            InitializeComponent();
            this.fm = fm;
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            fm.textBox1.Text = comboBox1.Text;
        }
    }
    

    输出结果

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多