How can you add events to a date with MonthCalendar?
Printable View
How can you add events to a date with MonthCalendar?
I am not sure but i dont think you can actually add an event to a date with the MonthCalender. I think you will have to work with associations maybe using a List(Of ).
Hello there,
Am I right in saying that what you are trying to achieve is to indicate on the calendar control which dates have an event on that day? i.e. similar to the calendar in Outlook where the dates that you have something on, they are marked in bold?
If this is the case, then you might want to look into adding dates to the MonthCalendar AnnuallyBoldedDates, BoldedDates and MonthlyBoldedDates properties.
These allow to add dates to the calendar that will make them appear bold, for instance:
You could then use the DateChanged Event and if the selected date is one of the dates that have an event associated with it, bring up details of that event.Code:monthCalendar1.MonthlyBoldedDates = new System.DateTime[] {
new System.DateTime(2009, 9, 26, 0, 0, 0, 0),
new System.DateTime(2009, 9, 30, 0, 0, 0, 0)};
Hope that helps!!!
Gary
How could I have the user type in the event and then have it save the data in a text file and do it that way?
Hey,
What you would need to do would be something like the following (this is a very rough approximation of one way to achieve this):
1. Have the user select the date in the calendar
2. Have the user enter the information regarding the event
3. Save that information to a text file
Then when you want to display the data
1. Load all the information from the text file into memory
2. Loop through all the events and find the dates and add these to the MonthCalendar as shown above
3. When the user selects a date, display the information about that event.
Depending on exactly what you are trying to do, a text file might not be the best approach. You might want to think about a database, or an xml file.
If you provide some more details about what exactly you are trying to achieve, then I am sure someone here will be able to make some suggestions.
Gary
hey,
Could you please explain how I would save the data to a text file because I'm a newbie and don't know how.
Hey,
Again, there are various ways to do this, and it depends on what you are trying to do, but as a rough guide, this is what you would need to do:
1. Create a FileStream object and either create a new file, or open an existing one.
2. Create a StreamWriter to wrap around the FileStream
3. Use the methods of the StreamWriter to write the information to the file.
4. Once you are done, close the StreamWriter, and then close the FileStream.
It is possible to bypass the need for a FileStream object by simply calling the CreateText method on the File object, which returns a StreamWriter.Code:Dim theFile As FileStream = File.Create("C:\somefile.txt")
Dim writer As StreamWriter = New StreamWriter(theFile)
writer.WriteLine("Hello")
writer.Close()
theFile.Close()
Or you can write directly to a file using the WriteAllText method.Code:Dim writer As StreamWriter = File.CreateText("c:\somefile.txt")
writer.WriteLine("Hello")
writer.Close()
Hope that helps!!Code:File.WriteAllText("c:\somefile.txt", "Hello")
Gary
You may find the following usefull when you want to save to a specific file path on the users computer:
vb Code:
'Here: 'Application Start Up Path Dim AppStartUp As String = Application.StartupPath 'Desktop Dim Desktop As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 'MyDocuments Dim MyDocuments As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 'Application Data Dim AppData As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Sorry that I don't understand this, but I keep getting errors with the FileStream and StreamWriter. Can you help me with this?
Can we see your code?
Otherwise just use IO.File.WriteAllText().
Hey,
It might help if you tell me what errors you are getting?
In order for the above to work you need to include:
GaryCode:Imports System.IO
Or you can just write the full code.. namespace included.Quote:
Originally Posted by gep13
IO.
Granted. However, if he has copied and pasted the above code, it will not work unless the Import statement is used, which would be a lot easier than appending System.IO to each object.Quote:
Originally Posted by noahssite
Gary
Well yes.Quote:
Originally Posted by gep13
Also note that system is by default imported. So you would only need to append IO to each object not system.io
Again, granted. All I was trying to say was, if the above code has been copied and pasted, an Imports statement that has System.IO will be needed in order for it to work.
Gary
red_bull_NRG,
I have edited the code in Post #7 above, I noticed that there was an error.
Sorry, I was in C# land there for a while. If you re-copy the code then it should now work.
Sorry about that!!
Gary
Oh ya sorry i was jumping to conclusions..
How can I set it so that it saves the text file in the same directory that the program is in without knowing where the directory is located?
Hey,
In the example that I posted, replace the path to the string that I gave, i.e. C:\blah, and give it just a filename instead.
This will then default to storing the file in the same directory as the application is currently running from.
Gary
As said in post 8 (Click Here)Quote:
Originally Posted by red_bull_NRG
To retrieve the Application StartUp path (application folder..):
vb Code:
Dim AppLocation as String = Application.StartUpPath