برنامه نویسی در همه زمینه ها

فروش پروژه های برنامه نویسی دانشجوئی

برنامه نویسی در همه زمینه ها

فروش پروژه های برنامه نویسی دانشجوئی

تعدادی کد اعتبارسنجی(Validation) برای TextBox

TextBox عددی

کد:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

  {

    if ( !( char.IsDigit( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )

     {

                       e.Handled = true;

                 }

}

TextBox عددی با اعشار

کد:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 

{

   if ( !( char.IsDigit( e.KeyChar) || char.IsControl( e.KeyChar ) ||(e.KeyChar== (char )46)) )

     {

                      e.Handled = true;

                }

}

TextBox فقط کاراکنری

کد:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

  {

    if ( !( char.IsLetter( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )

                 {

                       e.Handled = true;

                 }

}

TextBox برای فقط حروف بزرگ

کد:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

  {

    if ( !( char.IsUpper( e.KeyChar ) || char.IsControl( e.KeyChar )) )

     {

                       e.Handled = true;

                }

}

TextBox برای فقط حروف کوچک

کد:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

  {

   if ( !( char.IsLower( e.KeyChar ) || char.IsControl( e.KeyChar )) )

     {

                       e.Handled = true;

                }

}

چک کردن TextBoxهای خالی (می‌توانید کلیه TextBox های روی فرمتان را به آن ارسال کنید )

کد:

public static bool ChkEmpty(params System.Windows.Forms.TextBox[ ] tb)  {    int i;    for (i = 0; i < tb.Length; i++)

     {

       if (tb[i].Text.Trim() == "")

      {

        MessageBox.Show("Don't keep field empty");

        tb[i].Focus();

        return false;

      }

    }

   return true;

}

TextBox اعشاری(با علامت اعشار مربوط به هر منطقه)

کد:

string DecimalSeparator = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;

 private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

if ( !( char.IsDigit( e.KeyChar) || char.IsControl( e.KeyChar ) || (DecimalSeparator.IndexOf(e.KeyChar) != -1 ) ) )

{

e.Handled = true;

}

}

منبع : مقداریCodeProject

نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد