简体   繁体   English

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

[英]Textbox won't update when content passed from form 2 to form 1

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.我指的是我上一个问题的信息,它详细告诉我如何从表单 2 更新表单 1 中的文本框。

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.单击按钮时,它应该使用参数 ChangeTextBox 将内容显示到 MainWindow 中的 txtDisplay 中。 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.问候。

Don't write mainWindow = new MainWindow(); 不要写mainWindow = new MainWindow(); .
You want the existing MainWindow , not a new one. 您需要现有的MainWindow ,而不是新的。

I see that you create a new instance of MainWindow, you update the new instance, not the old one. 我看到您创建了MainWindow的新实例,更新了新实例,而不是旧实例。 Pass the mainwindow to the secondary window and change this instance, you'll see your changes. 将主窗口传递到辅助窗口并更改此实例,您将看到所做的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM