错误消息:从字符串“Label14.Text”转换输入'Integer'无效 [英] Error message: Conversion from string "Label14.Text" to type 'Integer' is not valid
问题描述
大家好,
我的应用程序中有2个标签#14& 15,名为Employee Actions Manager,使用VB.Net2010& amp; MS Access 2013.
我的问题:
当我比较两个结果Type:Number正在上述标签中显示时,我收到一条错误消息,指出从字符串Label14.Text到Integer类型的转换无效。
我的代码:
Dim A As 整数
Dim B As 整数
A = Label14.Text ' 对于计划关闭持续时间
B = Label15.Text ' 实际关闭持续时间
如果 A> = B 那么
PictureBox4.Visible = True ' 悲伤的脸部图像
PictureBox3.Visible = False ' 张开脸图像
Else
PictureBox3.Visible = True ' 快乐的脸部图像
PictureBox4.Visible = 错误 ' 悲伤的脸部图像
结束 如果
你能帮我避免这个错误吗?
提前致谢!!
你的问题在于:
Dim A As 整数
A = Label14.Text
如果你刚读过这篇文章在英语中你可以看到你告诉编译器A将持有一个数字(一个整数),然后你试图将一个字符串值放入其中,Label14.Text
我收集到你想要使用Label14.Text中的内容并将其放入A?
您将要验证输入并执行类似的操作:
Dim A As 整数
Dim temp 作为 整数
' 检查文本框中是否包含数字。
如果 Int32 .TryParse(Label14.Text,out temp)然后
' temp现在将存储Label14中的值.Text
否则
' Label14.Text不是数字
结束 如果
...
查看你的代码:
Dim A As 整数
Dim B As 整数
A = Label14.Text ' < span class =co取消评论>计划关闭持续时间
B = Label15.Text ' 实际关闭持续时间您将A和B声明为整数,所以当你尝试分配一个字符串值时,系统会尝试将字符串转换为整数 - 并且文本字符串Label14.Text不能转换为数字,不能超过文本字符串Sherif Adely。
可能,你的意思是将标签中的值赋给变量,在这种情况下:
Dim A 作为 整数
Dim B As 整数
A = 整数 .Parse(Lable14.Text)
B = 整数 .Parse(L able15.Text)可能会更好。
顺便说一句:帮个忙,并停止使用Visual Studio默认名称 - 你可能还记得label14是今天计划的关闭时间,但是当你需要修改时间是三周时间,那么你呢?使用描述性名称 - 例如lbPlannedClosure - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以在三次击键中获得lbPlannedClosure,其中Label14需要思考约7次击键......
Hi guys,
I have 2 Labels# 14&15 in my application called Employee Actions Manager developed by using VB.Net2010 & MS Access 2013.
My problem:
When I compare between the two results "Type: Number" are being shown in the aforementioned Labels, I get an error message stated that Conversion from string "Label14.Text" to type 'Integer' is not valid.
My codes:
Dim A As Integer
Dim B As Integer
A = "Label14.Text" 'For Planned Closure Duration
B = "Label15.Text" ' For Actual Closure Duration
If A >= B Then
PictureBox4.Visible = True ' Sad face image
PictureBox3.Visible = False ' Happy face image
Else
PictureBox3.Visible = True ' Happy face image
PictureBox4.Visible = False ' Sad face image
End If
So can you help me to avoid this error?
Thanks in advance!!
Your problem is with:
Dim A As Integer A = "Label14.Text"
If you just read this outloud in English you can see that you told the compiler that A will be holding a number (an integer) and then you tried to put a string value into it, "Label14.Text"
I gather that you want to take what is in Label14.Text and put that into A?
You'll want to validate the input and do something similar to:
Dim A As Integer Dim temp As Integer 'Check if the textbox actually has a number in it. If Int32.TryParse(Label14.Text, out temp) Then ' temp will now store the value from Label14.Text Else ' Label14.Text was not a number End If ...
Look at your code:
Dim A As Integer Dim B As Integer A = "Label14.Text" 'For Planned Closure Duration B = "Label15.Text" ' For Actual Closure DurationYou are declaring A and B as integers, so when you try to assigna string value, the system tries to convert teh string to an integer - and the text string "Label14.Text" cannot be converted to a number, any more than the text string "Sherif Adely" can.
Probably, you mean to assign the value in the label to the variable, in which case:
Dim A As Integer Dim B As Integer A = Integer.Parse(Lable14.Text) B = Integer.Parse(Lable15.Text)Would probably work better.
BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "label14" is the planned closure duration today, but when you have to modify it is three weeks time, will you then? Use descriptive names - "lbPlannedClosure" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "lbPlannedClosure" in three keystrokes, where "Label14" takes thinking about and 7 keystrokes...
这篇关于错误消息:从字符串“Label14.Text”转换输入'Integer'无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!