Hello,

I did not find what I was needing with a search, so here goes. I am having problems getting my calendar to work, passing a start and end date for a rental car and displaying on the form the number of days chosen and the amount for the total. This is the first form problem I have done. I cannot see what I am doing wrong. It does not figure number of days and the amount just stays zero. Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class CarRental : Form
    {
        double pricePerDay = 0;
        int numDays = 0;
       


        public CarRental()
        {
            InitializeComponent();
        }


        private void monthCalendar1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
        {           
            // Show the start and end dates in the labels.
            this.SelectionStart.Text = "Date Selected: Start = " +
                e.Start.ToShortDateString();
            this.SelectionEnd.Text = "Date Selected: End = " + e.End.ToShortDateString();
        }
        
        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            DateTime selectedDates = monthCalendar1.SelectionStart;
            TimeSpan diffDays = selectedDates.Subtract(e.Start);
            numDays = diffDays.Days;
        }

        private void selectionBox_Enter(object sender, EventArgs e)
        {

        }

        private void radioBtnCompact_CheckedChanged(object sender, EventArgs e)
        {
            pricePerDay = 19.95;
        }

        private void radioBtnStandard_CheckedChanged(object sender, EventArgs e)
        {
            pricePerDay = 24.95;
        }

        private void radioBtnLuxury_CheckedChanged(object sender, EventArgs e)
        {
            pricePerDay = 39.00;
        }

        private void btnSelection_Click(object sender, EventArgs e)
        {
            numbOfDaysLbl.Text = "Number of Rental Days is: " + numDays;
            totalLbl.Text = "Total is: " + (numDays * pricePerDay).ToString("C");
        }
        
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

       

        
    }
}

Thanks a bunch!

Recommended Answers

All 3 Replies

Hi,

I sould not use a Monthcalendar for this, but 2 Datetimepickers.
I made you a small example how I should do the calculation.

private void Button1_Click(System.Object sender, System.EventArgs e)
{
	System.DateTime d1 = DateTimePicker1.Value;
	System.DateTime d2 = DateTimePicker2.Value;
	TimeSpan ts = default(TimeSpan);
	ts = d2.Subtract(d1);
	if (!(ts.Days > 0)) {
		Label1.Text = ("There are " + ts.Days + 1 + " " + "days");
	} else {
		Label2.Text = ("There are " + ts.Days + " " + "days");
	}
	int pricePerDay = 39.0;
	Label2.Text = "Total is: " + (ts.Days * pricePerDay).ToString("C");

}

Because I'm not using C# offen is it possible that the're some errors in it.
Let me know if there some problems with it.

Thanks a bunch. I was kinda thinking I might have to use 2 calendars. I was actually biting off a bit more than asked for. The assignment only called for the user to pick the return day, with a start day of the current date.
So what determines if you use monthCalendar or datePicker? Can you clarify that for me?

Thank you for the help!

Just to close the loop, here is my final production. Thanks again. It works great!

namespace WindowsFormsApplication1
{
    public partial class CarRental : Form
    {
        double pricePerDay = 0;
        int numDays = 0;
       


        public CarRental()
        {
            InitializeComponent();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
           
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }


        private void selectionBox_Enter(object sender, EventArgs e)
        {

        }

        private void radioBtnCompact_CheckedChanged(object sender, EventArgs e)
        {
            pricePerDay = 19.95;
        }

        private void radioBtnStandard_CheckedChanged(object sender, EventArgs e)
        {
            pricePerDay = 24.95;
        }

        private void radioBtnLuxury_CheckedChanged(object sender, EventArgs e)
        {
            pricePerDay = 39.00;
        }

        private void btnSelection_Click(object sender, EventArgs e)
        {
            DateTime d1 = dateTimePicker1.Value;
            DateTime d2 = dateTimePicker2.Value;
            TimeSpan ts = default(TimeSpan);
            ts = d2.Subtract(d1);
            numDays = ts.Days + 1;

            if (!(ts.Days > 0))
            {
                numbOfDaysLbl.Text = ("Number of Rental Days is: " + numDays);
            }
            
            numbOfDaysLbl.Text = "Number of Rental Days is: " + numDays;
            totalLbl.Text = "Total is: " + (numDays * pricePerDay).ToString("C");
        }
        
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CarRental_Load(object sender, EventArgs e)
        {

        }
   
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.