I want to fill a combobox on Form1 , when the OK button on Form2 is clicked.
First, the Load Form2 button on Form1 is clicked to display Form2. 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.
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 :
public partial class FORM2 : Form
{
public FORM2(SqlConnection connfromFORM3)
{
Form2Conn = connfromFORM3;
InitializeComponent();
}
private Form1 form1;
public SELECTGROUPHEADDIALOG(FORM1 form1) : this()
{
this.fo开发者_运维百科rm1 = 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
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. 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. If the DialogResult is OK, then you know you should fill your ComboBox. As far as returning the data from Form2, you need to expose a property or field that Form1 can access. Here is a code example:
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
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. You control the DialogResult by setting it when you are ready to close the form. The 'Items' field would be your array or collection of returned data from your datasource.
精彩评论