Opening a console

Bryan_James

Member
Joined
Apr 3, 2015
Messages
5
Location
Manila, Philippines, Asia, Earth
Programming Experience
Beginner
Hi!
I am a 100% newbie in VB.NET. I just wanted to ask, is there any way to open a console with windows forms? Okay. So what I am planning to do is to create a windows form that has two buttons. I want the first button to open a console application (that is also written in the same program). Is this possible? And, if yes, then how can I run console? And how and where can I code the console program to be executed? Thank you in advance! I'm really sorry, I'm a newbie.
 
It's a bit hard to tell what you're really asking for.
I want the first button to open a console application (that is also written in the same program).
I don't really know what that means. I'm going to give you some general information and we'll see if we can get to the root of the matter.

In .NET code, you can start a new process by calling the Process.Start method. The Process class is a member of the System.Diagnostics namespace, which is imported by default in a Windows Forms Application project and most, if not all, other project types too. Calling Process.Start is much like double-clicking an item in Windows/File Explorer. If you pass an EXE path then that EXE will be executed. If you pass the path of a data file then that file will be opened in its default application. You can even pass a URL to open it in the default browser. You can also pass commandline arguments, much as you can with shortcuts or typing directly into a command prompt in Windows.

In order to run a console application, you can call Process.Start and pass the path of the EXE. That will open a command window, run the program and then close the command window afterwards. If you don't want the window to close then you can pass "cmd.exe" as the file to execute, which is the command prompt itself, and then pass the path of your console application as a commandline argument. If you want to interact with the console application from your code, you can also redirect the standard input and/or output streams to send input and receive output.

Armed with that information, you can try running your console application form your WinForms application and see if it does what you want. If it doesn't, post back and please try to provide a clearer explanation of what you actually want.
 
Console Form

Try this:

VB.NET:
Public Class Form1

	Declare Function AllocConsole Lib "kernel32" () As Integer
	Declare Function FreeConsole Lib "kernel32" () As Integer

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim mydata As String
		If Convert.ToBoolean(AllocConsole()) Then
			Console.WriteLine("THIS IS FIRST LINE")
			Console.WriteLine("THIS IS SECOND LINE")
			Console.WriteLine("THIS IS THIRD LINE")
			Console.WriteLine("THIS IS FOURTH LINE")
			Console.WriteLine("THIS IS FIFTH LINE")
			Console.WriteLine()
			Console.Write("Now enter some data:  ")
			mydata = Console.ReadLine
			Console.WriteLine("You wrote:  " & mydata)
			Console.WriteLine()
			Console.WriteLine("Press any key to Clear")
			Console.ReadKey()
			Console.Clear()
			Console.WriteLine("THE CONSOLE WAS CLEARED")
			Console.WriteLine("Press any key to close console")
			Console.ReadKey()
			FreeConsole()

			'System.Environment.Exit(1)		'ends entire application
		End If
	End Sub

End Class
 
Back
Top