Open New Form passing Variable

  • Thread starter Thread starter Chris Thunell
  • Start date Start date
C

Chris Thunell

I would like to open a form and pass variables to the new form as needed.
Example:

Dim myform as new chrisform
myform.visible = true

But what i would like is
Dim myform as new chrisform(Record=5)
or
Dim myform as new chrisform(5)

so the form would get this information, and then open the appropriate
record.

I think this is called overloading??? or doing something with a base class??

Any ideas?
Thanks!
Chris
(e-mail address removed)
 
* "Chris Thunell said:
I would like to open a form and pass variables to the new form as needed.
Example:

Dim myform as new chrisform
myform.visible = true

But what i would like is
Dim myform as new chrisform(Record=5)
or
Dim myform as new chrisform(5)

so the form would get this information, and then open the appropriate
record.

You will have to add a parameterized constructor to 'ChrisForm'.

\\\
Public Class ChrisForm
Inherits ...

Public Sub New(ByVal RecordToOpen As Integer)
MyBase.New()
...
End If
 
If I understand correctly, you want to overload the constructor. In this
example, you have the default constructor (nothing passed), or a constructor
receiving a single integer.
Colin


Public Class chrisform
Inherits Form
Dim privateRecord As Integer

Public Sub New(ByVal record As Integer)
MyBase.New()
privateRecord = record
End Sub

Private Sub chrisform_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'and put in here the stuff to use privateRecord to
'display the correct information, whatever that may be, on your
form
End Sub
End Class
 
Hi Chris, Colin,

Colin explains overloading of the constructor very nicely but just a
couple of changes.

All versions of Sub New will need the calls to
MyBase.New (which Colin showed)
and
InitializeComponent
whether they do it on their own or get another method (or even Sub New to
do it)

For instance.
Public Sub New (a As Integer, b As Integer)
MyBase.New
InitializeComponent()
End Sub

Public Sub New (a As Integer)
New (a, 54)
End Sub

The other point is that if you replace New() with New (SomeArg), you won't
have a default constructor anymore. If New() with no arguments is required,
you'll have to add your second constructor rather than replacing New().

Regards,
Fergus
 
Fergus, thanks for this. I didn't know that defining a new constructor would
hide the default constructor. But it explains a lingering problem I think I
had in week 1 when I started learning VB.Net a few months ago.

It is really funny because when I cherry picked this posting to resolve, I
thought I could do it. I put in the "InitializeComponent" as you have said
also because I noted that this was also in the Windows Form Designer
generated code and it seemed sensible too. But my posting didn't include the
"InitializeComponent" because my IDE displays the error message "Reference
to a non-shared member requires an object reference". I don't really
understand why this is and I have google'd out references to the message and
don't understand the explanation. Further the Windows Form Designer
generated code for the outer class (Form1) has it in there without a
problem. So why is this?
Thank you for putting me straight here.
Colin


Public Class Form1
Inherits System.Windows.Forms.Form

'//Windows Form Designer generated code

Public Class chrisform
Inherits Form
Dim privateRecord As Integer

Public Sub New(ByVal record As Integer)
MyBase.New()
'//Remove the comment on the next line to get the error
'InitializeComponent()
privateRecord = record
End Sub

Private Sub chrisform_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'and put in here the stuff to use privateRecord to
'display the correct record
End Sub
End Class


Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Form load stuff
End Sub
End Class
 
All versions of Sub New will need the calls to
MyBase.New (which Colin showed)
and
InitializeComponent

Is it better to call MyBase.New or MyClass.New?

It seems to me that if you call MyClass.New, then the default constructor
gets called as well as InitializeComponent.

Public Sub New(RecNo As Integer)
MyClass.New 'Calls InitializeComponent in the default constructor
'Do something with RecNo here
End Sub

Any thought on this?
 
Hi Chris,

I guess I wasn't so clear in that last post. I was wondering whether it
was perhaps a bit clumsy, and it has proved so. ;-)

There were two parts to the sentence - the first part on its own gives the
wrong impression.

|| All versions of Sub New will need the calls to
|| MyBase.New
|| InitializeComponent
|| whether they do it on their own or get another method
|| (or even Sub New to do it)

The last sentence in the block was suggesting what you are saying and
preceded examples showing just what yours does - but without using the MyClass
keyword

Public Sub New (a As Integer)
New (a, 54) 'Same as MyClass.New (a, 54)
End Sub

This is a technique that I have used more in C# where a single new() does
all the work and all the overloads just parcel up the parameters in various
ways.

The keyword MyClass is not required with New as this can only refer to the
current class. MyClass comes into its own when a derived class overrides a
method, say Foo, and a method , eg Bar, in the <current > class wants to
access its own Foo rather than the derived one. The Language Reference will
explain this more clearly. I've only just read it, lol, as I've not come
across MyClass before. (So thanks for asking - I learned something too ;-)).

Regards,
Fergus
 
Hi Colin,

|| when I cherry picked this posting to resolve,
|| I thought I could do it.

And so you should and glad you did, because there was nothing wrong with
your knowledge, and only those two points with the answer. One of those was a
by-the-way and the other was a mistake, not in knowledge, but in setting up
your test scene.

I'm guessing but I'd say that you created a new Form (Form1) and then
created the class for chrisform inside it. Instead, you could have simply
renamed Form1 to chrisform. As it was, you then had a nested class without an
InitializeComponent of its own. The InitializeComponent that chrisform was
referring to belonged to Form1 and there was, of course, no object reference.

Keep cherry picking - every basket adds to the harvest. ;-)

Regards,
Fergus
 
Hi Chris,

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top