Powered by Blogger.

Getting User Inputs in Visual Basic .NET 2013

It often happens that you need to prompt the user to enter an expected value.

You could do it using InputBox method which displays a prompt in a dialog box or using a console application using ReadLine method which reads the next line of characters.


VB.NET Console Application:

1
2
3
4
5
6
7
Dim nl As String = Environment.NewLine
' say hi and ask them for a name
Console.WriteLine("Hello there. What's your name?" + nl)
Dim input = Console.ReadLine()
' say something to the user
Console.WriteLine(nl & "Nice to meet you " & input & "." & nl)
Console.Read()

VB.NET Windows Forms Application:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Dim prompt As String = String.Empty
Dim title As String = String.Empty
Dim defaultResponse As String = String.Empty
 
Dim answer As Object
' Set prompt.
prompt = "Hello there. What's your name?"
' Set title.
title = "Getting user input"
' Set default value.
defaultResponse = "Your name here"
 
' Display prompt, title, and default value.
answer = InputBox(prompt, title, defaultResponse)
 
' Say something to the user
Messagebox.Show("Nice to meet you " & answer)