Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obviously missing a basic concept: pgm flow & windows

Status
Not open for further replies.

cgv

IS-IT--Management
Aug 19, 2002
48
US
Greetings, I'll try to be brief and clear. Fluent in VB6, which is obviously a problem in .NET

simple program: 1) display authenticate form, 2) display 'main' working form & data which may include 'openfile' dialog box and other functions.

My problem is having the app close down once the OpenFile dialog box is closed - I cant seem to figure out how to keep the 'Main' form open, even though I use showdialog().

a) Pgm Startup: authenticate_form...
b) authenticate form performs hide(me) and main.showdialog(me)
c) Main form loads some data.
user may click button that creates and displays
an open file dialog. When a file is selected (or not), dialog closes, main closes - authenticate closes, application ends.

I tried a couple different approaches - such as calling the forms from a main module - but get the feeling thats not the way to go in .NET. Not crazy about 'chaining' 1 window from another (such as instantiating and displaying main form from within the Authentication window), but not sure how else to structure it.

VB.NET examples not very helpful, and either the documentation is weak on this topic, or I am just too dense to get (what I immagine is) a core/basic concept here of tread or flow control.

Any comments greatly appreciated.
 
cgv,

The usual procedure which I have seen here is the other way around. Like first create the main window and in Load event of the main window open the Authentication/Login window (showdialog) and depending on the SUCCESS/FAILURE in Authentication/Login window either close the main window or proceed with main window processings. I am assuming that this what you were trying to find out.
 
Kris,

Thank you very much for the reply.

That makes sense, and I will try it.

Technically though I still dont understand why when the user closes (either through open or cancel) the OpenFileDialog box, the main form closes and the app ends. Im going to guess that the same thing will happen even if my 'main' form is calling the authentication form rather than the other way 'round.

Thanks,

STeve
 
Craig,

of course - Thank you for the fortitude to ask. :) There is more code but I believe that these are the 2 important bits: The first is the authentication form:

This window shows up fine, authenticates the user, and if authenticated – goes on to diaplay the ‘Main’ form. This code is contained within an authentication button on that form...

If HasConnected Then
'if successful logon, update registry & Hide logon form.
'MsgBox("connected as " & CONN_UID & "...show main...")
Put_Reg_Keys()
Me.Hide()

(more stuff here…)

' Create main form & assign dataset to Grid control…
Dim CGRL_MainForm As New CGRL_Form1()
CGRL_MainForm.DataGrid1.DataSource = ds
CGRL_MainForm.DataGrid1.DataMember = "SY01500"
'...Display Main form
CGRL_MainForm.ShowDialog()

End If

'*NOTE* Application terminates immediately of main form
' not displayed as modal dialog
' note I put this .exit here because the application
simply hangs without it. (or rather it is as if the app is open with no form displayed?)
Application.Exit()


*** Here is the code in the main form. It doesnt do much. The user can press a button to select a file (Crystal report). If a file is selected, or if the dialog is cancelled... the application continues on to termination.

Please pardon my code... Im trying a variety of different things, and making a mess so far. :0

****** Main form souce:

'Option Strict On
Imports ADODB
Imports System.IO

Public Class CGRL_Form1

Inherits System.Windows.Forms.Form

Public report_name As String
Public report_path As String

‘ Button1 pressed to select a file……. When the dialog box is closed, the application terminates.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Opendlg As New OpenFileDialog()
Opendlg.InitialDirectory = "c:\"
Opendlg.Filter = "Crystal Report files (*.rpt)|*.rpt|All files (*.*)|*.*"
Opendlg.FilterIndex = 1
Opendlg.RestoreDirectory = True
Opendlg.Title = "Select Crystal Report:"
If Opendlg.ShowDialog(Me) = DialogResult.OK Then
MsgBox(Opendlg.FileName)
'report_path = Path.GetDirectoryName(OpenFileDialog1.FileName)
'report_name = Path.GetFileName(OpenFileDialog1.FileName)
'Label2.Text = report_name
End If
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub

‘ Exit button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub

Private Sub CGRL_Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Update form with values from first item on grid
Label1.Text = DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)
End Sub

Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
' note: this commant crashes 'option strict'.
Label1.Text = DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)
End Sub

** the ‘about’ window created displays & closes just fine (without
** terminating the application.)
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
Dim frm_about As New CGRL_About()
frm_about.ShowDialog(Me)
End Sub

End Class


 
Steve,

Put the code that opens BOTH forms into a sub main procedure. Should be what you need. I use that to instantiate a splash, then a login and finally the app.

Craig
 
Well.... OK. I dont like it, but its working I guess.

The actual solution is 2 parts:

First, as you suggested craig - I used a sub main() to act as the controlling routine for both windows. I guess that externalizes ownership of the main thread of the program - but it did not completely solve the problem. The mainform.showdialog() statement still did not retain control of the user interface after the embedded OpenFileDialog was activated & closed by the user - the app would still complete & terminate.

I had to change the second window call from:

frm_main.showdialog()

to:

Windows.Forms.Application.Run(Frm_main)

----------------------------------------



So... thanks for the replies. I appreciate them.! If anyone has any additional comments regarding control flow, windows, threads - or whatever you want to call it - please feel free! (In short, I dont believe I learned anything - just found a workaround. :D )

Cheers,

Steve




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top