在对话框中输出字符串
Output a String in a Dialog
我想知道如何在对话框中打印字符串。我不想在控制台中打印出字符串。
所以我有这个代码:
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);
}
我已经尝试过在互联网上找到的不同解决方案。
谁能告诉我我需要什么对话框?
如果文字不多,可以使用MessageBox
,否则,如果文字很多,则应使用自定义对话框。
using System.Windows.Forms;
...
private void info_Click(object sender, EventArgs e)
{
string line = System.IO.File.ReadAllText("Bedienungsanleitung.txt"); // Solution Exxplorer Rechtsklick add text file
MessageBox.Show(line);
}
如果您首先在控制台应用程序中工作,则必须将 System.Windows.Forms 的引用添加到您的 solution.To 中,您可以使用解决方案资源管理器中的引用子文件夹并右键单击它然后单击添加 reference.In 到 select 框架,在此选项卡下您将看到 System.Windows.Forms 和 select。
执行此操作后返回代码,然后将其放在 class 的顶部,您可以在其中看到 using 语句
using System.Windows.Forms;
在 MessageBox.Show() 中,我可以看到有 21 种不同的方式(重载)在 .Net Framework 4.5 中使用此 .Show() 方法,可能在其他版本中为 well.That意味着您可以自定义。我更喜欢使用的一种完整签名是
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
}
}
输出如下所示
我想知道如何在对话框中打印字符串。我不想在控制台中打印出字符串。 所以我有这个代码:
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);
}
我已经尝试过在互联网上找到的不同解决方案。 谁能告诉我我需要什么对话框?
如果文字不多,可以使用MessageBox
,否则,如果文字很多,则应使用自定义对话框。
using System.Windows.Forms;
...
private void info_Click(object sender, EventArgs e)
{
string line = System.IO.File.ReadAllText("Bedienungsanleitung.txt"); // Solution Exxplorer Rechtsklick add text file
MessageBox.Show(line);
}
如果您首先在控制台应用程序中工作,则必须将 System.Windows.Forms 的引用添加到您的 solution.To 中,您可以使用解决方案资源管理器中的引用子文件夹并右键单击它然后单击添加 reference.In 到 select 框架,在此选项卡下您将看到 System.Windows.Forms 和 select。
执行此操作后返回代码,然后将其放在 class 的顶部,您可以在其中看到 using 语句
using System.Windows.Forms;
在 MessageBox.Show() 中,我可以看到有 21 种不同的方式(重载)在 .Net Framework 4.5 中使用此 .Show() 方法,可能在其他版本中为 well.That意味着您可以自定义。我更喜欢使用的一种完整签名是
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
}
}
输出如下所示