0

What I'm trying to do is get user input from a form (IncomeForm) by using a textbox (TextBoxIncomePrice) and after pressing the button (ButtonConfirmIncome), the LabelIncome label in the form MainPage should change to the value input by the user.

Everything works as intented, except for when I try to reopen the IncomeForm by clicking the AddIncomeButton. I get the error shown in the title. It should be able to reopen and accept a new value no matter how many times you close the IncomeForm.

Main Form (MainPage):

IncomeForm incomeForm = new IncomeForm();

        private void incomeForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LabelIncome.Text = incomeForm.TextBoxIncomePrice.Text;            
        }

        private void AddIncomeButton_Click(object sender, EventArgs e)
        {
            incomeForm.FormClosed += new FormClosedEventHandler(incomeForm_FormClosed);
            incomeForm.Show();
        }

Add Income Form (IncomeForm):

private void ButtonConfirmIncome_Click(object sender, EventArgs e)
        {              
            this.Close();        
        }
isidoros _
  • 11
  • 4
  • 2
    When you close it you need to re-create it `new`. Maybe you want to just `Hide` it? [MSDN](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.close?view=netcore-3.1): _When a form is closed, all resources created within the object are closed and the form is disposed._ – TaW May 08 '20 at 11:27
  • Hiding does not work for what I'm trying to achieve. I tried multiple ways of re-creating the form after closing it but I haven't been able to solve it. Thanks for replying. – isidoros _ May 08 '20 at 13:42
  • _I tried multiple ways of re-creating the form_ What else than `incomeForm = new IncomeForm(); incomeForm.Show();` would you need??? – TaW May 08 '20 at 13:47
  • 1
    Ok, that's embarassing. I tunnel visioned so hard on doing it completly differently because this didnt work at first, but all it needed was this line of code `incomeForm = new IncomeForm();` in `AddIncomeButton_Click`. I don't even remember what I was doing wrong at first. Thanks for the help – isidoros _ May 08 '20 at 14:33

0 Answers0