|
-
Jan 26th, 2009, 02:00 PM
#1
[2008] What is best practice to abort startup and close form
I've got a single form application.
In the Sub New() of that form I'm creating two database classes.
In those classes if I get database errors (such as connection issues) I'm messagebox'ing the error message and setting an ERRFLAG property.
Back in the Sub New() I'm checking that ERRFLAG and I thought I could ME.CLOSE() - but that's not working.
What all is best practice for this type of operation?
This is what I had up to now
Code:
Sub New()
Me.CMCStarting = True
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
vendorBAL_C = New vendorBAL
If vendorBAL_C.ErrFlag Then
Me.Close()
Exit Sub
End If
caseBAL_C = New caseBAL
If caseBAL_C.ErrFlag Then
Me.Close()
Exit Sub
End If
Call vendorLoad()
Call caseLoad()
Me.CMCStarting = False
End Sub
-
Jan 26th, 2009, 02:20 PM
#2
Re: [2008] What is best practice to abort startup and close form
Would you not be better off doing this in the Form Load event? I've not seen anyone use the constructor (Sub New) of a form for anything other than creating the controls that are going to be on that form.
-
Jan 26th, 2009, 02:23 PM
#3
Re: [2008] What is best practice to abort startup and close form
Probably so - I was about to rip this stuff out and go there...
I think the only reason this started in the NEW() was back when we were prototyping some of this we were binding controls - and thought that doing that in NEW() was best...
-
Jan 26th, 2009, 02:29 PM
#4
Re: [2008] What is best practice to abort startup and close form
Well by all means wait for someone with more experience to post before you go scrapping it all if you want as thats just my opinion and I'm no expert... but like I said, if it was me I would definitely be doing that kind of thing in the form load event.
Also, if you ever need to close the app but for some reason cant call Me.Close I think Application.Exit should do it There's probably some good reason why you shouldn't normally use that command but hey its gotta be there for a reason.
Just to clarify, You can call Me.Close from the Form Load event though.
-
Jan 26th, 2009, 02:46 PM
#5
Re: [2008] What is best practice to abort startup and close form
Thanks
This worked better.
Code:
Private Sub CMCConsole_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CMCStarting = True
vendorBAL_C = New vendorBAL
If vendorBAL_C.ErrFlag Then
Me.Close()
Exit Sub
End If
caseBAL_C = New caseBAL
If caseBAL_C.ErrFlag Then
Me.Close()
Exit Sub
End If
Call vendorLoad()
Call caseLoad()
Me.CMCStarting = False
End Sub
But I'm still eager to hear some more opinions.
-
Jan 26th, 2009, 02:48 PM
#6
Re: [2008] What is best practice to abort startup and close form
I'd start the application from a shared sub main. In this sub, I'd create the necessary DB connections... and if succeed, I'll go on instantiate the form and show it, else I exit the app.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jan 26th, 2009, 02:51 PM
#7
Re: [2008] What is best practice to abort startup and close form
 Originally Posted by stanav
I'd start the application from a shared sub main. In this sub, I'd create the necessary DB connections... and if succeed, I'll go on instantiate the form and show it, else I exit the app.
And still do MESSAGEBOX.SHOW's from that SUB MAIN?
-
Jan 26th, 2009, 03:14 PM
#8
Re: [2008] What is best practice to abort startup and close form
 Originally Posted by szlamany
And still do MESSAGEBOX.SHOW's from that SUB MAIN?
Yes... Why can't you? If the sub main is in a separate class, all you have to do is to fully qualify the class name or imports the System.Windows.Forms namespace.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jan 26th, 2009, 03:18 PM
#9
Re: [2008] What is best practice to abort startup and close form
I'm just asking stupid questions...
I don't do much .net coding - I'm a sql programmer for the most part with a pile of old VB6 programs we've used for the past 10 years
-
Jan 26th, 2009, 03:26 PM
#10
Re: [2008] What is best practice to abort startup and close form
What advantage does that have over just doing it in the Form Load event though? I suppose you might save about a second or something where you would of been waiting for the form class to be instantiated but isn't that the only difference?
-
Jan 26th, 2009, 03:31 PM
#11
Re: [2008] What is best practice to abort startup and close form
I'm leaning of leaving it in the FORM_LOAD because most of what happens after the SQLDataAdapter/DataTables/DataSet are created in the CLASS is that they are bound to control objects anyway...
-
Jan 26th, 2009, 05:26 PM
#12
Re: [2008] What is best practice to abort startup and close form
 Originally Posted by chris128
What advantage does that have over just doing it in the Form Load event though? I suppose you might save about a second or something where you would of been waiting for the form class to be instantiated but isn't that the only difference?
What advantage? The form only gets created when needed... There's no point in creating a form and then destroying immediately because you find out that you don't need it... If you can check to see whether the form is needed in 1st place, why not doing so?
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jan 26th, 2009, 08:00 PM
#13
Re: [2008] What is best practice to abort startup and close form
The correct way to do this in VB 2005 and later is to handle the Startup event of the application. You do your application initialisation there and, if it fails, you set e.Cancel to True. That's it. The startup form will never be created and the app will exit gracefully. The application events like Startup and Shutdown are generally used INSTEAD of a Main method when the Application Framework is enabled, which it is by default. You can disable the Application Framework and use your own Main method (instead of the one that is otherwise hidden within auto-generated code) but, really, why would you?
-
Jan 27th, 2009, 05:12 AM
#14
Re: [2008] What is best practice to abort startup and close form
Now I see why Max was so fascinated by the Startup event
-
Jan 27th, 2009, 07:58 AM
#15
Re: [2008] What is best practice to abort startup and close form
 Originally Posted by chris128
Now I see why Max was so fascinated by the Startup event 
-
Jan 27th, 2009, 09:21 AM
#16
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|