简体   繁体   English

从另一种形式调用ComboBox

[英]Call ComboBox from another form

I have 2 forms - Form1 and Form2, Form1 have ComboBox, Form2 property has FormBorderStyle as FixedToolWindow. 我有2种形式-Form1和Form2,Form1具有ComboBox,Form2属性具有FormBorderStyle作为FixedToolWindow。 I need to call ComboBox from Form2, how do I? 我需要从Form2调用ComboBox,该怎么办?

I see the that the question has been reworded and the meaning has changed significantly. 我看到问题已被改写,其含义已发生重大变化。

The simple fact is that Form2 shouldn't even know that the ComboBox exists or even that Form1 exists for that matter. 一个简单的事实是,Form2甚至不应该知道ComboBox的存在,甚至就不知道Form1的存在。 The proper way to do this is for Form2 to raise an event and for Form1 to handle that event. 正确的方法是让Form2引发一个事件,并让Form1处理该事件。 When something needs to happen, Form2 raises the event and then Form1 accesses its own ComboBox. 当需要发生某些事情时,Form2引发事件,然后Form1访问其自己的ComboBox。

Check this out . 检查一下 Part 3 explains how it SHOULD be done. 第3部分将说明应如何完成。

Here's a relatively contrived example: 这是一个相对人为的示例:

Form1: 表格1:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Form2 form2Instance;

        private void button1_Click(object sender, EventArgs e)
        {
            // Check whether there is already a Form2 instance open.
            if (this.form2Instance == null || this.form2Instance.IsDisposed)
            {
                // Create a new Form2 instance and handle the appropriate event.
                this.form2Instance = new Form2();
                this.form2Instance.SelectedValueChanged += form2Instance_SelectedValueChanged;
            }

            // Make sure the Form2 instance is displayed and active.
            this.form2Instance.Show();
            this.form2Instance.Activate();
        }

        private void form2Instance_SelectedValueChanged(object sender, EventArgs e)
        {
            // Update the ComboBox based on the selection in Form2.
            this.comboBox1.SelectedIndex = this.form2Instance.SelectedValue;
        }
    }
}

Form2: 表格2:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the value selected in the NumericUpDown as an int.
        /// </summary>
        public int SelectedValue { get { return Convert.ToInt32(this.numericUpDown1.Value); } }

        /// <summary>
        /// Raised when the value in the NumericUpDown changes.
        /// </summary>
        public event EventHandler SelectedValueChanged;

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (this.SelectedValueChanged != null)
            {
                // Notify any listeners that the selection has changed.
                this.SelectedValueChanged(this, EventArgs.Empty);
            }
        }
    }
}

As you can see, Form2 has no reference to Form1 at all so doesn't need to know or care whether Form1 exists. 如您所见,Form2完全没有引用Form1,因此不需要知道或关心Form1是否存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM