none
c# Winform Save FileDialog adding a space between the file name and Date RRS feed

  • Question

  • I have a Winform that I export a excel file. It save the workbook and sheet name, I need to add a space between  Date and shift. Currently it looks like this (Truck Report 7-9-18Days) But needs to look like this (Truck Report 7-9-18 Days). dt2 is where I get the date and Combox3 is where I get the shift

    This is my code.

     private void pictureBox1_Click(object sender, EventArgs e)
            {
    
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Excel Documents (*.xls)|*.xls";
                sfd.FileName = string.Format("Truck Report {0:M-d-yy}{1}.xls", dt2.Value  ,comboBox3.SelectedItem);
                sfd.OverwritePrompt = false;  /// Turn off prompt to Overwrite File False
                if (sfd.ShowDialog() == DialogResult.OK)  // Show File Dialog Box
    


    Booney440

    Friday, January 11, 2019 8:48 PM

Answers

  •  I need to add a space between  Date and shift. Currently it looks like this (Truck Report 7-9-18Days) But needs to look like this (Truck Report 7-9-18 Days). 

                sfd.FileName = string.Format("Truck Report {0:M-d-yy}{1}.xls", dt2.Value  ,comboBox3.SelectedItem);
    

    As Tim suggests, making that minor change to the string format should be
    rather trivially obvious - assuming that you are familiar with formatting
    strings. Wouldn't simply putting a space between {0:M-d-yy} and {1} do what
    you want? e.g. -

    sfd.FileName = string.Format("Truck Report {0:M-d-yy} {1}.xls",dt2.Value,comboBox3.SelectedItem);
    

    - Wayne

    • Marked as answer by Booney440 Saturday, January 12, 2019 2:42 PM
    Saturday, January 12, 2019 4:16 AM

All replies

  • Did you write this code originally?  Because if you did, it is almost impossible for be to believe that you can't figure out how to fix it. You can see that there are two substitutions in the string.Format call, right?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Saturday, January 12, 2019 2:13 AM
  •  I need to add a space between  Date and shift. Currently it looks like this (Truck Report 7-9-18Days) But needs to look like this (Truck Report 7-9-18 Days). 

                sfd.FileName = string.Format("Truck Report {0:M-d-yy}{1}.xls", dt2.Value  ,comboBox3.SelectedItem);
    

    As Tim suggests, making that minor change to the string format should be
    rather trivially obvious - assuming that you are familiar with formatting
    strings. Wouldn't simply putting a space between {0:M-d-yy} and {1} do what
    you want? e.g. -

    sfd.FileName = string.Format("Truck Report {0:M-d-yy} {1}.xls",dt2.Value,comboBox3.SelectedItem);
    

    - Wayne

    • Marked as answer by Booney440 Saturday, January 12, 2019 2:42 PM
    Saturday, January 12, 2019 4:16 AM
  • I was adding the space in the wrong place. Thanks Wayne

    Booney440

    Saturday, January 12, 2019 2:42 PM