2

I am referring to the information from my previous question which tells me in details how to update textbox in form 1 from form 2.

I double checked everything and I still have a problem while updating the textbox. When button clicked it should display content into txtDisplay in MainWindow by using parameter ChangeTextBox. But it doesn't.

Have I missed something here? If not, how can I correct this problem?

Code with the problem:

        private void btnAddEntry_Click(object sender, EventArgs e)
        {
            // Making sure that type is selected.
            if (cmbType.SelectedIndex == -1)
            {
                MessageBox.Show("Please select entry type!", "Error!", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            // Each field must be filled for specified type.
            // Here we are checking if all fields were filled.
            else if ((cmbType.SelectedIndex == 0 && (txtUserName.Text == string.Empty || txtPassword.Text == string.Empty)) ||
                (cmbType.SelectedIndex == 1 && (txtURL.Text == string.Empty || txtPassword.Text == string.Empty)) ||
                (cmbType.SelectedIndex == 2 && (txtSoftwareName.Text == string.Empty || txtSerialCode.Text == string.Empty)))
            {
                MessageBox.Show("Please fill all the fields!", "Error!", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                int totalEntries = 0;

                mainWindow = new MainWindow();

                if(cmbType.SelectedIndex == 0)
                {
                    addedEntry.Add(new AddPC(cmbType.Text, txtUserName.Text, txtPassword.Text));
                }
                else if(cmbType.SelectedIndex == 1)
                {
                    addedEntry.Add(new AddWebSite(cmbType.Text, txtUserName.Text, txtPassword.Text, txtURL.Text));
                }
                else if(cmbType.SelectedIndex == 2)
                {
                    addedEntry.Add(new AddSerialCode(cmbType.Text, txtSoftwareName.Text, txtSerialCode.Text));
                }

                StringBuilder stringBuilder = new StringBuilder();

                foreach (var list in addedEntry)
                {
                    if (list is AddPC)
                    {
                        totalEntries++;

                        AddPC tmp = (AddPC)list;

                        stringBuilder.Append(tmp.ToString());
                    }
                    else if (list is AddWebSite)
                    {
                        totalEntries++;

                        AddWebSite tmp = (AddWebSite)list;

                        stringBuilder.Append(tmp.ToString());
                    }
                    else if (list is AddSerialCode)
                    {
                        totalEntries++;

                        AddSerialCode tmp = (AddSerialCode)list;

                        stringBuilder.Append(tmp.ToString());
                    }
                }

                mainWindow.ChangeTextBox = stringBuilder.ToString();

                // The foreach loop works and display because content is showing here.
                MessageBox.Show(stringBuilder.ToString());

                // Clearing all fields.
                ClearFields();
            }
        }

Hope for some help.

Regards.

Community
  • 1
  • 1
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

2 Answers2

4

Don't write mainWindow = new MainWindow();.
You want the existing MainWindow, not a new one.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

I see that you create a new instance of MainWindow, you update the new instance, not the old one. Pass the mainwindow to the secondary window and change this instance, you'll see your changes.

Toto
  • 149
  • 2