Home > Backend Development > C++ > How Can I Replicate VB.NET's InputBox Functionality in C#?

How Can I Replicate VB.NET's InputBox Functionality in C#?

DDD
Release: 2025-01-03 00:47:39
Original
339 people have browsed it

How Can I Replicate VB.NET's InputBox Functionality in C#?

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");
Copy after login

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}");
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template