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

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

问题描述

我是初学者在.NET。我需要显示三不同的组合框。为了让他们填补我是从1950年力图codeD值present一年。这是我后来意识到,我需要检查也ferbruary(28天),闰年等问题。是否有任何其他的方式来做到这一点没有硬编码的值。

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.

在检索互联网上的答案我碰到日期选择器和MONTHCALENDAR,我不想使用(因为我的项目应该是从我的其他谁使用日期选择器和MONTHCALENDAR朋友是唯一的)。否则,如果您有任何其他的建议而不是组合框,请分享一下。

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.

后可进行编辑的 NIKHIL阿格拉瓦尔的的答案:

EDITED after nikhil agrawal's answer:

工作code:

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)
        {
            //alert message or your managing code.
        }
    }             

PS: cmbMonth 为字符串格式(月,月,月,...)

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

推荐答案

填充所有三个包含日期1到31的所有可能的值,本月Jan到日和1950年以present一年。

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);

为一年,你可以直接投选定的项目为int并为一个月,你可以找到的selectedIndex +1(堂妹的索引为0)。

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天全站免登陆