Results 1 to 12 of 12

Thread: Datetimepicker loading a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Datetimepicker loading a text file

    Hello there!

    I'm a student from holland and we have to make a VB project for our IT-class.
    I picked the idea to make a little agenda for my own use and my plan was to edit a txt file using the RichTextBox. I want to use a DateTimePicker too, so that I can load a specific text file for the date chosen so that I can edit it. Does anyone have an idea how I can use the DateTimePicker to create and load certain txt files related to the same date?

    Yours Sincerely,
    Ragno

  2. #2
    Fanatic Member Satal Keto's Avatar
    Join Date
    Dec 2005
    Location
    Me.Location
    Posts
    518

    Re: Datetimepicker loading a text file

    The way I would go about doing this would be to use the date from the DateTimePicker as the name of the text file. Assuming you're wanting just to have one file a day then you could get the file name by using the following code
    Code:
    Dim filename as String = DateTimePicker1.Value.ToString("dd-MM-yyyy")
    This will give you the date selected in the DateTimePicker as a string in the format you specified, to find out more about how to format the date you can have a look here or here. Please remember you can't use slashes in your file name so you'll either need to miss them out or use another character instead.

    Once you have the file name you just need to read the text file, you can find out how to do that from any of the following links.
    http://support.microsoft.com/kb/304427
    http://tinyurl.com/3y8qf2h

    I hope this helps

    Satal

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datetimepicker loading a text file

    Just a note on formatting: I would recommend something like "yyyyMMdd" for file names. That way "alphabetical" order will match chronological order.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Datetimepicker loading a text file

    Thanks, both of you for the helpful info on the topic. I do have another question if you don't mind. I want to save the created text files in a specified folder which only contains text files from one month. So what I mean is that the folder May only contains text files from the 1st of May to the 31st of May.
    Is there any way to do this and to auto name the text files with the specific date given by my DateTimePicker?

    Thanks in advance for any kind of reply
    Ragno

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datetimepicker loading a text file

    I'm currently on a phone so forgive me if my code isn't perfect. It would be something like:
    Code:
    dim folderpath = io.path.combine(rootfolderpath, mydate.tostring("MMMM"))
    
    if not io.directory.exists(folderpath) then
        io.directory.createdirectory(folderpath)
    end if
    
    dim filepath = io.path.combine(folderpath, mydate.tostring("yyyyMMdd") & ".txt")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Datetimepicker loading a text file

    And for the Mydate part in your script, what should I add to make it copy the date selected in my date time picker?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datetimepicker loading a text file

    How do you normally get a selected date from a DateTimePicker? If you're not sure then you should consult the documentation for the DateTimePicker class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Datetimepicker loading a text file

    Hey people,
    My programme is almost up and running. There is just one thing that doesn't seem to work. I changed the extension to which I'm saving files into RTF instead of txt. I am using a RichTextBox in my programme, so this way of saving files makes more sense. But the fact that the title of the files I'm saving consists of numbers, the LoadFile command sees this file as a file of the type Integer. RichTextBox isn't able to load Integer files for some reason. Is it possible to change this? The actual text in the RTF is just some random text at the moment. Something as much as Lalalalalalala.

    Any help would/will be appreciated!
    Yours sincerely,
    Ragno

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datetimepicker loading a text file

    I think you need to show us the code you're using. There's no reason that a RTB should have trouble with file names containing numbers.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: Datetimepicker loading a text file

    The code that I'm using at the moment looks like this
    Code:
    Dim folderpath = IO.Path.Combine("C:\PlannerDX", DateTimePicker1.Value.ToString("yyyy-MM"))
            Dim filename As String = DateTimePicker1.Value.ToString("yyyy-MM-dd")
            filename = IO.Path.Combine(folderpath, DateTimePicker1.Value.ToString("yyyy-MM-dd") & ".rtf")
    
            OpenFileDialog1.InitialDirectory = Application.ExecutablePath
            OpenFileDialog1.DefaultExt = ".rtf"
            OpenFileDialog1.FileName = DateTimePicker1.Value.ToString("yyyy-MM-dd")
            OpenFileDialog1.Filter = "Rich Text Files (*.rtf)|*.rtf|All Files (*.*) | *.*"
            OpenFileDialog1.ShowDialog()
            RichTextBox1.LoadFile(OpenFileDialog1.FileName, DateTimePicker1.Value.ToString("yyyy-MM-dd"))
    And for some reason the error always shows up in the last line of the code. It tells me that the conversion of the text 2010-06-27, which was yesterday, to the type Integer is not valid. But I never even once specified the date as an Integer, it just makes it that way because the title consists of numbers I guess..

    Again, any help is more than welcome.
    Ragno

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datetimepicker loading a text file

    I suggest that you read the documentation for the RichTextBox.LoadFile method and see what the arguments actually mean. I don't know what you're actually trying to achieve with that second argument but it's not going to work whatever it is.

    I strongly suggest that you turn Option Strict On as well. That code would not even compile with Option Strict On so you'd have caught the issue at design time instead of run time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width