Frage

When I click on the file drop down where I have new, for new file, I prompt the user. If they have data in the textbox, if they would like to save what they have done before starting a new file. I get the save prompt dialog box to pop up and all that but I wont save the file.

private void mnuFileNew_Click(object sender, EventArgs e)
{
    if (txtText.Text.Trim().Length <= 0)
    {
        lblStatus.ForeColor = Color.BlueViolet;
        lblStatus.Text = "New file!";
    }
    else
    {
        if (MessageBox.Show("Start new file without saving?", "Exit",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==  System.Windows.Forms.DialogResult.No)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                // set properties
                sfd.Title = "Where would you like to save this?";
                sfd.InitialDirectory = @"c:\Users\public";
                sfd.Filter = "Text File (*.txt)|*.txt|All files (*.*)|*.*";
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {                         
                    lblStatus.Text = sfd.FileName;
                    lblStatus.ForeColor = Color.Navy;
                }
                // show a message 
                lblStatus.ForeColor = Color.BlueViolet;
                lblStatus.Text = "The Text has been uploaded!";
            }
            catch (Exception ex)
            {
                lblStatus.ForeColor = Color.Red;
                lblStatus.Text = ex.Message;
            }
        }
        else
        {
            txtText.Text = "";
        }
War es hilfreich?

Lösung

you need to create the file

if(sfd.ShowDialog() == DialogResult.OK)
{
    using(StreamWriter sw = new StreamWriter(sfd.FileName))
    {
      sw.WriteLine(YourTextBox.Text);
    }

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top