如何在 VB.NET 中将 TextBox 中的值显示到 MessageBox 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28332012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display value from TextBox into a MessageBox in VB.NET?
提问by Abtra16
I honestly don't know what I'm doing wrong. I have a textbox named "txtNumSticks" where the user enters a number. After the user hits start, I want a message box to pop up that says "Okay! We'll play with (x) sticks!" But I can't get it to work. First day learning VB.net. Thanks in advance!
老实说,我不知道我做错了什么。我有一个名为“txtNumSticks”的文本框,用户可以在其中输入一个数字。用户点击开始后,我希望弹出一个消息框,上面写着“好的!我们将玩 (x) 根棍子!” 但我无法让它工作。第一天学习VB.net。提前致谢!
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim NumSticks As String
txtNumSticks.Text = NumSticks
Game.Show()
Me.Close()
MessageBox.Show("Okay! We'll play with " & NumSticks & "sticks!")
End Sub
回答by Jay
You are setting the variable the wrong way around you should be assigning NumSticks to the value in the text box so:
您正在以错误的方式设置变量,您应该将 NumSticks 分配给文本框中的值,因此:
NumSticks = txtNumSticks.Text
or alternatively without the use of a variable
或者不使用变量
MessageBox.Show("Okay! We'll play with " & txtNumSticks.Text & "sticks!")
回答by HKImpact
You may want to add a little bit of error checking in your program to make sure your value entered is Numeric.
您可能希望在您的程序中添加一些错误检查以确保您输入的值是数字。
Dim NumSticks As String
NumSticks = txtNumSticks.Text.ToString
If IsNumeric(NumSticks) Then
Game.Show()
MessageBox.Show("Okay! We'll play with " & NumSticks & " sticks!")
Me.Close()
Else
' Let user know the value is non-numeric
MessageBox.Show("Non Numeric Value entered", "Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If