Console Application and Windows Form in VB .Net-Please help!

G

Guest

Hi, Al
I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window available in the same form for users to enter commands and display outputs if some prefer to use character based user interface. I would like to implement the user interface in vb .net. Now I have menus and toolbars ready, but do not now how to get a console window run together with menus and toolbars

Hope I make my question clear and thanks in advance for any help and hint

Thanks
Alison
 
A

Alex Papadimoulis

Alison,

The Console Application is a determed per project, so you really can't have
a Console run under a window. What you could do, however is add a textbox on
your form that accepts input and does the appropriate thing.

-- Alex Papadimoulis

Alison said:
Hi, All
I am trying to design a user interface which provides both menus and
toolbars for some users to click on whatever they want to do, at the same
time, I would like to have a console window available in the same form for
users to enter commands and display outputs if some prefer to use character
based user interface. I would like to implement the user interface in vb
..net. Now I have menus and toolbars ready, but do not now how to get a
console window run together with menus and toolbars.
 
G

Guest

Alex

Thanks for the reply.

I am not sure whether you have seen the user interface of Matlab6.0. I try to do a similar one. The matlab user interface seems to have a command window together with menus and toolbars. I thought that command window was a console application. I might be wrong in this though.

Any idea on this

Thanks again
Aliso
----- Alex Papadimoulis wrote: ----

Alison

The Console Application is a determed per project, so you really can't hav
a Console run under a window. What you could do, however is add a textbox o
your form that accepts input and does the appropriate thing

-- Alex Papadimouli

Alison said:
Hi, Al
I am trying to design a user interface which provides both menus an
toolbars for some users to click on whatever they want to do, at the sam
time, I would like to have a console window available in the same form fo
users to enter commands and display outputs if some prefer to use characte
based user interface. I would like to implement the user interface in v
..net. Now I have menus and toolbars ready, but do not now how to get
console window run together with menus and toolbars
 
T

Tom Shelton

Hi, All
I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window available in the same form for users to enter commands and display outputs if some prefer to use character based user interface. I would like to implement the user interface in vb .net. Now I have menus and toolbars ready, but do not now how to get a console window run together with menus and toolbars.

Hope I make my question clear and thanks in advance for any help and hint.

Thanks
Alison

I'm playing with this a little bit still... So, far what I have will
only work on 2K up, but it could be modified to use a more general
technique to get the console window handle. I think that if I was to
play with the windows style bits, I could get a more attractive
integration (that's what I'm starting to play with now).

ption Explicit On
Option Strict On

Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.StatusBar1 = New System.Windows.Forms.StatusBar
Me.Panel1 = New System.Windows.Forms.Panel
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.MenuItem1})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.MenuItem2})
Me.MenuItem1.Text = "&File"
'
'MenuItem2
'
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "E&xit"
'
'StatusBar1
'
Me.StatusBar1.Location = New System.Drawing.Point(0, 323)
Me.StatusBar1.Name = "StatusBar1"
Me.StatusBar1.Size = New System.Drawing.Size(552, 22)
Me.StatusBar1.TabIndex = 0
Me.StatusBar1.Text = "StatusBar1"
'
'Panel1
'
Me.Panel1.AutoScroll = True
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel1.Location = New System.Drawing.Point(0, 0)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(552, 323)
Me.Panel1.TabIndex = 1
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 1000
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(552, 345)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.StatusBar1)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

' this function only works on 2k up...
Private Declare Function GetConsoleWindow Lib "kernel32" () As
IntPtr
Private Declare Function AllocConsole Lib "kernel32" () As Boolean
Private Declare Function FreeConsole Lib "kernel32" () As Boolean
Private Declare Function SetParent Lib "user32" _
(ByVal hWndChild As IntPtr, _
ByVal hWndNewParent As IntPtr) As IntPtr

Private allocated As Boolean
Private hConsoleWindow As IntPtr
Private hOldParent As IntPtr

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not allocated Then
allocated = AllocConsole()
hConsoleWindow = GetConsoleWindow()
If Not hConsoleWindow.Equals(IntPtr.Zero) Then
hOldParent = SetParent(hConsoleWindow, Me.Panel1.Handle)
End If
End If
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If allocated Then
If Not hOldParent.Equals(IntPtr.Zero) Then
hOldParent = SetParent(hOldParent, hConsoleWindow)
End If
FreeConsole()
End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Static count As Integer = 0

count += 1
Console.WriteLine(count)
End Sub
End Class

I'll post again if I get this working better...
 
G

Guest

Thanks a lot, Tom. I got the idea now. I will start from what you provided here. I believe I will have more questions concerning this. I will ask for more help later
Thanks agai
Aliso

----- Tom Shelton wrote: ----

Hi, Al
I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window available in the same form for users to enter commands and display outputs if some prefer to use character based user interface. I would like to implement the user interface in vb .net. Now I have menus and toolbars ready, but do not now how to get a console window run together with menus and toolbars
Aliso

I'm playing with this a little bit still... So, far what I have wil
only work on 2K up, but it could be modified to use a more genera
technique to get the console window handle. I think that if I was t
play with the windows style bits, I could get a more attractiv
integration (that's what I'm starting to play with now)

ption Explicit On
Option Strict O

Imports Syste
Imports System.Runtime.InteropService
Imports System.Windows.Form

Public Class Form
Inherits System.Windows.Forms.For

#Region " Windows Form Designer generated code

Public Sub New(
MyBase.New(

'This call is required by the Windows Form Designer
InitializeComponent(

'Add any initialization after the InitializeComponent() cal

End Su

'Form overrides dispose to clean up the component list
Protected Overloads Overrides Sub Dispose(ByVal disposing A
Boolean
If disposing The
If Not (components Is Nothing) The
components.Dispose(
End I
End I
MyBase.Dispose(disposing
End Su

'Required by the Windows Form Designe
Private components As System.ComponentModel.IContaine

'NOTE: The following procedure is required by the Windows For
Designe
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMen
Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBa
Friend WithEvents Panel1 As System.Windows.Forms.Pane
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuIte
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuIte
Friend WithEvents Timer1 As System.Windows.Forms.Time
<System.Diagnostics.DebuggerStepThrough()> Private Su
InitializeComponent(
Me.components = New System.ComponentModel.Containe
Me.MainMenu1 = New System.Windows.Forms.MainMen
Me.MenuItem1 = New System.Windows.Forms.MenuIte
Me.MenuItem2 = New System.Windows.Forms.MenuIte
Me.StatusBar1 = New System.Windows.Forms.StatusBa
Me.Panel1 = New System.Windows.Forms.Pane
Me.Timer1 = New System.Windows.Forms.Timer(Me.components
Me.SuspendLayout(

'MainMenu

Me.MainMenu1.MenuItems.AddRange(Ne
System.Windows.Forms.MenuItem() {Me.MenuItem1}

'MenuItem

Me.MenuItem1.Index =
Me.MenuItem1.MenuItems.AddRange(Ne
System.Windows.Forms.MenuItem() {Me.MenuItem2}
Me.MenuItem1.Text = "&File

'MenuItem

Me.MenuItem2.Index =
Me.MenuItem2.Text = "E&xit

'StatusBar

Me.StatusBar1.Location = New System.Drawing.Point(0, 323)
Me.StatusBar1.Name = "StatusBar1"
Me.StatusBar1.Size = New System.Drawing.Size(552, 22)
Me.StatusBar1.TabIndex = 0
Me.StatusBar1.Text = "StatusBar1"
'
'Panel1
'
Me.Panel1.AutoScroll = True
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel1.Location = New System.Drawing.Point(0, 0)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(552, 323)
Me.Panel1.TabIndex = 1
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 1000
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(552, 345)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.StatusBar1)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

' this function only works on 2k up...
Private Declare Function GetConsoleWindow Lib "kernel32" () As
IntPtr
Private Declare Function AllocConsole Lib "kernel32" () As Boolean
Private Declare Function FreeConsole Lib "kernel32" () As Boolean
Private Declare Function SetParent Lib "user32" _
(ByVal hWndChild As IntPtr, _
ByVal hWndNewParent As IntPtr) As IntPtr

Private allocated As Boolean
Private hConsoleWindow As IntPtr
Private hOldParent As IntPtr

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not allocated Then
allocated = AllocConsole()
hConsoleWindow = GetConsoleWindow()
If Not hConsoleWindow.Equals(IntPtr.Zero) Then
hOldParent = SetParent(hConsoleWindow, Me.Panel1.Handle)
End If
End If
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If allocated Then
If Not hOldParent.Equals(IntPtr.Zero) Then
hOldParent = SetParent(hOldParent, hConsoleWindow)
End If
FreeConsole()
End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Static count As Integer = 0

count += 1
Console.WriteLine(count)
End Sub
End Class

I'll post again if I get this working better...
 
G

Guest

Hi, Tom
I cannot see the counting displayed in the console window. How can I make the console window interactive with the form
Thank
Alison
 
T

Tom Shelton

Hi, Tom,
I cannot see the counting displayed in the console window. How can I make the console window interactive with the form?
Thanks
Alison

Are you running it with Ctrl-F5 or F5? If it is with F5, then it will
probably not work. I'm really not sure this approach is going to work for
you anyway. After playing with it a little more, I can't seem to get the
console to respond correctly...
 
A

Alexandre Moura

I tend to agree with Alex (not only because of the name) - you could have a
form that has a single line editable text box and a non editable multiline
textbox to display previously used commands and output, and simulate a
console with that.

Alternatively, you could make your project a console app project, and start
your UI in a separate thread - this will only allow you to provide a
console, and still have a ui, but it isn't a very clean solution - sample
code, for a console app to which I added a windows form:

Module Module1

Sub Main()
'start ui
Dim th As New Threading.Thread(AddressOf StartMenus)
th.Start()

'do you console stuff here
Dim Leave As Boolean = False
While Not Leave
Dim command As String = Console.ReadLine()

If LCase(command) = "exit" Then Leave = True

'other processing
End While

'close ui
f.Close()
End Sub

Dim f As New Form1
Sub StartMenus()
'start your ui here
System.Windows.Forms.Application.Run(f)
End Sub
End Module


--------------------
 
D

Dev Guru

Alex:

Check this product out. http://www.neopixell.com/products.php

I am not assoicated with this company in form or shape but thought this
product might be of help to you. It offers a console "window" from a
standard application.

Let us know what you think of this product.

Dev Guru

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top