如果操作者输入错误可以报错!谢谢!

解决方案 »

  1.   

    我需要的是类似文本框,要输入的
    DateTimePicker不行的,相当于给文本框做个输入时间的掩码格式!
      

  2.   

    呵呵,自己做的话好象挺麻烦啊。不如你把年,月,日分开,每个做一个Text,用户输入完一个就跳到下一个,同时做合法性的Check。这样是不是会简单些啊?
      

  3.   

    using System.Windows.Forms;class Test : Form
    {
      Test()
      {
        DateTimePicker dtp = new DateTimePicker();
        dtp.Parent         = this;
        dtp.Width          = 140;
        dtp.CustomFormat   = "yyyy.MM.dd HH:mm:ss";
        dtp.Format         = DateTimePickerFormat.Custom;
        dtp.ShowUpDown     = true;
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  4.   

    我曾经做过一个是在WINFORM下面使用的,你要的话给我发邮件:[email protected]
      

  5.   

    asp里面日期选择用楼主说的这个,.net里winform用datatimepicker,webform用calendar,都是日历那种格式选的。可以上网找找有没有其他人做的现成的。
      

  6.   

    搂主可以用javascript来做!net里winform用datatimepicker,webform用calendar
    在网上挺多得!搜索日期控件!
      

  7.   

    可以试试这样1.把你要得格式定下来,会出现文本内容如:"2005-08-09 10:05:08",初始化时就给它赋值当前系统时间或者全0都可以,并将关标置第一位位置.
    2.将textbox设置成定长就是上面这个字符串得长度.
    3.在textbox得keypress事件里处理
      屏蔽backspace键,屏蔽字母键.
      当光标不在格式字符位置时,可以输入数字,在格式符上时,光标再往后移动.
      输入的字符插入在原来的text里面.哦,这个方法要能获取光标的位置,不知能否实现,先试试去.
      

  8.   

    下面是我写的控件,可按要求输入。using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;namespace NullableDateControls
    {
    /// <summary>
    /// 类名:MaskDateEdit
    /// 功能:可按掩码输入日期,提供下拉选择
    /// 作者:魏孙鼎
    /// </summary>
    public class MaskDateEdit: TextBox
    {
    private string _Mask = "yyyy年MM月dd日hh时mm分ss秒";
    private Button button;
    private MonthCalendar mc; public MaskDateEdit()
    {
    button = new Button();   
      BindButton();
    } /// <summary>
    /// 设置相应的输入掩码,如:yyyy年MM月dd日
    /// </summary>
    public string Mask
    {
    get { return _Mask;}
    set { _Mask = value; this.GetMaskChars();}
    } private void BindButton()
    {
    if (!this.Contains(button))
    {
    this.Controls.Add(button);
    button.TabStop = false;
    button.Click += new EventHandler(button_Click);
    } button.BringToFront(); button.Text = "6";
    button.Font = new Font("Webdings",10);
    button.BackColor = Color.FromKnownColor(System.Drawing.KnownColor.ControlLightLight);
    button.TextAlign = ContentAlignment.MiddleCenter;
    button.FlatStyle = FlatStyle.System;
    button.Width = 16;
    button.Height = this.Height - 4;
                button.Left = this.Width - button.Width - 4;            
    } private string GetMaskChars()
    {
    return this.Mask.Replace("y"," ")
            .Replace("m"," ")
    .Replace("d"," ")
    .Replace("Y"," ")
    .Replace("M"," ")
    .Replace("D"," ")
    .Replace("#"," ")
    .Replace("h"," ")
    .Replace("m"," ")
    .Replace("s"," ");

    } public override string Text
    {
    get
    {
    if (base.Text == this.GetMaskChars() || base.Text == null)
    {
    return "";
    }
    else
    {
    return base.Text;
    }
    }
    set
    {
    if(value == null || value == "")
    {
    base.Text = GetMaskChars();
    }
    else
    {
    base.Text = value;
    }
    }
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {  
    char[] chars;
    int j;
    int i = this.SelectionStart;

    if (keyData == Keys.Left || keyData == Keys.Right ||
    keyData == Keys.Enter || keyData == Keys.Tab)
    {
    return base.ProcessCmdKey(ref msg, keyData);
    } if (keyData == Keys.Delete)
    {
    this.Text = null;
    return true;
    } //退格删除
    if (keyData == Keys.Back)
    {
    if (i == 0) return true; chars = base.Text.ToCharArray();
    for(j = i-1; j>=0;j--)
    {
    if( chars[j] == ' ' || IsDigit(chars[j]) )
    {
    chars[j] = ' ';
    break;
    }
    } string s= "";
    foreach(char c in chars)
    {
    s += c;
    }
    base.Text = s;
    this.SelectionStart = j;
    }
    //输入数字日期
    if ( IsDigit(keyData) )
    {
    if (i >= this.Text.Length ) i = 0;
    this.SelectionLength = 0; chars = base.Text.ToCharArray();
    for(j = i; j<base.Text.Length; j++)
    {
    if( chars[j] == ' ' || IsDigit(chars[j]) )
    {
    chars[j] = GetDigit(keyData);
    break;
    }
    }
    string s= "";
    foreach(char c in chars)
    {
    s += c;
    }
    base.Text = s;
    this.SelectionStart = j + 1;
    }

    //拦截其它所有键
    return true;
    } char GetDigit(Keys keyData)
    {
    int code = (int)keyData;

    if (keyData >= Keys.D0 && keyData <=Keys.D9)
    {
    code -= (int)Keys.D0;
    } if (keyData >= Keys.NumPad0 && keyData <=Keys.NumPad9)
    {
    code -= (int)Keys.NumPad0;
    } return char.Parse(code.ToString());;
    } bool IsDigit(Keys keyData)
    {
    return (keyData >= Keys.D0 && keyData <=Keys.D9 ) ||
       (keyData >= Keys.NumPad0 && keyData <=Keys.NumPad9);
    } bool IsDigit(char c)
    {
    return (c <= '9')  &&  (c>='0');
    }

    protected override void OnEnter(EventArgs e)
    {
    this.SelectionLength = 0;
    this.SelectionStart = 0;
    base.OnEnter (e);
    } protected override void OnSizeChanged(EventArgs e)
    {
    BindButton();
    base.OnSizeChanged (e);
    } protected override void OnLocationChanged(EventArgs e)
    {
    BindButton();
    base.OnLocationChanged (e);
    }
    private void button_Click(object sender, EventArgs e)
    {
    if (mc != null) 
    {
    mc.Dispose();
    mc = null;
    }

    mc = new MonthCalendar();
    this.Parent.Controls.Add(mc); this.Parent.Click += new EventHandler(Parent_Click);
    mc.LostFocus += new EventHandler(mc_LostFocus);
    mc.DateSelected += new DateRangeEventHandler(mc_DateSelected);
                
    mc.Focus();
    mc.Visible = true;
    mc.BringToFront(); mc.Left = this.Left; if (this.Parent.Height - this.Bottom < mc.Height )
    {
    //显示在控件上面
    mc.Top = this.Top - mc.Height;
    }
    else
    {
    //显示在控件的下面
    mc.Top  = this.Bottom;
    }
    } private void mc_LostFocus(object sender, EventArgs e)
    {
    mc.Visible = false;
    } private void mc_DateSelected(object sender, DateRangeEventArgs e)
    {
    this.Text = mc.SelectionEnd.ToString(this.Mask);
    this.Focus();
    mc.Visible = false;
    } protected override void OnLostFocus(EventArgs e)
    {
    CheckDate();
    base.OnLostFocus (e);
    } private bool CheckDate()
    {
    bool bValid = true; if (base.Text == this.GetMaskChars())
    {
    return true;
    } try
    {
    DateTime.Parse(base.Text);
    }
    catch
    {
    bValid = false;
    } if (!bValid)
    {
    base.Text = this.GetMaskChars();
    }            return bValid;
    } private void Parent_Click(object sender, EventArgs e)
    {
    mc.Visible = false;
    } protected override void Dispose(bool disposing)
    {
    if (mc != null) mc.Dispose();
    if (button != null) button.Dispose();

    base.Dispose (disposing);
    } }
    }
      

  9.   

    是啊,用datatimepicker比较方便,而且也简单。
      

  10.   

    搞了一个简单的,勉强能用,主要就textBox1.SelectionStart,textBox1.SelectionLength这两个属性的用法,可以查MSDN
    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if((e.KeyChar==8)  && (e.KeyChar<48 || e.KeyChar>57))
    {
    e.Handled=true;
    }
    else
    {
    if(this.textBox1.SelectionStart==4 || this.textBox1.SelectionStart==7 || this.textBox1.SelectionStart==10 || this.textBox1.SelectionStart==13 || this.textBox1.SelectionStart==16)
    {
    this.textBox1.SelectionStart=this.textBox1.SelectionStart+1;
    this.textBox1.SelectionLength=1;
    e.Handled=true;
    }
    else
    {

    }

    }
    } private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    this.textBox2.Text=e.KeyValue.ToString();
    if(e.KeyValue==37 || e.KeyValue==38 || e.KeyValue==39 || e.KeyValue==40  //屏蔽方向键
    || e.KeyValue==46 )   //屏蔽del键
    {
    e.Handled=true;
    }            
    } private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    { this.textBox1.SelectionLength=1;
    if(this.textBox1.SelectionStart==4 || this.textBox1.SelectionStart==7 || this.textBox1.SelectionStart==10 || this.textBox1.SelectionStart==13 || this.textBox1.SelectionStart==16)
    {
    this.textBox1.SelectionStart=this.textBox1.SelectionStart+1;
    this.textBox1.SelectionLength=1;
    } if(this.textBox1.SelectionStart>18)
    {
    this.textBox1.SelectionStart=0;
    this.textBox1.SelectionLength=1;
    } }
      

  11.   

    datatimepicker好象在web里不能用吧.......
      

  12.   

    我需要在webform下的!各位的好像都是form下的!不过还是谢谢!
    那位有webform下的提供!
      

  13.   

    还是自己写个自定义的吧下面是支持自动换行的TextBox(你可以参考自己写个你所要的,要求可以输入 年:月:日:小时:分钟:秒 )
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace OverRed.RichTextBox
    {
    /// <summary>
    /// 自动处理换行的TextBox
    /// </summary>
    public class RichTextBox : System.Web.UI.WebControls.TextBox
    {
    /// <summary>
    /// 重写的Text属性,获得经过处理的文本
    /// </summary>
    public override string Text
    {
    get
    {
    return format(base.Text);
    }
    set
    {
    base.Text = value;
    }
    }//字符转换
    private string format(string msg)
    {
    //这里是生成了三个全新的字符串,如果需要替换的文本太长,就会对性能产生不好的影响
    msg=ms.Replace( " ", "&nbsp;" ).Replace( Convert.ToString( ( char ) 10 ), "&nbsp;" ).Replace( Convert.ToString( ( char ) 13 ), "<br>" );
    return msg;
    }
    }
    }1.打开.aspx的设计视图
    2.找出工具箱,在上面点右键-选择“添加/删除项”
    3.在弹出的窗口中点浏览,找到你需要的那个自定义控件的.dll
    4.添加,完成
    5.控件添加到工具箱后,就可以像其他server controls一样使用,在需要的地方拖过去就可以