在VB.NET中一起在Label上显示多条消息

时间:2014-08-31 21:17:02

标签: vb.net vb.net-2010

NET实际上我正在创建一个表单,我需要执行三个文本框的验证并在单个标签上显示消息我为所有三个文本框创建了代码以进行验证但是当我在验证按钮上调用它时它只执行最后调用的函数请帮助我希望它在标签

上显示来自所有三个文本框的消息
sub ValidatingBox1 ()
if Textbox1.Text = "" Then
label1.text = "Box 1 is blank"
elseif textbox1.text > 10 Then
label1.text = "Box 1 is not in range"
else: label1.text = "Box 1 is valid"
endif
end sub

sub ValidatingBox2 ()
if Textbox2.Text = "" Then
label1.text = "Box 2 is blank"
elseif textbox2.text > 10 Then
label1.text = "Box 2 is not in range"
else: label1.text = "Box 2 is valid"
endif
end sub


sub ValidatingBox3 ()
if Textbox3.Text = "" Then
label1.text = "Box 3 is blank"
elseif textbox3.text > 10 Then
label1.text = "Box 3 is not in range"
else: label1.text = "Box 3 is valid"
endif
end sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

call ValidatingBox1()
call ValidatingBox2()
call ValidatingBox3()

end sub

2 个答案:

答案 0 :(得分:0)

像这样分配消息

Label1.Text = Label1.text & "Message "

它会像这样出现

方框1为空白方框2有效方框3不在范围内

答案 1 :(得分:0)

您覆盖该消息的事实将使其仅返回最后一个消息。请尝试下面的内容;​​

sub ValidatingBox1 ()

if StringIsNullOrEmpty(Textbox1.Text) Then

' Concatenate error message with message already in label
label1.text = String.Concat(textbox1.text, "Box 1 is blank")
elseif textbox1.text > 10 Then
label1.text = String.Concat(textbox1.text, "Box 1 is not in range")
else: label1.text = String.Concat(textbox1.text, "Box 1 is valid")
endif
end sub

sub ValidatingBox2 ()
if StringIsNullOrEmpty(Textbox2.Text) Then
label1.text = String.Concat(textbox1.text, "Box 2 is blank")
elseif textbox2.text > 10 Then
label1.text = String.Concat(textbox1.text, "Box 2 is not in range")
else: label1.text = String.Concat(textbox1.text, "Box 2 is valid")
endif

end sub


sub ValidatingBox3 ()
if StringIsNullOrEmpty(Textbox3.Text) Then

label1.text = String.Concat(textbox1.text, "Box 3 is blank")
elseif textbox3.text > 10 Then
label1.text = String.Concat(textbox1.text, "Box 3 is not in range")
else: label1.text = String.Concat(textbox1.text, "Box 3 is valid")
endif
end sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' Initialise label
label1.text = ""
call ValidatingBox1()
call ValidatingBox2()
call ValidatingBox3()

end sub