How to Use InputBox in C#
While VB.NET provides a convenient InputBox function for displaying a modal dialog and capturing user input, C# does not have a direct equivalent. However, there is a simple way to achieve similar functionality using the Interaction class from the Microsoft.VisualBasic namespace.
Solution
To use the InputBox equivalent in C#, add a reference to the Microsoft.VisualBasic assembly, then call the InputBox method from the Interaction namespace as follows:
using Microsoft.VisualBasic; string input = Interaction.InputBox("Prompt");
The first argument (prompt) is mandatory, while the remaining arguments (title, default value, x-coordinate, and y-coordinate) are optional.
Example
The following code demonstrates how to display an InputBox dialog with a specified prompt and capture the user's input:
string name = Interaction.InputBox("Please enter your name:"); Console.WriteLine($"Your name is {name}");
By leveraging the Interaction class, you can easily use the InputBox functionality in your C# applications to gather user input through a modal dialog.
The above is the detailed content of How Can I Replicate VB.NET's InputBox Functionality in C#?. For more information, please follow other related articles on the PHP Chinese website!