Wednesday, 31 July 2013

How do I set Custom Date Format in DateTimePicker (WinForms)? || Windows Controls - .NET Interview Questions and Answers || Interview Questions on .NET Windows Forms.

Hi friends in this article I will explain about how do I set Custom Date Format in DateTimePicker (WinForms)?  Or Windows Controls - .NET Interview Questions and Answers or Interview Questions on .NET Windows Forms.
How do I set Custom Date Format in DateTimePicker (WinForms)?
You can set custom date format in DateTimePicker using below code.

In VB.NET:
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DateTimePicker1.Format = DateTimePickerFormat.Custom
        DateTimePicker1.CustomFormat = "dd/MM/yyyy"
        DateTimePicker1.ShowUpDown = False
    End Sub
In C#:
private void Form1_Load(System.Object sender, System.EventArgs e)
{
      DateTimePicker1.Format = DateTimePickerFormat.Custom;
      DateTimePicker1.CustomFormat = "dd/MM/yyyy";
      DateTimePicker1.ShowUpDown = false;
}
When you run the code above code it will show the output like below figure. Because you use the code DateTimePicker1.ShowUpDown = False.



In VB.NET:
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DateTimePicker1.Format = DateTimePickerFormat.Custom
       DateTimePicker1.MinDate = DateTime.Today

        DateTimePicker1.MaxDate = DateTime.Today.AddYears(1)
        DateTimePicker1.CustomFormat = "dd/MM/yyyy"
        DateTimePicker1.ShowUpDown = True

    End Sub
In C#:
private void Form1_Load(System.Object sender, System.EventArgs e)
{
      DateTimePicker1.Format = DateTimePickerFormat.Custom;
      DateTimePicker1.MinDate = DateTime.Today;

      DateTimePicker1.MaxDate = DateTime.Today.AddYears(1);
      DateTimePicker1.CustomFormat = "dd/MM/yyyy";
      DateTimePicker1.ShowUpDown = true;
}

When you run the code above code it will show the output like below figure. Because you use the code DateTimePicker1.ShowUpDown = true.


You can also set the selected date to textbox. I explained this using one example.
Create on new project
 File----New ---Project ----Windows Application ---Form1.vb
Take one DateTimePicker,four labels and four textboxes and double click on form and write the below code.


In VB.NET:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DateTimePicker1.Format = DateTimePickerFormat.Custom
       DateTimePicker1.MinDate = DateTime.Today

        DateTimePicker1.MaxDate = DateTime.Today.AddYears(1)
        DateTimePicker1.CustomFormat = "dd/MM/yyyy"
        DateTimePicker1.ShowUpDown = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = DateTimePicker1.Value.Date
        TextBox2.Text = DateTimePicker1.Value.Day
        TextBox3.Text = DateTimePicker1.Value.DayOfWeek
        TextBox4.Text = DateTimePicker1.Value.Hour

    End Sub
End Class

In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
Public Class Form1
{
      private void Form1_Load(System.Object sender, System.EventArgs e)
      {
            DateTimePicker1.Format = DateTimePickerFormat.Custom;
            DateTimePicker1.MinDate = DateTime.Today;

            DateTimePicker1.MaxDate = DateTime.Today.AddYears(1);
            DateTimePicker1.CustomFormat = "dd/MM/yyyy";
            DateTimePicker1.ShowUpDown = false;
      }

      private void Button1_Click(System.Object sender, System.EventArgs e)
      {
            TextBox1.Text = DateTimePicker1.Value.Date;
            TextBox2.Text = DateTimePicker1.Value.Day;
            TextBox3.Text = DateTimePicker1.Value.DayOfWeek;
            TextBox4.Text = DateTimePicker1.Value.Hour;

      }
    Public Form1()
      {
            Load += Form1_Load;
      }
}

And run the above code and click on button then it will like below figure. 

Go to another interview question Difference between Cookies and Session in ASP.Net || What is cookie? || What is session? How to use sessions and cookies?
 I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.