19
Answers

How to add a date from mounth calender to list box in C#?

Photo of Joe Wilson

Joe Wilson

10y
2.1k
1
When i double click on an special date in the calender the current date which has been chosen automatically added to a list box
 
How can i do that ?Please Guide me.

Answers (19)

8
Photo of Jose Saiz
NA 107 7.2k 10y

listbox.items.add(calendar.date.picked)

Quick and dirty

Instantiate the listbox control
ListBox lslBox = new ListBox();

// Add the system date mm/dd/yyyy       

lslBox.Items.Add(DateTime.Now.ToShortDateString());

1
Photo of Ranjit Powar
NA 8.1k 496.7k 10y
Hey if you are using MonthCalender control then use following code.

  private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
  {
            listBox1.Items.Add(monthCalendar1.SelectionEnd.ToString());
  }
0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
Thank you very Much about your confirmation.
0
Photo of Vulpes
NA 98.3k 1.5m 10y
"...,so i use mouse click to choose the date then use button to add selected date to list box."

Much better idea :)

0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
Your answer was perfect but it couldn't be use for month calender .
0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
I changed my answer because when i was using mouse double_click compiler cannot distinguish the main purpose with other things,so i use mouse click to choose the date then use button to add selected date to list box.
What do you think about this way?

0
Photo of Vulpes
NA 98.3k 1.5m 10y
Which aspect are you not clear about?

0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
Could you please explain more or using example please?
0
Photo of Vulpes
NA 98.3k 1.5m 10y
When a full month's calendar is displayed, the code should be selecting the date beneath the mouse when it is double clicked and then adding that date to the listbox, provided it hasn't been added previously.

At least, that's what it did when I tested it :)

Incidentally, it might be best to set the MonthCalendar's MaxSelectionCount property to 1 to ensure that there's  no confusion with other dates that may be selected at the same time.



0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
Thank you for your answer ,but your answer just add today date to the list box.
0
Photo of Vulpes
NA 98.3k 1.5m 10y
The MonthCalendar control doesn't expose the DoubleClick or MouseDoubleClick events.

However, you can manufacture you're own using the MouseDown event by calculating the time between events and seeing if it's short enough to be regarded as a double click:

        private int lastTicks = 0;

        private void monthCalendar1_MouseDown(object sender, MouseEventArgs e)
        {
            int currentTicks = Environment.TickCount;
            if (currentTicks - lastTicks <= SystemInformation.DoubleClickTime)
            {
                string selected = monthCalendar1.SelectionStart.ToLongDateString();
                if (!listBox1.Items.Contains(selected)) listBox1.Items.Add(selected);
            }
            lastTicks = currentTicks;           
        }

Note that double clicking on the month or a year also causes a selection to be made.

0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
Could you please guide me?
0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
Your answer wasn't correct,I think.
0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
Your code just add today date to the list box,
although i  want to add every date which i want for example my birth date to the list box.
Thank you

0
Photo of Ranjit Powar
NA 8.1k 496.7k 10y
What is the problem?
0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
It wasn't correct,I think.
Please Check it.

0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
And your answer didn't  add the date which has been chosen by user to list box .

0
Photo of Joe Wilson
NA 6.6k 237.5k 10y
I mean that when i double click on the left button of mouse on an special date in the mouth calender ,the current date automatically is added to list box.
-61
Photo of Ranjit Powar
NA 8.1k 496.7k 10y
User ValueChaned event of datetimepicker as follows.

 private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
         listBox1.Items.Add(dateTimePicker1.Value .ToString());
}