Question How do I call a form in synchronis mode from a console application?

CodyB

Active member
Joined
Jul 23, 2007
Messages
26
Programming Experience
3-5
I have a console application that 99% of the time it just runs. It searches a txt file and extracts information for another app. However it might not find what it's searching for, and in this rare case I want to present a form to the user to fill in the data. I could use a series of inputboxes but I need 12 pieces of data, and I don't want to subject the user to that many popup boxes.

So how do I call a form from a console application and pause the execution of the console app until the form closes?
 
You can add a form to your ConsoleApplication project same way as in WindowsApplication project, needed references are added automatically. Then create an instance and use ShowDialog method. If you want separate projects you have run the forms application as Process and WaitForExit, but embedding the form is best if you want to extract info from it's properties after it closes.
 
Cool, heading in the right direction now.
Now the next problem.
The console window is appearing and is ahead of the form in the z-order and focus. I can't find anyway to override this situation. I would prefer to hide the console window, but I could settle for minimized if that is all I can get. I assume at that point the form would automatically take the focus.
 
I neglected to mention that I used the ShowDialog option. Here is the code I started testing.

VB.NET:
Module Module1

    Sub Main()
        Dim FN1 As Int16 = FreeFile()
        FileOpen(FN1, "C:\Test.txt", OpenMode.Output, OpenAccess.Write)
        PrintLine(FN1, "1st")

        Dim T As New Form1
        'T.Activate()
        If T.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            PrintLine(FN1, T.DialogResult.ToString)
            T.Text = T.TextBox1.Text
        Else
            PrintLine(FN1, T.DialogResult.ToString)
            T.Text = "Cancelled"
        End If
        T.Dispose()

        PrintLine(FN1, "|" & T.Text & "|")

        FileClose(FN1)
    End Sub
End Module

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Me.Focus()
        Me.BringToFront()
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
End Class
 
Use Form.Activate method in Load or Shown event. Focus and BringToFront methods have different purposes.
You don't have to write code to set DialogResult for a button if isn't conditionally, just set the buttons DialogResult property in Designer. You also don't need to use the Close method, when Form.DialogResult is assigned the modal dialog automatically hides and returns control to caller.
 
Consoles are best configured initially, with a shortcut you can set it to run Normal/Minimized/Maximized. Otherwise you have to use Process class and try to find it by MainWindowTitle, if found you have MainWindowHandle and Win32 APIs GetWindowPlacement/SetWindowPlacement, see declarations for example in article Find, Minimize, Maximize, Restore the window.
 
Back
Top