c# - 在对话框中输出字符串

标签 c# string dialog output

我想知道如何在对话框中打印字符串。我不想在控制台中打印出字符串。 所以我有这个代码:

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 并选择它。

enter image description here

完成此操作后,返回代码,然后将其放在类的顶部,您可以在其中看到 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

            }
        }

输出如下所示

enter image description here

关于c# - 在对话框中输出字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32739757/

相关文章:

java - 在数组列表中搜索字符串(Java)

javascript - 为什么 JQuery 上的对话框在 XPages 中不起作用

css - 对话框内的 Angular 6 Primeng 确认对话框停用页面

android - 如何添加这个谷歌播放更新对话框?

c# - Custom Tag Helper - 替换 html 标签

c# - 将 XAML 事件订阅到不同 C# 文件中的事件处理程序

C处理可变长度字符串

c# - RandomNumberGenerator 与 Aes 生成 key 。使用 AES 加密时,我应该使用哪个来获取 key (C#)

c# - WinRT : Extract the Icon Associated with a File?

java - Base64 无法将字符串解码为字节数组