输入“1”的C#代码价值并将其更改为迄今 [英] C# code to enter "1" value and change it to date

查看:99
本文介绍了输入“1”的C#代码价值并将其更改为迄今的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,我真的需要帮助我的问题我想将文本框输入的值更改为确切的月份,例如:



TextBox.Text =1 ,当输入时应将DateTimePicker更改为=01/01/2018。

其他月份

TextBox.Text =2,输入时应更改DateTimePicker to =01/02/2018。



直到

TextBox.Text =12,输入此内容时应将DateTimePicker更改为=01/12/2018。



我的尝试:



亲爱的所有人,我真的需要帮助我的问题我想将文本框输入的值更改为确切的月份,例如:

TextBox.Text =1,当输入此内容应将DateTimePicker更改为=01/01/2018。其他月份的

TextBox.Text =2,输入时应将DateTimePicker更改为01/02/2018。

直到
TextBox.Text =12,输入时应将DateTimePicker更改为=01/12/2018。


 private void myTextBox_TextChanged(object sender,EventArgs e)
{
int month;
if(int.TryParse(myTextBox.Text,out month))
{
DateTime dt = myDateTimePicker.Value;
myDateTimePicker.Value = dt.AddMonths(month - dt.Month);
}
}


Dear All, I really need help with my problem I want to Change textbox entered value to the exact month for example:

TextBox.Text = "1", when enter this should change DateTimePicker to ="01/01/2018".
for other months
TextBox.Text = "2", when enter this should change DateTimePicker to ="01/02/2018".

until
TextBox.Text = "12", when enter this should change DateTimePicker to ="01/12/2018".

What I have tried:

Dear All, I really need help with my problem I want to Change textbox entered value to the exact month for example:

TextBox.Text = "1", when enter this should change DateTimePicker to ="01/01/2018".
for other months
TextBox.Text = "2", when enter this should change DateTimePicker to ="01/02/2018".

until
TextBox.Text = "12", when enter this should change DateTimePicker to ="01/12/2018".

解决方案

Try this:

private void myTextBox_TextChanged(object sender, EventArgs e)
    {
    int month;
    if (int.TryParse(myTextBox.Text, out month))
        {
        DateTime dt = myDateTimePicker.Value;
        myDateTimePicker.Value = dt.AddMonths(month - dt.Month);
        }
    }


这篇关于输入“1”的C#代码价值并将其更改为迄今的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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