当内容从表单2传递到表单1时,文本框不会更新

时间:2011-11-03 13:08:39

标签: c# winforms

我指的是from my previous question信息,它详细告诉我如何从表单2更新表单1中的文本框。

我仔细检查了所有内容,但在更新文本框时仍然遇到问题。单击按钮时,它应使用参数ChangeTextBox将内容显示在MainWindow中的txtDisplay中。但事实并非如此。

我错过了什么吗?如果没有,我该如何解决这个问题?

有问题的代码:

        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();
            }
        }

希望得到一些帮助。

问候。

2 个答案:

答案 0 :(得分:3)

不要写mainWindow = new MainWindow(); 您想要现有的MainWindow,而不是新的。{/ p>

答案 1 :(得分:1)

我看到您创建了MainWindow的新实例,您更新了新实例,而不是旧实例。将主窗口传递到辅助窗口并更改此实例,您将看到更改。