PDA

View Full Version : بررسی خالی نبودن تکست باکس ها



javad_r_85
یک شنبه 26 دی 1389, 09:20 صبح
در یه جاهایی از برنامم لازمه که چک کنم تا ببینم که تمام تکست باکسها خالی نباشند تا کدهای اصلی را بنویسم برای اینکار از کد زیر استفاده میکنم و جواب هم می ده خواستم ببینم روش درستی هست




foreach (Control txt in this.Controls)
{

if(txt is TextBox )
if (txt.Text == string.Empty)
{
MessageBox.Show("NOOO");
return;
}
MessageBox.Show("0ok");
return;
}

amir65gh
یک شنبه 26 دی 1389, 10:28 صبح
به نظر من این کد زیر استفاده کن


int cnt = 0;
foreach (Control c in Controls)
if (c is TextBox && c.Text.Trim().Length > 0)
{
cnt++;
break;
}
if (cnt == 0)
MessageBox.Show("OK");
else
MessageBox.Show("Not OK");

موفق باشید.

tooraj_azizi_1035
یک شنبه 26 دی 1389, 10:50 صبح
سلام،
از متدی که مایکروسافت برای این کار پیش بینی کرده استفاده کن:

using System;

class Sample
{
public static void Main()
{
string s1 = "abcd";
string s2 = "";
string s3 = null;

Console.WriteLine("String s1 {0}.", Test(s1));
Console.WriteLine("String s2 {0}.", Test(s2));
Console.WriteLine("String s3 {0}.", Test(s3));
}

public static String Test(string s)
{
if (String.IsNullOrEmpty(s)) return "is null or empty";
else
return String.Format("(\"{0}\") is not null or empty", s);
}
}
// The example displays the following output:
// String s1 ("abcd") is not null or empty.
// String s2 is null or empty.
// String s3 is null or empty.