【问题标题】:Textbox won't update when content passed from form 2 to form 1当内容从表单 2 传递到表单 1 时,文本框不会更新
【发布时间】:2011-11-03 22:07:44
【问题描述】:

我指的是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();
            }
        }

希望得到一些帮助。

问候。

【问题讨论】:

  • 很高兴您发布了您的代码,但如果您准确描述问题所在会有所帮助。
  • @MusiGenesis:好点子。我已经更新了我的 OP。

标签: c# winforms


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      相关资源
      最近更新 更多