在三个组合框中显示日期 [英] show date in three combo boxes

查看:216
本文介绍了在三个组合框中显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.net的初学者。我需要在三个月内显示 day month year 不同的组合框。为了使他们填补我试图硬编码值从1950年至今年。其中我后来意识到,我需要检查也是2月(28天),闰年和其他问题。有没有任何其他的方式来做这个没有硬编码的值。



在互联网上搜索答案遇到datepicker和monthcalendar我不想使用(因为我的项目应该是唯一的从我的其他朋友谁使用datepicker和月份日历)。或者如果您有任何其他建议,而不是组合框,请分享。





我相信有人曾经问过这个问题。

回答:



p> 工作代码:

  cmbDay ---& combobox 
cmbMonth ---> combobox
cmbYear ---> combobox

FormLoad事件

  this.cmbYear.Leave + = new EventHandler(cmbYear_Leave); 
this.cmbDay.Leave + = new EventHandler(cmbYear_Leave);
this.cmbMonth.Leave + = new EventHandler(cmbYear_Leave);
//
for(int i = 1950; i <2012; i ++)
{
cmbYear.Items.Add(i.ToString());
}
for(int i = 1; i <32; i ++)
{
cmbDay.Items.Add(i.ToString());
}
//
cmbDay.SelectedIndex = 0;
cmbMonth.SelectedIndex = 0;
cmbYear.SelectedIndex = 0;

OnLeave事件



  private void cmbYear_Leave(object sender,EventArgs e)
{
int day = DateTime.DaysInMonth(Convert.ToInt32(cmbYear.Text),cmbMonth.SelectedIndex +1);
if(day< = cmbDay.SelectedIndex)
{
//警告消息或您的管理代码。
}
}

PS: cmbMonth 是字符串格式(Jan,Feb,Mar,...)

解决方案

将所有三个值填入所有可能的值,例如日期1到31,月份1月到12月和年份1950到今年。



检查失去焦点时的有效日期所有三个使用

  DateTime.DaysInMonth(int year,int month); 

你可以直接将选定的项目转换为int和月份,你可以找到selectedindex +1索引为0)。



现在在失去焦点上(所有三个失去焦点的一个失去焦点事件),检查日期是否小于或等于方法返回的天数。如果没有,那么警报消息或某事。


I am beginner in .net. I need to show day, month and year in three different comboboxes. To make them fill i was trying hardcoded values from 1950 to present year. which i later realized that i need to check for also ferbruary(28 days), leap year and other issues. Is there any other way to do this without hardcoding the values.

while searching for answer on internet i came across datepicker and monthcalendar which i dont want to use(because my project should be unique from my other friends who are using datepicker and monthcalendar). or else if you have any other suggestions instead of comboboxes please share.

I am sure someone has asked this question before. I dont mind if you direct the link.

EDITED after nikhil agrawal's answer:

Working Code:

cmbDay    --->  combobox
cmbMonth  --->  combobox
cmbYear   --->  combobox

FormLoad event

        this.cmbYear.Leave+=new EventHandler(cmbYear_Leave);
        this.cmbDay.Leave += new EventHandler(cmbYear_Leave);
        this.cmbMonth.Leave += new EventHandler(cmbYear_Leave);
        //
        for (int i = 1950; i < 2012; i++)
        {
            cmbYear.Items.Add(i.ToString());
        }
        for (int i = 1; i < 32; i++)
        {
            cmbDay.Items.Add(i.ToString());
        }
        //
        cmbDay.SelectedIndex = 0;
        cmbMonth.SelectedIndex = 0;
        cmbYear.SelectedIndex = 0;

OnLeave Event

private void cmbYear_Leave(object sender, EventArgs e)
    {
        int day = DateTime.DaysInMonth(Convert.ToInt32(cmbYear.Text), cmbMonth.SelectedIndex+1);                       
        if (day <= cmbDay.SelectedIndex)
        {
            //alert message or your managing code.
        }
    }             

PS: cmbMonth is in string format ("Jan","Feb","Mar",...)

解决方案

Populate all three with all possible values like for date 1 to 31, for month Jan to Dec and year 1950 to present year.

Check for valid date when lost focus on all three using

DateTime.DaysInMonth(int year, int month);

for year you can directly cast selected item to int and for month you can find selectedindex +1 (coz index is 0 based).

Now on lost focus(one single lost focus event on all three's lost focus) check if the date is less or equal to days returned by method. If not then alert message or something.

这篇关于在三个组合框中显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆