简体   繁体   English

从另一个表单调用一个表单上的方法,如果单击Form2上的一个按钮,则在Form1上填充组合框

[英]calling method on one form from another form, fill combobox on Form1 if a button on Form2 is clicked

I want to fill a combobox on Form1 , when the OK button on Form2 is clicked. 我想在Form1上填充一个组合框,当单击Form2上的OK按钮时。

First, the Load Form2 button on Form1 is clicked to display Form2. 首先,单击Form 1上的Load Form 2按钮以显示Form 2。 Then, Form2 appears, and if OK(Button on Form2) is pressed then the Form1 ComboBox must be filled with values from a SQL SERVER database table. 然后,出现Form2,如果按下OK(Form2上的按钮),则必须使用SQL SERVER数据库表中的值填充Form1 ComboBox。

public partial class FORM1 : Form
{
     public void LoadValue()
        {
            string query = "SELECT FullName FROM Studs ";

            SqlCommand cmd = new SqlCommand(query, FORM1conn);

                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    FORM1COMBOBOX.Items.Add(reader.GetString(0));
                }
        }
}

public partial class FORM2 : Form
{
        private void FORM2_OK_Button_Click(object sender, EventArgs e)
        {
               //HERE I WANT TO CALL THE  LOADVALUE() METHOD OF FORM1  ????????
         }
}

在此输入图像描述

EDIT: In form2.cs : 编辑:在form2.cs:

public partial class FORM2 : Form
{

    public  FORM2(SqlConnection connfromFORM3)
    {
        Form2Conn = connfromFORM3;
        InitializeComponent();
    }

    private Form1 form1;
    public SELECTGROUPHEADDIALOG(FORM1 form1) : this()
    {
        this.form1 = form1;
    }

        private void FORM2_OK_Button_Click(object sender, EventArgs e)
        {
               //HERE I WANT TO CALL THE  LOADVALUE() METHOD OF FORM1  ????????
         }
}

Updated 更新

You can try passing Form1 instance in the constructor of Form2 您可以尝试在Form2的构造函数中传递Form1实例

Example: 例:

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2(this);
        form2.Show();
    }

    internal void TestMethod()
    {
        throw new NotImplementedException();
    }
}

public partial class Form2 : Form
{
    private Form1 form1;

    public Form2()
    {
        InitializeComponent();
    }

    public Form2(Form1 form1)
        : this()
    {
        // TODO: Complete member initialization
        this.form1 = form1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        form1.TestMethod();
    }
}

Updated 更新

try this: 尝试这个:

public  FORM2(SqlConnection connfromFORM3, FORM1 form1)
{
    this.form1 = form1;
    Form2Conn = connfromFORM3;
    InitializeComponent();
}

Your design sounds like Form2 is used as a Dialog. 您的设计听起来像Form2用作对话框。 While your design approach is good, the implementation and architecture is not. 虽然您的设计方法很好,但实现和架构却不是。 When you show Form2, you should use the ShowDialog method call, and wait for a DialogResult. 当您显示Form2时,您应该使用ShowDialog方法调用,并等待DialogResult。 If the DialogResult is OK, then you know you should fill your ComboBox. 如果DialogResult没问题,那么你知道你应该填充你的ComboBox。 As far as returning the data from Form2, you need to expose a property or field that Form1 can access. 至于从Form2返回数据,您需要公开Form1可以访问的属性或字段。 Here is a code example: 这是一个代码示例:

Form1.cs Form1.cs的

namespace CrossFormAccess {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

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

        private void ShowForm2(object sender, EventArgs e) {
            using (Form2 form = new Form2()) {
                DialogResult result = form.ShowDialog();
                if (result == DialogResult.OK) {
                    comboBox1.Items.Clear();
                    comboBox1.Items.AddRange(form.Items);
                }
            }
        }
    }
}

Form2.cs Form2.cs

namespace CrossFormAccess {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

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

        public object[] Items;

        private void DoWork(object sender, EventArgs e) {
            Items = new object[] { "hello", "world" };
            DialogResult = DialogResult.OK;
        }
    }
}

Form1 just has a ComboBox and Button on it where the button shows form2, and Form2 has just a button on it that calls DoWork. Form1上只有一个ComboBox和Button,按钮显示form2,而Form2上只有一个调用DoWork的按钮。 You control the DialogResult by setting it when you are ready to close the form. 您可以在准备关闭表单时通过设置来控制DialogResult。 The 'Items' field would be your array or collection of returned data from your datasource. “Items”字段将是您的数组或数据源返回数据的集合。

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

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