13 Replies - 1348 Views - Last Post: 06 December 2010 - 06:38 AM Rate Topic: -----

#1 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

open save new file

Posted 04 December 2010 - 05:17 PM

im doing my own not pad but im having a bit of a problem

if i press open a file... it doesnt save the old file open
if i open a file and dont modify it and press new it still asks me if i wanna save
if i open a file and dont modify it and press save it performs a save as instead
if i open a file and dont modify it and press exit it still asks me if i wanna save it


here is my code

private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
        
            if (richTextBox1.Modified )
            {
                DialogResult response;
                response = MessageBox.Show("Do you Want to save changes to Document?", "Confirm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (response == DialogResult.No)
                {
                    richTextBox1.Clear();
                    this.Text = this.Text;
                }
                if (response == DialogResult.Cancel)
                {
                    richTextBox1.Show();
                }
                if (response == DialogResult.Yes)
                {
                    saveToolStripMenuItem_Click(sender, e);
                    richTextBox1.Clear();
                    this.Text = this.Text;
                }
            }
            else
            {
                richTextBox1.Clear();
                this.Text = this.Text;
            }

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            string fileChosen = "";

            openFileDialog1.InitialDirectory = "C:|Libraries|Documents";
            openFileDialog1.Title = "Open";
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Rich Text Files|*.rtf|Text Files|*.txt";
           

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileChosen = openFileDialog1.FileName;
                richTextBox1.LoadFile(fileChosen, RichTextBoxStreamType.RichText);
                this.Text = openFileDialog1.FileName;

            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileSaved = "";

            saveFileDialog1.InitialDirectory = "C:|Libraries|Documents";
            saveFileDialog1.Title = "Save As";
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter = "Rich Text Files|*.rtf|Text Files|*.txt";
            
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                fileSaved = saveFileDialog1.FileName;
                richTextBox1.SaveFile(fileSaved, RichTextBoxStreamType.RichText);
                this.Text = saveFileDialog1.FileName;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {


            if (saveFileDialog1.FileName != ""  )
            {
                richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
            }
            else
            {
                string fileSaved = "";
                saveFileDialog1.InitialDirectory = "C:|Libraries|Documents";
                saveFileDialog1.Title = "Save";
                saveFileDialog1.FileName = "";
                saveFileDialog1.Filter = "Rich Text Files|*.rtf|Text Files|*.txt";
                


                if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    fileSaved = saveFileDialog1.FileName;
                    richTextBox1.SaveFile(fileSaved, RichTextBoxStreamType.RichText);
                    this.Text =  saveFileDialog1.FileName;
                }

            }
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectionLength > 0)
            {
                richTextBox1.Cut();
            }
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectionLength > 0)
            {
                richTextBox1.Copy();
            }
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified == true)
            {
                DialogResult response;
                response = MessageBox.Show("Do you Want to save changes to Document?", "Confirm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (response == DialogResult.No)
                {
                    this.Close();
                }
                if (response == DialogResult.Cancel)
                {
                    richTextBox1.Show();
                }
                if (response == DialogResult.Yes)
                {
                    saveToolStripMenuItem_Click(sender, e);
                }

            }
            else
            {
                this.Close();
            }





Can anybody tell me wat am im doing wrong????
thanks

This post has been edited by Sergio Tapia: 04 December 2010 - 05:29 PM
Reason for edit:: Remember to use CODE tags.


Is This A Good Question/Topic? 0
  • +

Replies To: open save new file

#2 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 04 December 2010 - 05:38 PM

Anyone???? Help please!!!
Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: open save new file

Posted 04 December 2010 - 05:41 PM

You'll get help eventually, relax. People here are volunteers not staff. It's a Saturday night, and most people are not here.
Was This Post Helpful? 0
  • +
  • -

#4 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 04 December 2010 - 05:47 PM

sorry...
;-(
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: open save new file

Posted 04 December 2010 - 05:58 PM

Didn't mean for the post to sound like scolding, I was just explaining why you didn't get help yet. :P
Was This Post Helpful? 0
  • +
  • -

#6 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 04 December 2010 - 06:05 PM

neva mind....im just stressed...lol...
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: open save new file

Posted 04 December 2010 - 08:25 PM

Quote

im doing my own not pad but im having a bit of a problem

  • if i press open a file... it doesnt save the old file open
  • if i open a file and dont modify it and press new it still asks me if i wanna save
  • if i open a file and dont modify it and press save it performs a save as instead
  • if i open a file and dont modify it and press exit it still asks me if i wanna save it


Item 1: Then tell it to save the file.
Items 2-4: You need to track whether or not a change has been made. This is usually called making the file 'dirty'. Create a boolean. Something creative like: IsDirty. When you make a change, set IsDirty to true. When you save, make IsDirty false. Then check its condition when doing the other things.

If (IsDirty) AskToSave();

Personally I use a property so I can do some other things when IsDirty is set and cleared.

bool _isdirty = false;
bool IsDirty
{
  get { return _isdirty; }
  set 
    {
        _isdirty = value;
        if (IsDirty) this.Text += "*"; // Add an asterisk to the window title to show it dirty
        else this.Text = this.Text.TrimEnd('*'); // Remove the asterisk when clean.
    }
}

Was This Post Helpful? 2
  • +
  • -

#8 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 05 December 2010 - 02:47 AM

should I do something like that for the richtextbox text changed method or on the form???
then add it to the save...open and new file???
How is the IsDirty gonna know if there was any changes in the richtextbox???
sorry but im new to c#...I tought i was doing good but now im completely lost
thanks for your help
Was This Post Helpful? 0
  • +
  • -

#9 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: open save new file

Posted 05 December 2010 - 03:14 AM

Hello there,

You could use the TextChanged event of the RichTexTbox. That way, you get notified when a change has been made in the RichTextBox, and you can react by changing the IsDirty property. Then, if you use IsDirty in your save, exit, open methods instead of the richTextBox1.Modified property, you should be able to save only when modifications have been made. The IsDirty property will be changed if any characters (including spaces) are added or taken away from the text box. Is that what you want?

You could probably even unregister the TextChanged event once it has been fired once, as you may not want it firing on every single change :).

This post has been edited by CodingSup3rnatur@l-360: 05 December 2010 - 03:16 AM

Was This Post Helpful? 0
  • +
  • -

#10 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 05 December 2010 - 05:57 AM

yeap...Ill try it now...thanks mate
Was This Post Helpful? 0
  • +
  • -

#11 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 05 December 2010 - 04:48 PM

still can get it to work properly

is dirty adds as much * as i add characters or spaces to the rtb...

could you explain it to me again... c if i can get it right this time

thanks
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: open save new file

Posted 05 December 2010 - 08:08 PM

View Postdorinha, on 05 December 2010 - 03:48 PM, said:

is dirty adds as much * as i add characters or spaces to the rtb...


Even without posting your modified code it is fairly obvious that you must be making IsDirty true with every textbox.TextChanged event.

Let's think this through...
  • Every time I change the text, I make it dirty.
  • Every time I make it dirty I add an * to the name.
  • I don't want to add an * over and over and over.
  • Therefore maybe I shouldn't make it dirty over and over.
  • Maybe I should only make a clean document dirty.


If (!IsDirty) IsDirty = true; // Duh


The code is actually very simple. Even the logic of this is really simple. Look at the thinking above: Its really straight forward and about 5 steps of thought.

We can help you code. But we can't teach you to think. Coding is a puzzle. If you aren't equipped to walk through a puzzle. If you are motivated to solve a mystery. If logic puzzles don't intrigue you. This might not be your best line of work.

I'm not drawn to baking and experimenting with recipes. I'm not drawn to flying planes or sculpting statues. There is no shame in admitting ones lack of abilities or interests.

Think through the problems. Pace and talk it out to your dog. Diagram it on a white board. Use lego blocks to represent your logic methods. Visualize it while you run a treadmill. Find whatever scheme works for you. But find something that wakes up your brain and gets you thinking instead of just giving up and asking for help at the first tiny roadblock.
Was This Post Helpful? 4
  • +
  • -

#13 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 06 December 2010 - 12:35 AM

ohhh ok...no need ti be rough lol....
thanks
Was This Post Helpful? 0
  • +
  • -

#14 dorinha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-December 10

Re: open save new file

Posted 06 December 2010 - 06:38 AM

All sorted tlhIn'toq

U r a star....manage do makeit work.....

One last question...ive been told this is the tricky bit!!!

I've set to save the richtextbox if any changes are made
so when changes are made and if I press
open
save
new

it asks me if i wanna save the changes

if the document already exists it saves with the same name and performas watever it was set up to perfoerm
if the document doesnt exist the save dialog box opens so i can save the document and when saved, it performs whatever it was set up to perform

but imagine if the document doesnt exist and the save dialog box opens and instead of saving i press cancel

it is suppose to go back to the same document....but mine instead of going back performs watever it was set up to perform

for example....
if i write something and dont save it
then i press open
it asks me if i wanna save
i press yes
the save dialog box opens
instead of saving i press enter
it is suppose to go back to the document but mine opens the opendialogbox
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1