迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

在 C# 中创建输入对话框

作者:迹忆客 最近更新:2024/02/01 浏览次数:

本教程将演示如何使用与 VB.Net 中类似的两种不同方法在 C# 中创建输入对话框。

输入对话框是一个弹出窗口,它显示消息提示并请求用户输入。然后可以在代码中使用输入。

C# 中没有 VB.NET 的输入对话框版本,因此你可以使用两种方法之一。第一个也是最简单的方法是使用 Microsoft.VisualBasic.Interaction 中提供的 InputBox。另一种方法是使用 System.Windows.FormsSystem.Drawing 创建你自己的自定义对话框。


使用 Microsoft Visual Basic 的输入对话框

由于 C# 没有像 VB.NET 中那样的输入对话框版本,因此你可以添加对 Microsoft.VisualBasic 的引用并使用它的 InputBox

要使用 Microsoft.VisualBasic,你必须首先按照以下步骤将其添加到项目的引用中。

  • 导航到解决方案资源管理器
  • 右键单击​​引用
  • 点击添加引用
  • 单击左侧的 Assemblies 选项卡
  • 找到 Microsoft.VisualBasic.dll 文件并点击 OK

成功添加引用后,你可以在代码中使用其 using 语句。

using Microsoft.VisualBasic;

输入框本身是通过提供以下参数创建的。

  • 提示:要在窗口中显示的消息。这是唯一需要传递的强制参数。
  • 标题:要显示的窗口标题
  • 默认值:输入文本框的默认值
  • X 坐标:输入框起始位置的 X 坐标
  • Y 坐标:输入框起始位置的 Y 坐标
string input = Interaction.InputBox("Prompt", "Title", "Default", 10, 10);

调用此输入框后,程序将等待响应,然后继续执行其余代码。

例子:

using System;
using Microsoft.VisualBasic;

namespace VisualBasic_Example {
  class Program {
    static void Main(string[] args) {
      // Create the input dialog box with the parameters below
      string input =
          Interaction.InputBox("What is at the end of the rainbow?", "Riddle", "...", 10, 10);

      // After the user has provided input, print to the console
      Console.WriteLine(input);
      Console.ReadLine();
    }
  }
}

在上面的示例中,我们创建了一个带有谜语提示的输入框,出现在屏幕的左上角。提交输入后,将值打印到控制台。

输出:

The letter w

C# 中使用 Windows 窗体的自定义输入对话框

创建输入对话框的另一个选项是创建你自己的自定义输入对话框。创建输入对话框的一个好处是它允许你自定义窗口,而不是第一种方法。要使用以下示例中的代码,你必须首先使用以下步骤添加对 System.Windows.Forms.dllSystem.Drawing.dll 的引用。

  • 导航到解决方案资源管理器
  • 右键单击引用
  • 点击添加引用
  • 单击左侧的 Assemblies 选项卡
  • 找到 Microsoft.VisualBasic.dllSystem.Drawing.dll 文件并单击 OK

成功添加引用后,你可以在代码中使用它们的 using 语句。

例子:

using System;
using System.Windows.Forms;
using System.Drawing;

namespace CustomDialog_Example {
  class Program {
    static void Main(string[] args) {
      // Initialize the input variable which will be referenced by the custom input dialog box
      string input = "...";
      // Display the custom input dialog box with the following prompt, window title, and dimensions
      ShowInputDialogBox(ref input, "What is at the end of the rainbow?", "Riddle", 300, 200);

      // Print the input provided by the user
      Console.WriteLine(input);

      Console.ReadLine();
    }

    private static DialogResult ShowInputDialogBox(ref string input, string prompt,
                                                   string title = "Title", int width = 300,
                                                   int height = 200) {
      // This function creates the custom input dialog box by individually creating the different
      // window elements and adding them to the dialog box

      // Specify the size of the window using the parameters passed
      Size size = new Size(width, height);
      // Create a new form using a System.Windows Form
      Form inputBox = new Form();

      inputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
      inputBox.ClientSize = size;
      // Set the window title using the parameter passed
      inputBox.Text = title;

      // Create a new label to hold the prompt
      Label label = new Label();
      label.Text = prompt;
      label.Location = new Point(5, 5);
      label.Width = size.Width - 10;
      inputBox.Controls.Add(label);

      // Create a textbox to accept the user's input
      TextBox textBox = new TextBox();
      textBox.Size = new Size(size.Width - 10, 23);
      textBox.Location = new Point(5, label.Location.Y + 20);
      textBox.Text = input;
      inputBox.Controls.Add(textBox);

      // Create an OK Button
      Button okButton = new Button();
      okButton.DialogResult = DialogResult.OK;
      okButton.Name = "okButton";
      okButton.Size = new Size(75, 23);
      okButton.Text = "&OK";
      okButton.Location = new Point(size.Width - 80 - 80, size.Height - 30);
      inputBox.Controls.Add(okButton);

      // Create a Cancel Button
      Button cancelButton = new Button();
      cancelButton.DialogResult = DialogResult.Cancel;
      cancelButton.Name = "cancelButton";
      cancelButton.Size = new Size(75, 23);
      cancelButton.Text = "&Cancel";
      cancelButton.Location = new Point(size.Width - 80, size.Height - 30);
      inputBox.Controls.Add(cancelButton);

      // Set the input box's buttons to the created OK and Cancel Buttons respectively so the window
      // appropriately behaves with the button clicks
      inputBox.AcceptButton = okButton;
      inputBox.CancelButton = cancelButton;

      // Show the window dialog box
      DialogResult result = inputBox.ShowDialog();
      input = textBox.Text;

      // After input has been submitted, return the input value
      return result;
    }
  }
}

在上面的示例中,我们创建了一个自定义函数,该函数使用 System.Windows.Forms 中的元素并将它们单独添加到对话框中。虽然你可以对不同的元素进行硬编码,但你也可以在函数中添加参数并引用它们,以获得尽可能多的自定义。在构建并显示对话框后,程序等待用户提供输入以在控制台中打印它。

输出:

the letter w

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C# 中发出 HTTP POST Web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求

在 C# 中调整图像大小

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

在 C# 中下载图片

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据

在 C# 中使用秒表

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。

在 C# 中获取可执行路径

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便