Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calender for date? 1

Status
Not open for further replies.

nowayout

Programmer
Feb 25, 2003
364
US
Hi all,

I want to creat calender but dont know how. Also how can i get one? how can i make the selected date in calender to be enter into database field!!

Thanks
 
You mean, like a calendar control to use on Windows Forms?

There's already one that ships with VS.NET -- look for "DateTime Picker" control.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
hi
how can i link it to c# application and enter selected data in to database???

thanks
 
Solution 1
-add directly on the form the MonthCalendar control like any another control using Designer.
//..
System.Windows.Forms.MonthCalendar objCalendar = new System.Windows.Forms.MonthCalendar();
//..
- initialize it
Code:
      objCalendar.SelectionStart=objCalendar.MinDate = System.DateTime.Today;
-add handler for the DateChanged event
Code:
    private void objCalendar_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e)
	{
		DateTime dtSelected     = objCalendar.SelectionStart;
		string sShortDateString = objCalendar.SelectionStart.ToShortDateString();
		
	}
- to put back to database use Parameters to build the command or build the UPDATE/INSERT statement
Code:
      UPDATE table SET datehire="'"+ sShortDateString + "'" + "WHERE id=100";
Solution 2
Create a form in which you put the MonthCalendar and maybe other things and when you need to select a date then instantiate that form from where you get the selected date.
Use Designer to create this form. In this example, the form contains the MonthCalendar and a Close button used to hide the form.
Code:
public class CalendarForm : System.Windows.Forms.Form
{
	private System.Windows.Forms.MonthCalendar m_Calendar;
	private DateTime m_DateTime;
	private System.Windows.Forms.Button btnClose;
	//...
	public CalendarForm()
	{
		InitializeComponent();
		m_Calendar.SelectionStart=m_Calendar.MinDate = System.DateTime.Today; 
		m_Calendar.SelectionEnd =m_Calendar.SelectionStart;

	}
	private void InitializeComponent()
	{
	
	this.m_Calendar = new System.Windows.Forms.MonthCalendar();
	this.btnClose = new System.Windows.Forms.Button();
	
	// 
	// m_Calendar
	// 
	this.m_Calendar.AllowDrop = true;
	this.m_Calendar.DateChanged += new System.Windows.Forms.DateRangeEventHandler(m_Calendar_DateChanged);
	// 
	
	}
	private void m_Calendar_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e)
	{
		m_DateTime=m_Calendar.SelectionStart;
	}
	private void btnClose_Click(object sender, System.EventArgs e)
	{
	   Hide(); 
	}
	// Accessor method
	public DateTime SelectedDateTime
	{
		get
		{
			return m_Calendar.SelectionStart;
		}
	}
		
}
// How to use it from another form:
Code:
public class ResultsForm : System.Windows.Forms.Form
{
	private CalendarForm m_CalendarForm;
	public Calendar CalendarForm{get{return m_CalendarForm;}}
	// Constructor 
	public ResultsForm()
	{
		InitializeComponent();
		m_CalendarForm = new CalendarForm();
	}
	// Example of use , assume there is a grid in this form
	private void m_DataGrid_CurrentCellChanged(object sender, System.EventArgs e)
	{

	     Calendar.ShowDialog(); 
	     string sDateTime = Calendar.SelectedDateTime.ToShortDateString();
	}
}
obislavu
 
yeah that right but how wouldi show that in form. is there a link to an oject or image of calender that i need to show?

thanks
 
[tt]System.Windows.Forms.MonthCalendar[/tt]

is the calendar control. When the form loads, it'll be shown.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
it will be the same thing with datetime picker right??? but how would i get startdate and enddate both entered in database???
 
That is depending on your design.
Solution 1.
Bind startdate/enddate to TextBox controls which can be hidden on your form. When the startdate/enddate is selected from the MonthCalendar update the corresponding TextBox controls which will automatically update the datasource.
Solution 2.
Retrieve the startdate/enddate from the control and update back the database by sending an UPDATE command or execute a stored procedure to accomplish that.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top