Window Opening of date control in WinForm

Source: Internet
Author: User

When we use WinForm to develop projects or gadgets, we often need to do window opening events. What do you do at this time?

Of course, Microsoft provides us with a powerful date control. We can select a date from the drop-down list, but you will find that the date format is not what we need, or

We hope to get the date format, and the presented results are not very beautiful. Here I will show you how to set a date window opening event in the development gadgets,

This is a relatively simple window opening date. Start now!

Create a project with two Windows forms, as shown in:

The first form requires a date, a text box, a Button, and an image resource. in Form 2, we need to drag a MonthCalendar control to adjust it to an appropriate size.

Next, we need to write the event below the Button:

Private void button#click (object sender, EventArgs e)
{
String tDateTime = string. Empty;
NewOpenManager1.OpenWindow (ref tDateTime); // returns the date on which the callback is returned using OpenWindow.
If (tDateTime = null)
{

}
Else
{
This. textBox1.Text = tDateTime;
}
}

In this click event, we need to pay attention to NewOpenManager1.OpenWindow (ref tDateTime); this Code, which means that we will call the OpenWindow method of NewOpenManager1 class, the method body of this class is described as follows:

Public abstract class NewOpenManager1
{
Public static void OpenWindow (ref string tDateTime)
{
FrmDateTime frmDt = new FrmDateTime ();
TDateTime = frmDt. GetNewWindowDateTime ();
}
}

When defining this abstract class, we will instantiate the date form and call the GetNewWindowDateTime () method in the date form to obtain the date selected in the date form.

Let's take a look at the code in the date form!

First, we need to define a date attribute as follows:
Public string tDateTime
{
Get;
Set;
}

 

Then we define a method GetNewWindowDateTime () called above. The content of the method body is as follows:

Public string GetNewWindowDateTime ()
{
Switch (ShowDialog ())
{
Case DialogResult. OK:
Return this. tDateTime;
Default:
Break;
}
Return null;
}

In this way, we will get the selected date. below is the date format we need to format the selected date, the content is as follows:

Private void monthCalendar1_DateSelected (object sender, DateRangeEventArgs e)
{
String tDate = this. monthCalendar1.SelectionStart. ToString ("yyyy-MM-dd ");
This. tDateTime = tDate. Replace ("-","/");
This. DialogResult = DialogResult. OK;
}

Here, we format the date in the format of yyyy/MM/dd. You can format the date as needed and return it to the TextBox in the form Form1. The result is as follows:

OK. The window opening will be completed on this small date. Please refer to it. If there are better ways to introduce it to me, thank you very much!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.