C# 消息框,变量用法

发布于 2024-11-30 14:53:58 字数 366 浏览 5 评论 0原文

我已经搜索过,但我不知道我是否使用了正确的措辞进行搜索。我正在为我的班级编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雪若未夕 2024-12-07 14:53:58

尝试

MessageBox.Show(string.Format ("You are right, it only took you {0} guesses!!!", numGuesses ), "Results", MessageBoxButtons.OK);

查看 http://msdn.microsoft.com/en -us/library/system.string.format(v=VS.100).aspx

try

MessageBox.Show(string.Format ("You are right, it only took you {0} guesses!!!", numGuesses ), "Results", MessageBoxButtons.OK);

see http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.100).aspx

我要还你自由 2024-12-07 14:53:58

您可以使用 String.Format 或简单的字符串连接。

MessageBox.Show(String.Format("You are right, it only took you {0} guesses!!!", myVariable), "Results", MessageBoxButtons.OK);

http://msdn.microsoft。 com/en-us/library/system.string.format(v=VS.100).aspx

连接:

MessageBox.Show("You are right, it only took you " + myVariable + " guesses!!!", "Results", MessageBoxButtons.OK);

两个结果是等效的,但如果您有的话,您可能更喜欢 String.Format同一字符串中的多个变量。

You can use String.Format or simple string concatenation.

MessageBox.Show(String.Format("You are right, it only took you {0} guesses!!!", myVariable), "Results", MessageBoxButtons.OK);

http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.100).aspx

Concatenation:

MessageBox.Show("You are right, it only took you " + myVariable + " guesses!!!", "Results", MessageBoxButtons.OK);

Both results are equivalent, but you may prefer String.Format if you have multiple variables in the same string.

晌融 2024-12-07 14:53:58

String.Format() 怎么样?

MessageBox.Show(String.Format("You are right, it only took you {0} guesses!!!", numGuesses), "Results", MessageBoxButtons.OK);

What about String.Format() ?

MessageBox.Show(String.Format("You are right, it only took you {0} guesses!!!", numGuesses), "Results", MessageBoxButtons.OK);
无所谓啦 2024-12-07 14:53:58

String.Format 是你想要的:

string message = string.Format("You are right, it only took you {0} guesses!!!",numGuesses)

MessageBox.Show(message, "Results", MessageBoxButtons.OK);

String.Format is what you want:

string message = string.Format("You are right, it only took you {0} guesses!!!",numGuesses)

MessageBox.Show(message, "Results", MessageBoxButtons.OK);
白首有我共你 2024-12-07 14:53:58
MessageBox.Show(
                  string.Format(
                                 "You are right, it only took you {0} guesses!!!",
                                Results
                               ), 
                  MessageBoxButtons.OK
               );
MessageBox.Show(
                  string.Format(
                                 "You are right, it only took you {0} guesses!!!",
                                Results
                               ), 
                  MessageBoxButtons.OK
               );
错々过的事 2024-12-07 14:53:58

你的课做得很好,但这个帖子已经很旧了。话虽如此,今天您可以这样解决这个问题:

var firstName = "Sam";
MessageBox.Show($"My first name is { firstName }.", "First Name", MessageBoxButtons.Ok);

您还可以在 catch 块中使用它:

...
}
catch (Exception ex)
{
    MessageBox.Show($"Program encountered an error \n\n{ ex.Message }", "Program Error", MessageBoxButtons.Ok);
}

基本上任何字符串变量都可以以这种方式使用。不确定返回字符串的函数,但我不明白为什么不返回。

You are well done with your class, and this thread is old. With that said, today you can solve this issue as such:

var firstName = "Sam";
MessageBox.Show(
quot;My first name is { firstName }.", "First Name", MessageBoxButtons.Ok);

You can also use it in a catch block:

...
}
catch (Exception ex)
{
    MessageBox.Show(
quot;Program encountered an error \n\n{ ex.Message }", "Program Error", MessageBoxButtons.Ok);
}

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.

等待我真够勒 2024-12-07 14:53:58

我倾向于这样做 - 它允许灵活的参数并且非常紧凑:

private void MsgBox(string msg, params object[] args)
{
    MessageBox.Show(String.Format(msg, args));
}

string routine = "TimerHandler";
int errorcode = 1234;

MsgBox("Error {0} in function {1}.", errorcode, routine);

I tend to do this - it allows flexible arguments and it's quite compact:

private void MsgBox(string msg, params object[] args)
{
    MessageBox.Show(String.Format(msg, args));
}

string routine = "TimerHandler";
int errorcode = 1234;

MsgBox("Error {0} in function {1}.", errorcode, routine);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文