【问题标题】:Placer Holder in C#C# 中的 Placer Holder
【发布时间】:2016-08-24 17:30:35
【问题描述】:

如何在 C# windows 窗体应用程序中使用占位符作为字符串连接...

我尝试了控制台应用程序,它工作正常,但在 Windows 窗体应用程序上它不工作

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Hello {0}", textBox1.Text);
}

输出应该类似于“Hello + TextBox.Text”,但它给出的是“Hello {0}”

【问题讨论】:

  • 你在找string.Format

标签: c# .net windows


【解决方案1】:

使用String.Format()

MessageBox.Show(string.Format("Hello {0}", textBox1.Text));

【讨论】:

    【解决方案2】:

    C# 6.0 支持字符串插值

    MessageBox.Show($"Hello {textbox1.Text}")
    

    或者您可以像其他人所说的那样使用 string.Format。

    【讨论】:

      【解决方案3】:

      做一个

      string.Format("Hello {0}", textBox1.Text);
      

      dot net core 和旧框架的处理方式有所不同。

      【讨论】:

      • 这与.Net Core无关。
      【解决方案4】:

      MessageBox 的第二个参数是为MessageBox 的标题保留。所以这段代码MessageBox.Show("Hello {0}", textBox1.Text); 显示一个MessageBoxHello {0} 作为它的文本和textBox1.Text 作为你的MessageBox 的标题。你可以像这样使用String.Format

      string result = string.Format("Hello {0}", textBox1.Text);
      MessageBox.Show(result);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-18
        • 1970-01-01
        • 2014-01-10
        • 2019-11-09
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        相关资源
        最近更新 更多