1

I have declared one formClosing event procedure on form closing here is my code.I'm designing a notepad using c# and before closing the form or my notepad app it should ask user whether he/she wants to save it or not ..

private void Form13_FormClosing(object sender, FormClosingEventArgs e)
    {

        if (this.Text == "Untitled-FileEditor")
        {
            if (richTextBox1.Text.Length > 0)
            {
                DialogResult dr = MessageBox.Show("Do u want to save changes to untitled", "FileEditor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    saveFileDialog1.FileName = "NewText";
                    saveFileDialog1.Filter = "Text Files(*.txt)|*.txt";
                    DialogResult d = saveFileDialog1.ShowDialog();
                    if (d == DialogResult.OK)
                    {
                        string TextFilePath = saveFileDialog1.FileName;
                        richTextBox1.SaveFile(TextFilePath, RichTextBoxStreamType.PlainText);
                        richTextBox1.Text = "";
                    }
                }
                if (dr == DialogResult.No)
                {
                    //richTextBox1.Text = "";
                    this.Close();
                }
            }
            else if (richTextBox1.Text.Length == 0)
            { this.Close(); }
        }
        else if (this.Text != "Untitled-FileEditor")
        {
            if (richTextBox1.Modified)
            {
                DialogResult dr = MessageBox.Show("Do u want to save changes to " + openFileDialog1.FileName, "FileEditor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    saveFileDialog1.FileName = "NewText";
                    saveFileDialog1.Filter = "Text Files(*.txt)|*.txt";
                    DialogResult d = saveFileDialog1.ShowDialog();
                    if (d == DialogResult.OK)
                    {
                        string TextFilePath = saveFileDialog1.FileName;
                        richTextBox1.SaveFile(TextFilePath, RichTextBoxStreamType.PlainText);
                        richTextBox1.Text = "";
                    }
                }
                if (dr == DialogResult.No)
                {
                    //richTextBox1.Text = "";
                    this.Close();
                }
            }
            if (!richTextBox1.Modified)
            {
                this.Text = "Untitled-FileEditor";
                //richTextBox1.Text = "";
                this.Close();
            }
        }
    }

Error showing - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.

chikun
  • 199
  • 1
  • 15
  • Check the stack trace. – SLaks Dec 25 '15 at 17:30
  • You should name your forms and controls. – SLaks Dec 25 '15 at 17:31
  • Didn't get you .. Can you please elaborate – chikun Dec 25 '15 at 17:31
  • @SLaks already I've given names to my controls – chikun Dec 25 '15 at 17:32
  • Read the stack trace to see where you're calling something on the wrong thread. – SLaks Dec 25 '15 at 17:33
  • `richTextBox1` and `Form13` are not useful names. – SLaks Dec 25 '15 at 17:34
  • sorry I'm in little bit hurry to finish my assignment which i need to submit tomorrow . That's why i named it like that only .. Form13 is my form name – chikun Dec 25 '15 at 17:36
  • In this code there is nothing that triggers that exception. When/where is form_closing called? Oh, and we are in a hurry too so don't waste our time... – rene Dec 25 '15 at 17:37
  • 5
    Please take a look: http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the . This is exactly your case. – Alexey Chuikov Dec 25 '15 at 17:38
  • just give me a answer of simple question bro .. I want to perform some opertion before form closing .. On which event of form i should write the code ? – chikun Dec 25 '15 at 17:49
  • 1
    You are pretty rude bro... – rene Dec 25 '15 at 17:52
  • Dude what's wrong with u ? I'm new to .Net world. I'm trying as much as i can .. I've not used any words to hurt u bro .. Everybody is participating and answering my question.. Kindly say if u have any solution – chikun Dec 25 '15 at 18:04

1 Answers1

0

This means that you are trying to access/modify data from an other thread than it is allowd to.

Please try:

richtextBox1.Invoke(new Action(richtextBox1.Text = "sample text")); //just a sample

Instead of richtextBox1.Text = "sample text" you write the code of the line where the error appears.

Maybe you should perform your actions within OnClosed event:

protected override void OnClosed(EventArgs e)
{
    //your code here
    base.OnClosed(e);
}
cramopy
  • 3,459
  • 6
  • 28
  • 42
  • just give me a answer of simple question bro .. I want to perform some opertion before form closing .. On which event of form i should write the code ? – chikun Dec 25 '15 at 17:48
  • how can i override OnClosing event sir ? – chikun Dec 25 '15 at 17:50
  • tried above method .. still showing that exception.. and the exception raised in designer.cs file .. "base.Dispose(disposing);" -> on this line – chikun Dec 25 '15 at 17:55
  • yeah Brother thanx .. this overriden method helped me :) thanx a lot .. I have removed all this.close() functions and it helped me .. thanx a ton .. give this man a cookie – chikun Dec 25 '15 at 18:36
  • @chikun No problem, that what `SO` is here for :D cookie means upvote? :P – cramopy Dec 25 '15 at 18:38