消息框中有两个值? [英] Two values in message box?
问题描述
你好,
如果要在一个消息框中显示两个值,该怎么办?
我的意思是:
Hello,
What should I do if I want to show two values in one message box?
I mean something like:
MessageBox.Show("Your name and age will be: " + name +age ,"New guy");
但这只会将两个字符串拆分在一起,因此没有用.
But this will only split two strings together so it''s useless.
推荐答案
连接字符串(使用+)通常被认为是不好的做法.
使用string.Format().这允许您提供任意数量的位"以插入到字符串中,并允许您指定在何处以及使用哪种格式
在上面的示例中,您需要:
concatenating strings (using +) is generally considered bad practice.
use string.Format(). This allows you to provide any number of ''bits'' to insert into a string and allows you to specify where, and what formatting to use
in your above example you would need:
MessageBox.Show(string.Format("Your name and age will be: {0}{1}",name,age) ,"New guy");
使用此...
Use this...
MessageBox.Show("Your name and age will be: " + name +" "+ age ,"New guy");
或
像这样使用String.Formate函数...
OR
Use String.Formate function like this...
MessageBox.Show(string.Format("Your name and age will be: {0}{1}",name,age) ,"New guy");
我不是说英语的人,所以我可能会错过这里的重点,但我只是不要理解将某物分开"的意思.
对于快速消息框,您可以执行以下操作:
I''m not a native English speaker, so I might miss the important point here, but I just don''t get what "splitting something together" could mean.
For a quick message box you can do this:
MessageBox.Show(
"InitialStringLiteral"
+ integerVarialbe.ToString()
+ "IntermediateStringLiteral"
+ anotherVariable.ToString()
+ "EvenMoreStringLiteralStuff",
"HeadingAfterTheComma"
);
您甚至可以定义将显示哪些按钮和图标.只需参见 http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show.aspx [ StringBuilder [ ^ ].
You can even define what buttons and icon will be shown. Just see http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show.aspx[^]
Aside from the message box, if you want to concatenate large amounts of strings, consider using a StringBuilder[^].
这篇关于消息框中有两个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!