question

EugeneGough-2857 avatar image
0 Votes"
EugeneGough-2857 asked EugeneGough-2857 commented

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

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


dotnet-visual-basicwindows-formsdotnet-cli
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered EugeneGough-2857 commented

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")
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IIt now shows your InputBox solution as the answer.

0 Votes 0 ·
EugeneGough-2857 avatar image
0 Votes"
EugeneGough-2857 answered SimpleSamples commented

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. :-)
@SimpleSamples

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

With computers, the more we learn the more we know how much we do not know. I know you know that and I am just saying I understand. For a password, when security is important, you can just create your own form and then give a textbox a password property, but you know that.

0 Votes 0 ·

I am confused. If you are going to use InputBox then my answer was helpful but it is not marked as the answer.

0 Votes 0 ·
XingyuZhao-MSFT avatar image
1 Vote"
XingyuZhao-MSFT answered EugeneGough-2857 commented

Hi @EugeneGough-2857 ,
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.png (23.3 KiB)
gif.gif (45.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Previous answer was more to my needs. Use of console was due to my lack of knowledge of the inputbox function in vb.net. Thanks anyway. Item has been marked as answered.

0 Votes 0 ·