How to generate Calendar in c#.net windows applica

2019-05-30 23:45发布

问题:

I want to Generate Calendar in c#.net to enter daily attendance.I have used jquery calendar for similar purpose in web. Is there any ways to create calendar control and get click event to mark and display values in that.

Thanks

Example image:

回答1:

If you have a budget I would definitely go for a commercial component like Telerik's Scheduler Control. That will cost $999 for the collection, I've never used Telerik's controls before so I can't recommend them myself but by the looks it could be well worth it in the long run if you can reuse the components in other projects.

If that isn't an option, something as simple as your example would be fairly easy to roll yourself. I mainly work in WinForms so this is how I would go about building the form:

  1. Create a custom control to display any given day. You will need a label in the top corner for the day of month, and a label to display the text for the day (the In/Out times). Maybe add a border.
  2. Create a form or control that will contain the calendar
  3. Add a toolstrip docked to the top with your month and year controls. Use a combo box or drop-down button.
  4. Add a thin panel docked to the top and add labels for the days. Decide on the width you want each column to be and position the labels based on that.
  5. Add a FlowLayoutPanel and set Dock to Fill. This will contain the 'day' controls, which will flow left to right, top to bottom.

Now you need to be working in code, not the designer. This could probably be done in the constructor, the Page_Load handler, or somewhere that gets called when the month/year is set (or initialised):

  1. Add enough empty panels to the FlowLayoutControl to pad out the start of the month. You can find out how many to add with new DateTime(year, month, 1).DayOfWeek to find the day that the first of the month falls on. The empty panels should be set to the same size as the 'day' control.
  2. Add a day control to the FlowLayoutControl for each day in the month. Use DateTime.DayInMonth(year, month) to find the number of days in the selected month, and set each 'day' control up with the date (and probably the in/out data) as you're adding it.
  3. Now play with padding and borders on the 'day' controls and padding panels until everything looks right. You can add some test 'day' controls and panels in the designer to test layouts there and just call flowLayoutControl.Items.Clear() in the setup code.

That should result in a control that can dynamically display any month. To add the attendance editing feature, set up a Click handler either in the 'day' control or in the calendar control itself (when you're adding the 'day' controls to the FlowLayoutControl, and open up a modal form to edit or add the in/out times for the selected day.

If you wanted the day controls to resize dynamically register for the containing control's Resize event and set the width and height of each day control to Math.Floor(control.Width / 7) (seven days wide) and Math.Floor(control.Height / 5) (five weeks high) or something similar.



回答2:

As far as I know, there is no a standard schedule control on the market. I would suggest that you take a look at the XtraSchedule control from DevExpress.