我想知道如何在对话框中打印字符串。我不想在控制台中打印出字符串。 所以我有这个代码:
private void info_Click(object sender, EventArgs e)
{
// Solution Exxplorer Rechtsklick add text file
string line = System.IO.File.ReadAllText("Bedienungsanleitung.txt");
Console.Write(line);
}
我已经尝试过在互联网上找到的不同解决方案。 谁能告诉我需要什么对话框?
最佳答案
如果您首先使用控制台应用程序,则必须将 System.Windows.Forms 的引用添加到您的解决方案中。为此,您可以使用解决方案资源管理器中的引用子文件夹,右键单击它,然后单击“添加引用”在那里选择 Framework,在此选项卡下您将看到 System.Windows.Forms 并选择它。
完成此操作后,返回代码,然后将其放在类的顶部,您可以在其中看到 using 语句
using System.Windows.Forms;
在 MessageBox.Show() 中,我可以看到在 .Net Framework 4.5 中有 21 种不同的方式(重载)使用此 .Show() 方法,也可能在其他版本中。这意味着您可以自定义。我更喜欢使用的一种完整签名是
MessageBox.Show("Message", "Title of the dialog", MessageBoxButtons, MessageBoxIcon);
在这里您可以看到一个有效的示例
MessageBox.Show("Do you need to save before exit ?","Select the Option",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
与 MessageBox 相关的另一个重要事实是 DialogResult。我们可以使用它来检查代码中的条件。
private void Exit()
{
DialogResult answer= null;
answer = MessageBox.Show("Are you sure that you need to quit ?\nAll unsaved data will be lost.","Exit Confirmation!",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (answer == DialogResult.Yes)
{
//Do something if the user wants to exit
}
else
{
//Do something if user don't want to exit
}
}
输出如下所示
关于c# - 在对话框中输出字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32739757/