|
Main form Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ComboboxTest
{
// define a delegate
public delegate void MyInvoke (string txt);
public partial class Form1: Form
{
public Form1 ()
{
InitializeComponent ();
}
private void button1_Click (object sender, EventArgs e)
{
Form2 frm = new Form2 (new MyInvoke (UpdateCombobox));
frm.ShowDialog (this);
}
private void UpdateCombobox (string txt)
{
//Add to
this.comboBox1.Items.Add (txt);
// Select the newly added item
this.comboBox1.SelectedIndex = this.comboBox1.Items.Count-1;
}
}
}
Subform Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ComboboxTest
{
public partial class Form2: Form
{
private MyInvoke mi = null;
public Form2 (MyInvoke myInvoke)
{
InitializeComponent ();
mi = myInvoke;
}
private void button1_Click (object sender, EventArgs e)
{
this.mi (this.textBox1.Text.Trim ());
this.Close ();
}
}
}
For a detailed explanation, please see the original post .. |
|