locked
Dynamically populate and update dropdown from another form RRS feed

  • Question

  • I have one dropdown for city in form frmHealth.Now what i want is to dynamically populate this dropdown when user opens another form for master entry for city and enters city.I am aware this can be done through some custom property change event but not able to recollect.If somebody can post sample code for this then it would be very great.

    Thanks in advance(as always).
    Thanks and Regards,
    Monday, November 21, 2011 1:26 PM

Answers

  • Code for second form

        public partial class Form2 : Form
        {
            public string City { get; set; }
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
            }
    
            void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                City = textBox1.Text;
            }
        }
    
    

    Code for Main form

            Form2 frm = new Form2();
            private void AddCity_Click(object sender, EventArgs e)
            {
                frm.Show();
                frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            }
    
            void frm_FormClosed(object sender, FormClosedEventArgs e)
            {
                MessageBox.Show(frm.City);
            }
    
    



    Thanks,
    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    Dot Net Goodies
    • Proposed as answer by Neddy Ren Wednesday, November 23, 2011 9:08 AM
    • Marked as answer by Neddy Ren Thursday, December 1, 2011 5:29 AM
    Monday, November 21, 2011 1:36 PM

All replies

  • Code for second form

        public partial class Form2 : Form
        {
            public string City { get; set; }
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
            }
    
            void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                City = textBox1.Text;
            }
        }
    
    

    Code for Main form

            Form2 frm = new Form2();
            private void AddCity_Click(object sender, EventArgs e)
            {
                frm.Show();
                frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            }
    
            void frm_FormClosed(object sender, FormClosedEventArgs e)
            {
                MessageBox.Show(frm.City);
            }
    
    



    Thanks,
    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    Dot Net Goodies
    • Proposed as answer by Neddy Ren Wednesday, November 23, 2011 9:08 AM
    • Marked as answer by Neddy Ren Thursday, December 1, 2011 5:29 AM
    Monday, November 21, 2011 1:36 PM
  • I think  A.m.a.L Hashim's code is perfect.

    If you have any questions regarding to the issue, please feel free to tell us.

    Best Regards


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, November 23, 2011 9:14 AM