C# 消息框,变量用法
我已经搜索过,但我不知道我是否使用了正确的措辞进行搜索。我正在为我的班级编写一个 C# 程序,但我在使用消息框时遇到了问题。
我试图让消息框显示消息并同时读取变量。我在控制台应用程序中执行此操作没有问题,但在 Windows 端我无法弄清楚。
到目前为止我已经:
MessageBox.Show("You are right, it only took you {0} guesses!!!", "Results", MessageBoxButtons.OK);
效果很好。然而,我试图让 {0} 成为变量 numGuesses 的结果。我确信这很简单,我只是在书本或其他东西中忽略了它,或者我在某个地方的语法不正确。
I have searched but I dont know if I am using the correct verbiage to search for. I am writing a program in C# for my class but I am having trouble with the message box.
I am trying to have the message box show a message and read a variable at the same time. I have no problem doing this in the console applications but I cannot figure it out for the Windows side.
So far I have:
MessageBox.Show("You are right, it only took you {0} guesses!!!", "Results", MessageBoxButtons.OK);
Which works fine. Howerver I am trying to have the {0} be the result of the variable numGuesses. I'm sure this is simple and I am just overlooking it in the book or something, or I have the syntax incorrect someplace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试
查看 http://msdn.microsoft.com/en -us/library/system.string.format(v=VS.100).aspx
try
see http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.100).aspx
您可以使用 String.Format 或简单的字符串连接。
http://msdn.microsoft。 com/en-us/library/system.string.format(v=VS.100).aspx
连接:
两个结果是等效的,但如果您有的话,您可能更喜欢
String.Format
同一字符串中的多个变量。You can use
String.Format
or simple string concatenation.http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.100).aspx
Concatenation:
Both results are equivalent, but you may prefer
String.Format
if you have multiple variables in the same string.String.Format() 怎么样?
What about String.Format() ?
String.Format 是你想要的:
String.Format is what you want:
你的课做得很好,但这个帖子已经很旧了。话虽如此,今天您可以这样解决这个问题:
您还可以在 catch 块中使用它:
基本上任何字符串变量都可以以这种方式使用。不确定返回字符串的函数,但我不明白为什么不返回。
You are well done with your class, and this thread is old. With that said, today you can solve this issue as such:
You can also use it in a catch block:
Basically any string variable can be used in this way. Not sure about functions that return a string, though I don't see why not.
我倾向于这样做 - 它允许灵活的参数并且非常紧凑:
I tend to do this - it allows flexible arguments and it's quite compact: