|
-
May 26th, 2010, 03:15 AM
#1
Thread Starter
New Member
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
-
May 26th, 2010, 04:22 AM
#2
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
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
May 26th, 2010, 05:14 AM
#3
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.
-
May 26th, 2010, 05:27 AM
#4
Re: Datetimepicker loading a text file
That makes sense, hadn't thought of that, thanks jmcilhinney
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
May 31st, 2010, 03:31 AM
#5
Thread Starter
New Member
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
-
May 31st, 2010, 04:01 AM
#6
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")
-
Jun 9th, 2010, 02:45 AM
#7
Thread Starter
New Member
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?
-
Jun 9th, 2010, 02:57 AM
#8
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.
-
Jun 27th, 2010, 12:57 PM
#9
Thread Starter
New Member
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
-
Jun 27th, 2010, 08:32 PM
#10
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.
-
Jun 28th, 2010, 12:50 AM
#11
Thread Starter
New Member
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
-
Jun 28th, 2010, 12:55 AM
#12
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|