1

I am making a notepad program and I am having a problem; On my New Button, I have this code:

private void New()
    {
        if (us == true)
        {
            DialogResult dr = MessageBox.Show("Do you want to save changes to: " + filepath, "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
                Save();
            else if (dr == DialogResult.No)
            {
                filename = null;
                undoToolStripMenuItem.Enabled = false;
                undoToolStripMenuItem1.Enabled = false;
                redoToolStripMenuItem.Enabled = false;
                redoToolStripMenuItem1.Enabled = false;
                us = false;
                Form1.ActiveForm.Text = "Untitled - PadNotePro";
                richTextBox1.Clear();
            }
            else if (dr == DialogResult.Cancel)
                Close();
        }
        else
        {
            filename = null;
            undoToolStripMenuItem.Enabled = false;
            undoToolStripMenuItem1.Enabled = false;
            redoToolStripMenuItem.Enabled = false;
            redoToolStripMenuItem1.Enabled = false;
            Form1.ActiveForm.Text = "Untitled - PadNotePro";
            richTextBox1.Clear();
        }
    }

us means un-saved, it is to see if it is saved, if us = true, it is not saved.

When I click no on my DialogBox, it runs this code:

else if (dr == DialogResult.No)
            {
                filename = null;
                undoToolStripMenuItem.Enabled = false;
                undoToolStripMenuItem1.Enabled = false;
                redoToolStripMenuItem.Enabled = false;
                redoToolStripMenuItem1.Enabled = false;
                us = false;
                Form1.ActiveForm.Text = "Untitled - PadNotePro";
                richTextBox1.Clear();
            }

What I am having a problem with, is the: Form1.ActiveForm.Text = "Untitled - PadNotePro";, it seems like it is skipping that line of code. I think it has something to do with the MessageBox, but can't figure it out. Does anyone know why?

EDIT: I think it might have something to do with the Form not being active at the time.

4

3 回答 3

1

我自己已经弄清楚了我的问题。您不能FormMessageBox. 你必须使用一个BackgroundWorker. 作为参考,这是我使用的代码:

BackgroundWorker changeformtext = new BackgroundWorker();
public Form1()
{
    InitializeComponent();
    changeformtext.DoWork += changeformtext_DoWork;
}
void changeformtext_DoWork(object sender, DoWorkEventArgs e)
{
    Invoke(new Action(doit));
}
void doit()
{
    this.Text = "Untitled - PadNotePro";
}
private void New()
{
    if (us == true)
    {
        DialogResult dr = MessageBox.Show("Do you want to save changes to: Untitled?", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
        if (dr == DialogResult.Yes)
            Save();
        else if (dr == DialogResult.No)
        {
            changeformtext.RunWorkerAsync();
        }
        else if (dr == DialogResult.Cancel)
            Close();
    }
    else
    {
        changeformtext.RunWorkerAsync();
    }
}
于 2013-08-19T01:26:57.907 回答
0

What does the field/property 'ActiveForm' return? We won't be able to answer your question unless you give us a little more info about what the problem is/what error you're getting.

EDIT:

Use this method to change a form's text:

    public void ChangeFormText(Form form, string text)
    {
        form.Text = text;
    }
于 2013-08-16T02:15:28.043 回答
0

You should not call the static property on Form1. Instead, use the instance property to access the caption:

this.Text = "Untitled - PadNotePro";

It seems that the "active form" became the MessageBox instead of the form itself. Also, it's generally a good OOP practice to use instance methods on the current object instead of going though a static property to reach the same place.

于 2013-08-16T02:15:51.667 回答