1

I have set up my program so that the user can enter a new line into the combo box via a text box on a separate form (Popup form). So far the program allows the new entry and closes the popup form when the user presses the "Accept" button however the entry does not appear in the combobox and the entry is not saved.

Currently the only way to view the new entry is by the .ShowDialog(); function which opens a second instance of the first form.

Form 2

namespace RRAS
{
    public partial class NewRFRPopup : Form
    {
        public NewRFRPopup()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            formRRAS main = new formRRAS();
            string newRFR = txtNewRFR.Text;
            main.AddRFR(newRFR);
            this.Close();
            main.ShowDialog();
        }

        private void NewRFRPopup_Load(object sender, EventArgs e)
        {

        }
    }
}

AddRFR in Form 1

 public void AddRFR(object item)
        {
            cmbRFR.Items.Add(item);
        }
iiAaronXiX
  • 93
  • 2
  • 3
  • 13
  • You may need an event handler... see example in the answer to [this question](http://stackoverflow.com/questions/24731998/winforms-refresh-parent-form) – swinkel Feb 04 '16 at 19:10
  • Maybe refreshing the combo box works. Try cmbRFR,Refresh(); after. – Swen Feb 04 '16 at 19:22

1 Answers1

0

you are creating a new instance of your form1 in the accept handler:

formRRAS main = new formRRAS();

(which is why when you call showdialog you get another formRRAS appearing).

You need to pass the original formRRAS to the popup and call AddRFR on the instance passed through. I'd pass it on the constructor of the popup - i.e.

public partial class NewRFRPopup : Form
{
formRRAS _main;

 public NewRFRPopup(formRRAS main)
 {
  InitializeComponent();
  _main = main;
 }

and then in your Accept handler:

 string newRFR = txtNewRFR.Text;
 _main.AddRFR(newRFR);
 this.Close();

and of course to show the popup from formRRAS

NewRFRPopup popup = new NewRFRPopup (this);
popup.ShowDialog();
NDJ
  • 5,189
  • 1
  • 18
  • 27
  • Awesome that worked for making it so that there doesn't need to be another instance of formRRAS, is there a way to set it up so that the item is saved into the combobox permanently? – iiAaronXiX Feb 04 '16 at 20:45
  • you'd need to persist it somewhere, database, file, etc. – NDJ Feb 04 '16 at 20:49