A quick request for feedback… How often do you:
- Use Sub New in a form instead of handling the form’s Load event?
- When you do use Sub New, are there things that you have to do before the call to InitializeComponent, or do things generally go after the call to InitializeComponent?
We’re thinking about how to solve some issues that we’ve uncovered in the beta and this feedback might help in deciding how to deal with them. Thanks!
I use new many times, often with parameters as well. And at least some times things are dynamic enough that my code must run before InitializeComponent. And for that matter, I often must modify the code that VS claims we are not supposed to modify.
1. I usually use Form_Load in VB and the constructor in C#. I think Form_Load occurs earlier then the constructor of forms, am I right?
2. I usually put the code after the call to InitializeComponent.
Are you deciding to expose Sub New to the user partial of the Form class?
I use Sub.New often. In most cases it’s for SetStyles usually after InitializeComponent.
I use Sub New only if I need to possibly cancel the creation of a form (since throwing an error in the constructor will ‘cancel’ object creation) – and in this case it is almost always before the call to InitializeComponent.
Its a fairly rare requirement – maybe 10% of the time.
I use New then create new instance of Form, and pass parameters then need to assign values to private variable of instance of Form.
l do assignment BEFORE InitializeComponent.
If l need to assign any attributes of controls of form by value of private variables l do it AFTER InitializeComponent
I use load event enough seldom.
Normally I’m using the Sub New method and place a call to my own Initialize method after Initialize Component. When I want to pass an object to the Form, I overloads Sub New with a parameter and call this instead.
I never use Form_Load.
I usually use Form_Load event.
1) I don’t like to mix my code with the windows forms designer generated code.
2) It’s more "clean" for everything that it’s "layout" in my mind…
I use the constructor when:
a) I have to do something before initialize component (really rare)
b) I’m creating a form that will be base for other forms (using sub New I’m sure that this code will run even if someone overrides OnLoad)
…BTW what about default instance in VB.NET 2005? It’s really the most ugly feature I can imagine now… 🙁 Isn’t it better to control the creation/destruction of forms with your own code? Do you know if there will be?
I don’t know how, but a really cool stuff for next generation of Windows Forms in my opinion would be the possibility of doing an Application.run of second WF before closing the main one…
Example: in a "multi form" wizard it would be nice to have a button in which I can write:
P.S.=Sorry for my bad english… 🙁
Pingback: .NET Forever
Normally I prefer using Form_Load event.
I use New to create new instance of Forms with special parameters and normally I usually put the code after the call to InitializeComponent.
I almost always use the form constructor and place init code after the InitializeComponent call.
I’ve used the Load event handler one time.
I always use the form’s Load event.
I still use the Form_Load event for most of the initialization of the form. The only time that I can think that I actually use the New() constructor on forms is when I want to change the signiture of the constructor to take a parameter of some type. Usually this is passing in some object that is created on the main form and the child form needs to interact with that object.
The main reasons I use the Form_Load is a) it VB’ish and just feels right, b) when I double click on a form… that’s where the designer puts me 😉
I can’t remember doing anything before the InitializeComponent…
I would like to see the constructor pulled out of the don’t touch block; however, I can understand the reasoning for it being there. For those that need to modify it, it’s probably fine where it is.
Now that I’ve answered your questions… I’ve got one. You’ve got me curious as to "issues that we’ve uncovered in the beta" are and your rational for these questions in regards to those "issues", after this feedback can you elaborate 😉
Pingback: Panopticon Central
I always use Form_Load because it’s the most convenient (double-click goes there).
I use the constructor often, mostly when inheriting from a subclass of Form. In such cases, the base class would define a constructor with some important argument that must be set at instantiation (or at least at run-time instantiation since the designer requires use of a default constructor), and the end form’s default constructor is usually mapped to the non-default base class constructor.
At least at run-time, the constructor actions definitely need to run before InitializeComponent since the base class will not be in a reliable and/or useable state until its non-default constructor has run.
I place most code in the constructor. If I happen to double-click the form and the Load handler is created, then I may use that. I rarely take the time to manually override OnLoad. I’ve only had to place code before InitializeComponent once, but only because a custom control on the form wasn’t designed very well.
From VS.NET documentation, it seems that it doesn’t really matter whether the New or Load event. So, a more detailed description of what happens between construction and each event would help.
I use the New method to overload thing
public sub new()
public sub new(mydetail as data)
dim fDetails as new frmDetails(ClientX)
I could also overload show. Still figuring out what is the best
I do believe you can label a class as main in your
project properties and call all forms from there.
I believe the method handling MyBase.load
must take the system object param. not sure,
Public Class MyMain
Private Sub main()handles MyBase.load
Dim formOne as Form
formOne = new existingForm
‘ key in before clock runs out
Dim clock ‘ intitialize timer
Dim myTime As Integer = 15
While Cint(time.getSec) < myTime
‘ variables initialized in New Constructor
‘ and all methods accessable after close
‘ call overriden close method
‘ where close method assignes the
‘ data from components to variables
‘ as the destroy/close method called
‘ on close also destroys all components.
‘ override new in your existing form
‘ initialize what you wish to share.
‘ needs testing but I believe
‘ you can instantiate multiple
‘ instances of same form VB.net
‘ if not, existingForm.enabled
‘ allows you to use methods of
‘ form class and the methods
‘ in your form..
‘ like exsistingForm.ShowDialog()
‘ or exsistingForm.MyCount()
So now go use your form and feed it different params
Write a way to define your components with xml
and browse you program like a web page.
I do believe if you have a form.designer.vb source with
the form.vb they combine to create one class thus the key word partial… You can eliminate this, but why
Just Getting this now. I know it needs work
base = waveFunc(electronField)
When looking to assign a class as main form I am only given the option of forms that i have in the project…
One could create a form and close it overriding the destroy method to just close the window as soon as it opens… I don’t think this is the best way…
I am in the process of finding out if one could manually set a class as the main load…
This should work. It just has a couple of kinks /