2013-02-14 86 views
1

我正在创建一个程序来计算平均值。有12个TextBox,我想创建一些空白的可能性。现在只有错误和程序崩溃。有可能创造这个?是否可以忽略TextBox?

这部分代码:

ItalianoScritto = (TextBox1.Text) 
MatematicaScritto = (TextBox2.Text) 
IngleseScritto = (TextBox3.Text) 
InformaticaScritto = (TextBox4.Text) 
ScienzeScritto = (TextBox5.Text) 
FisicaScritto = (TextBox6.Text) 
MediaScritto = (ItalianoScritto + MatematicaScritto + IngleseScritto + InformaticaScritto + ScienzeScritto + FisicaScritto)/6 
Label10.Text = Str(MediaScritto) 

,如果我离开空白TextBox1中,当我点击按钮来计算平均Vb的说:演员不是从字符串“”有效的类型“单”和Te TextBox1中的栏变成黄色

+0

崩溃的代码是什么样子,它以什么方式崩溃?另外,一些关于编写好问题的提示:http://tinyurl.com/sohints – 2013-02-14 11:35:36

+0

它说: 从字符串“”投射无效,以键入“单一”。 – Simbox97 2013-02-14 11:39:40

+0

变量xxxxxScritto的数据类型是什么?您可以在没有任何检查的情况下自由地将字符串值传递给潜在的数字变量你有选择严格吗? – Steve 2013-02-14 11:57:51

回答

0

你只需要检查TextBox是每一个使用空白前值:

If TextBox7.TextLength <> 0 Then 
    'Use the value inside 
End If 

这样做的方式取决于你的很多代码。你应该考虑编辑你的问题,提供更多信息(和代码),以便我们更好地帮助你。

2

我将执行以下操作: 遍历文本框并检查是否可以将值解析为迭代器。如果是,请将其添加到值列表中。 然后添加该列表中的所有值,并将其除以个案数量。 它比大if语句更快,弹性反对错误

dim TBList as new list(of Textbox) 
'add your textboxes to the list here 
TbList.add(Textbox1) 
... 

dim ValList as new List(Of Integer) 
for each elem in Tblist 
    dim value as integer 
    If integer.tryparse(elem.text,value)=True 
    ValList.add(Value) 
    else 
    'report error or do nothing 
    end if 
next 

dim Result as Integer 
Dim MaxVal as Integer =0 
for each elem in ValList 
    Maxval +=elem 
next 

Result = MaxVal/ValList.count 

如果您需要点值支持,只要选择双人或单人,而不是整数。 另外:无论你做什么 - 检查文本框中的值是否为数字。如果你省略了tryparse,有人会输入“A”,你的应用程序将崩溃并烧毁。

另外:你选择严格!

相关问题