2012-12-14 43 views
0

嗨,大家好,可能是一个简单的。 使用C#.Net 4.0和Visual Studio 2012 Ultimate。逻辑来检查多个文本框中的空值

得到了下面的代码:

string part = ""; 
part = txtIOpart.Text; 
txtBatchCV.Text = txtBatchIO.Text; 
txtPartCV.Text = part; 
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg); 
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal(); 
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS(); 
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal(); 

txtBarCV.Text = "*" + Sqlrunclass.SplitInfo_ASno(part, pg) + "*"; 
txtBarNumCV.Text = txtBarCV.Text; 
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location(); 
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc(); 
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height(); 
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter(); 
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit(); 

picTypeCV.Image = ftpclass.Download("CVspecType" + Sqlrunclass.SplitSpec_TypeCV() + ".jpg", "ftp.shaftec.com/Images/TypeJpg", "0095845|shafteccom0", "4ccc7365d4"); 

if (txtBatchCV.Text == null || txtBatchCV.Text == "") 
{ 
    txtBatchCV.Text = "ALL"; 
} 

正如你可以在我检查批次底部看,但我需要检查所有的数据多数民众赞成由一堆方法来设置。如果它看到一个空或空白的txt,每个人将有不同的txt输出。无论如何缩短这个代码?

+0

http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx – ken

+0

'TextBox.Text'是从来没有空' ',它会返回''“'然后。 –

+1

@PLB:即使那么属性返回'String.Empty'。 'txt.Text = null; Console.Write(txt.Text == null);'outputs'“false”'。 –

回答

1

TextBox.Text永远不会是null,那么它会返回""。如果你的方法返回null你可以使用null-coalescing operator

string nullRepl = "ALL"; 
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg) ?? nullRepl; 
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal() ?? nullRepl; 
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS() ?? nullRepl; 
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal() ?? nullRepl; 
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location() ?? nullRepl; 
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc() ?? nullRepl; 
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height() ?? nullRepl; 
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter() ?? nullRepl; 
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit() ?? nullRepl; 
+0

多数民众赞成在相当不错的运气之前从未使用过,生病给它一个尝试队友谢谢! – lemunk

+0

如果方法返回“” – lemunk

+0

@StevenSmith:那么你不能使用'??'-operator。 –

3

尝试,txtBatchCV.Text例如

//Just for null 
txtBatchCV.Text = (txtBatchCV.Text ?? "ALL").ToString(); 

//for both null and empty string 
txtBatchCV.Text = string.IsNullOrEmpty(txtBatchCV.Text) ? "ALL": txtBatchCV.Text; 
1

对于您可以使用string.IsNullOrEmpty(txtBatchCV.Text)首先,它是一个convevience方法,基本上做了你的,如果检查做什么。

1

我会尝试这样的事:

void SetDefaultIfNull(TextBox txt, string defaultVal) 
{ 
    if (string.IsNullOrWhitespace(txt.Text)) 
     txt.Text = defaultVal; 
} 

然后通过每个文本框,默认的方法。

+0

'default'是一个关键字。这不会编译。 ;) – Leri

+0

哦拍,你说得对。我会马上编辑。 –

1

可以ATLEAST使用下列方法之一:

string.IsNullOrEmpty(txtBatchCV.Text)

string.IsNullOrWhitespace(txtBatchCV.Text)

3

你可以通过所有的文本框

foreach (var txt in form.Controls.OfType<TextBox>()) 
{ 
    switch(txt.Id){ 
     case "txtBatchCV": 
     // Do whatever you want for txtBatchCV e.g. check string.IsNullOrEmpy(txt.Text) 
     break; 
    } 
} 
迭代

我借以上从这里:

How do I loop through all textboxes and make them run corresponding actions from action dictionary?

在回答我得到了Tim的评论,我更增添了几分代码解释你能做些什么。我的代码示例从未打算成为一个完整的解决方案。

+0

会更改所有文本框文本。 –