验证winForms中的Datetimepicker [英] Validating the Datetimepicker in winForms

查看:75
本文介绍了验证winForms中的Datetimepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我很抱歉发布此问题,因为它可能对所有人都很愚蠢,但我没有得到确切的解决方案。问题是我的项目中有一个日期时间选择器,它出现在表单中的3个文本框之后,如果在文本框中没有输入文本并输入提交,它会给出一个消息(验证),即要输入的数据同样地,如果没有选择日期,则应该继续进行。这样做的代码是什么,为其他文本框工作但不适用于dateimepicker控件的代码是:



  if (dateInsert.Value.ToString()=  string  .Empty)
{
MessageBox。显示( 请选择日期!);
txtDescription.Focus();
return ;
}

解决方案

看看这里: DateTimePicker.Value [ ^ ]属性,其中获取或设置分配给控件的日期/时间值。



如果您想检查DateTimePicker的值是否具有正确的日期时间值,请使用< a href =http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx> DateTime.TryParse [ ^ ]方法。



 DateTime dtValue = DateTime.Now(); 
bool bres = DateTime.TryParse(dateInsert.Value.ToString(),dtValue);

if (bres == true
// 正确的日期
其他
// 无法转换!





更多:

解析日期和时间字符串 [ ^ ]


 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 WindowsApplication12
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load( object sender,EventArgs e)
{
dateTimePicker1.CustomFormat = ;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
}

private void dateTimePicker1_MouseEnter( object sender,EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
}
}
}


你的意思是每次用户都要更改默认日期吗?

Hello, I am sorry for posting this question as it may be find silly to all, but I am not getting the exact solution. The question is I have a Date time picker in my project, it comes after the 3 textboxes in the form, if there is no text is entered in the textbox and enter on submit, it gives a message(validation) that data to be entered, in the same way, if the date is not selected, it should proceed further. What is the code to do that, the code whcih worked for other textboxes and not working for dateimepicker control is:

if (dateInsert.Value.ToString() = string.Empty)
           {
               MessageBox.Show("Please select date!");
               txtDescription.Focus();
               return;
           }

解决方案

Have a look here: DateTimePicker.Value[^] property, which gets or sets the date/time value assigned to the control.

If you would like to check if the value of DateTimePicker has a proper datetime value, please, use DateTime.TryParse[^] method.

DateTime dtValue = DateTime.Now();
bool bres = DateTime.TryParse(dateInsert.Value.ToString(), dtValue);

if (bres==true) 
//proper date
else
//unable to convert!



More:
Parsing Date and Time Strings[^]


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePicker1.CustomFormat = " ";
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
        }

        private void dateTimePicker1_MouseEnter(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Short;
        }
    }
}


U mean to say every time user have to change the default date ?


这篇关于验证winForms中的Datetimepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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