What Is the C# Version of Vb.Net's Inputdialog

What is the C# version of VB.net's InputDialog?

Add a reference to Microsoft.VisualBasic, InputBox is in the Microsoft.VisualBasic.Interaction namespace:

using Microsoft.VisualBasic;
string input = Interaction.InputBox("Prompt", "Title", "Default", x_coordinate, y_coordinate);

Only the first argument for prompt is mandatory

Input Dialogs in C#

Add a reference to Microsoft.VisualBasic.dll.

Call Microsoft.VisualBasic.Interaction.InputBox(...).

Global Module in C# similar to VB.NET's Module

A module in VB.NET is the exact equivalent of a static class in C#. The only difference is that the VB.NET compiler by default puts your declarations inside the Module in the global namespace. This is a practice that the C# team does not favor, for a good reason, you have to use the class name when you reference a member of the static class in the rest of your code. In other words, you have to use Math.Pi, not just Pi.

It doesn't have to be this way, you can put a Module in a Namespace:

Namespace Contoso.Foobar
Module MyModule
'' etc..
End Module
End Namespace

Now it is the same thing.

What is VB.NET's equivalent of C#'s default keyword?

The VB.NET equivalent to C#'s default is the keyword Nothing. The code you wrote should compile just fine so long as Id.Value returns an Integer value.

The reason your updated sample is going wrong is because of the nature of Nothing. In VB.NET Nothing is the empty value and it's convertible to any type. Now for an If expression, the compiler has to infer what the type of the return should be, and it does this by looking at the two value arguments.

The value Nothing has no type, but the literal 1 has the type Integer. Nothing is convertible to Integer so the compiler determines Integer is the best type here. This means when Nothing is chosen as the value, it will be interpreted as Integer and not Integer?.

The easiest way to fix this is to explicitly tell the compiler that you want 1 to be treated as an Integer?.

Dim n As Integer? = If(True, Nothing, CType(1, Integer?))

What version of Visual Basic introduced static types?

Introduce in the first version of VB.Net (Visual Studio 2003) with the use of Option Strict ON compiler statement. (MSDN)

c# + Inputbox + multidimensional array

Using the inputbox isn't really the best way to accept inputs, as a matter of fact, it's not even good, but to satisfy your needs for now, this should be the shortest route.

VB.NET

 Dim toys(4, 3) As String
For week As Integer = 0 To 3
For day As Integer = 0 To 4
toys(day, week) = InputBox("Please enter value for Day " & CStr(day + 1) & " in week " & CStr(week + 1) & ".")
Next day
Next week

C#

string[,] toys = new string[5, 4];
for (int week = 0; week <= 3; week++) {
for (int day = 0; day <= 4; day++) {
toys(day, week) = Interaction.InputBox("Please enter value for Day " + Convert.ToString(day + 1) + " in week " + Convert.ToString(week + 1) + ".");
}
}

Prompt Dialog in Windows Forms

You need to create your own Prompt dialog. You could perhaps create a class for this.

public static class Prompt
{
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;

return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
}

And calling it:

string promptValue = Prompt.ShowDialog("Test", "123");

Update:

Added default button (enter key) and initial focus based on comments and another question.



Related Topics



Leave a reply



Submit