2 Replies - 3127 Views - Last Post: 09 July 2012 - 03:22 AM Rate Topic: -----

#1 jibbler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 05-July 12

Save As dialog box not working

Posted 05 July 2012 - 08:15 PM

First time poster, hope I don't break any rules.

I want to say first, this is a homework assignment for me. I'm taking my first programming course. I'm 5 weeks into an 8 week summer course. Everything was going good until today.

I have a textbox where the user enters a value. My code generates random numbers based on that value. The application writes these numbers to a text file. The text file saves in /bin/debug location and writes successfully. I'm trying to create a Save As dialog box to allow the user to save the file on the desktop. The Save As box opens, but doesn't actually save anything when I press Save. Any help you can provide would be greatly appreciated.

private void generateButton_Click(object sender, EventArgs e)
        {
            //create variable to hold random number 
            int number;

            //create a Random object
            Random rand = new Random();

            //get a random integer in the range of 1 to 100
            number = int.Parse(inputTextBox.Text);

            //declare a StreamWriter variable
            StreamWriter outputFile;

            //create a file and get a StreamWriter object
            outputFile = File.CreateText("Random Number.txt");

            for (int count = 0; count < number; count++)
            {
                outputFile.WriteLine(rand.Next(100) + 1);
            }

            //close the file
            outputFile.Close();

            saveFile.ShowDialog();
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Save As dialog box not working

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: Save As dialog box not working

Posted 05 July 2012 - 10:03 PM

saveFile.ShowDialog() will only show the dialog and give you an opportunity to get the saveFile.FileName that the user picked. It's still up to you to write to that.
Was This Post Helpful? 0
  • +
  • -

#3 zenom340  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 09-July 12

Re: Save As dialog box not working

Posted 09 July 2012 - 03:22 AM

In your code, you just opened the SaveFileDialog without any further commands. You have not included what the SFD will do upon clicking Save. Here's a sample:

private void generateButton_Click(object sender, EventArgs e)
{
int number;
Random rand = new Random();
number = int.Parse(inputTextBox.Text);

if(saveFile.ShowDialog()==DialogResult.OK)
{

StreamWriter outputFile;
outputFile = File.CreateText(saveFile.FileName+".txt");
for (int count = 0; count < number; count++)
{
outputFile.WriteLine(rand.Next(100) + 1);
}
outputFile.Close();

}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1