Getting User Inputs in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson illustrates how do you get an user input in Visual Basic

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:

    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)


VB.NET Windows Forms Application:

    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)

Previous Lesson | Next Lesson

Share This

Home | About | Contact | Privacy Policy

Copyright 2018 - All Rights Reserved.