Complete Guide to Implementing VB.NET InputBox Functionality in C#

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: C# | VB.NET | InputBox | Dialog | User Input

Abstract: This article provides a comprehensive exploration of various methods to implement VB.NET InputBox functionality in C#, including the official solution using Microsoft.VisualBasic library and flexible custom dialog implementations. The analysis covers advantages and disadvantages of each approach, with complete code examples and best practice recommendations to help developers choose the most suitable input box implementation for their specific needs.

Introduction

When migrating from VB.NET to C#, developers often encounter the need to implement functionality similar to VB.NET's InputBox. The InputBox is a simple and convenient dialog in VB.NET for obtaining text input from users. Although C# standard library doesn't include a direct built-in equivalent, the same functionality can be achieved through multiple approaches.

Using Microsoft.VisualBasic Library

The most straightforward method involves referencing the Microsoft.VisualBasic assembly, which contains functionality compatible with VB.NET. First, you need to add a reference to Microsoft.VisualBasic in your project:

  1. Right-click on the References folder in Solution Explorer
  2. Select Add Reference
  3. Choose Microsoft.VisualBasic from the .NET tab
  4. Click OK to complete the addition

After adding the reference, you can use the Interaction.InputBox method in your code:

using Microsoft.VisualBasic;

string input = Interaction.InputBox("Please enter content", "Input Dialog", "Default value", 100, 100);

This method accepts multiple parameters, with only the first prompt text parameter being mandatory. Other parameters include dialog title, default text content, and dialog position coordinates on the screen.

Custom Input Dialog Implementation

If you prefer to avoid external dependencies or require higher customization, you can create a custom input dialog. Here's a complete custom implementation:

private static DialogResult ShowInputDialog(ref string input)
{
    System.Drawing.Size size = new System.Drawing.Size(200, 70);
    Form inputBox = new Form();

    inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
    inputBox.ClientSize = size;
    inputBox.Text = "Input Dialog";

    System.Windows.Forms.TextBox textBox = new TextBox();
    textBox.Size = new System.Drawing.Size(size.Width - 10, 23);
    textBox.Location = new System.Drawing.Point(5, 5);
    textBox.Text = input;
    inputBox.Controls.Add(textBox);

    Button okButton = new Button();
    okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
    okButton.Name = "okButton";
    okButton.Size = new System.Drawing.Size(75, 23);
    okButton.Text = "&OK";
    okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39);
    inputBox.Controls.Add(okButton);

    Button cancelButton = new Button();
    cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    cancelButton.Name = "cancelButton";
    cancelButton.Size = new System.Drawing.Size(75, 23);
    cancelButton.Text = "&Cancel";
    cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39);
    inputBox.Controls.Add(cancelButton);

    inputBox.AcceptButton = okButton;
    inputBox.CancelButton = cancelButton;

    DialogResult result = inputBox.ShowDialog();
    input = textBox.Text;
    return result;
}

Usage example:

string input = "Default value";
DialogResult result = ShowInputDialog(ref input);
if (result == DialogResult.OK)
{
    // Use the input value
    Console.WriteLine("User input: " + input);
}

Method Comparison and Selection Guidelines

Advantages of using Microsoft.VisualBasic library:

Disadvantages:

Advantages of custom implementation:

Disadvantages:

Best Practice Recommendations

When choosing an implementation approach, consider the following factors:

  1. Project Requirements: If you only need simple text input functionality, using Microsoft.VisualBasic.Interaction.InputBox is the fastest solution
  2. Maintainability: Custom implementation requires more initial work but is easier to maintain and extend in the long term
  3. Performance Considerations: Both methods have similar performance, but custom implementation can be optimized for specific scenarios
  4. User Experience: Custom implementation can better integrate with the application's overall design style

Conclusion

C# provides multiple ways to implement functionality similar to VB.NET's InputBox. Developers can choose the most appropriate solution based on their project's specific requirements. For rapid prototyping or simple input needs, using the Microsoft.VisualBasic library is the optimal choice; for projects requiring high customization or wishing to avoid external dependencies, custom input dialogs offer greater flexibility.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.