Console Input in a VB.Net Forms program works EXCEPT write and writeline don't display.

Eugene Gough 196 Reputation points
2021-09-16T21:25:58.527+00:00

See code below. Everything works as far as opening, reading and closing the console. The write and writeline do not output to be displayed.

Public Class Form1
    <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="AllocConsole")>
    Private Shared Function AllocConsole() As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function


    <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="FreeConsole")>
    Private Shared Function FreeConsole() As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        getpassword()
    End Sub

 Private Sub getpassword()
        AllocConsole()
        Console.Write("Here I am! ")
        Console.WriteLine("Hello World!")
        Console.WriteLine("Enter Password:  ")
        Dim name As String = Console.ReadLine()

        FreeConsole()
    End Sub
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
328 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,531 Reputation points
    2021-09-16T22:47:45.723+00:00

    Use of a console in a Windows Forms application seems to be finicky and seems to be unnecessary for that. When I changed getpassword to be:

    AllocConsole()
    Console.Write("Here I am! ")
    Console.WriteLine("Hello World!")
    Console.WriteLine("Enter Password:  ")
    Dim name As String = String.Empty
    Try
        name = Console.ReadLine()
    Catch ex As Exception
        MsgBox("Can't write to console:" & vbCrLf & ex.Message)
    End Try
    FreeConsole()
    MsgBox("You entered: " & name)
    

    That works as expected. But why use a console? You can use an InputBox as in:

    Dim name As String = InputBox("Enter a name")
    

2 additional answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,361 Reputation points
    2021-09-17T03:40:10.647+00:00

    Hi @Eugene Gough ,
    If you do want to display 'Console.Write' in WinForm, then you can refer to the following steps.
    Create a Module :

    Imports System.Runtime.InteropServices  
      
    Module Program  
        <DllImport("kernel32.dll")>  
        Private Function AttachConsole(ByVal dwProcessId As Integer) As Boolean  
        End Function  
        Private Const ATTACH_PARENT_PROCESS As Integer = -1  
        Sub main()  
            AttachConsole(ATTACH_PARENT_PROCESS)  
            Application.Run(New Form1())  
        End Sub  
    End Module  
    

    Then in 'Project Properties' > Application , set 'Startup object' to 'Sub Main' and 'Application type' to 'Console Application', finally un-check the 'Enable application framework' check box.
    132944-1.png

    Code in button1:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles   
        Button1.Click  
            Console.Write("Here I am! ")  
            Console.WriteLine("Hello World!")  
            Console.WriteLine("Enter Password:  ")  
            Dim name As String = Console.ReadLine()  
            TextBox1.Text = name  
        End Sub  
    

    Result of my test:
    132991-gif.gif
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Eugene Gough 196 Reputation points
    2021-09-16T23:19:14.347+00:00

    Thanks. I didn't know about InputBox. I had never had an occasion for the need to get a password prior to allowing a screen to fill. Thanks. Even at 87, I just keep on finding out that I don't know a lot of stuff. :-)
    @Sam of Simple Samples