Page 1 of 1

VB .net help

PostPosted: Thu Mar 17, 2005 10:54 am
by Kenshin17
Ok here's the deal:
I am making a multi-form program, and I am having trouble moving data between forms. I need to know how to dim a form without using the
,dim objectname as new frmname, command. It will work when I return from one of the forms but when I load the next form it makes a new version of the main form and erases the data I input into it from the previous form. So I need to know if anyone knows how to dim a form so I can use it but dim it in such a way that its contents remain when I load another form to input more data.

PostPosted: Fri Mar 18, 2005 10:17 am
by TheMelodyMaker
If transferring data from one form to another is your only issue, you could set up a bunch of public variables to temporarily store the values from the first form just before it closes -- which could then be loaded into the second form. (I hope I'm understanding you correctly here, and if so, I hope it works for you -- it's all I can think of at the moment and I'm not sure how different VB.NET is from VB6. ^_^; )

Edit: You could also try DIMing it dynamically and then REDIMing it with the "Preserve" keyword; I'm not sure if that'll help or not.

PostPosted: Fri Mar 18, 2005 6:49 pm
by Kenshin17
Well that could help. I do have several globals. The problem is because for each subform I have to dim the main one as a new form it erases the data in the original form. Therefore the text I put in a label from one subform is erased when I return to the main form after exiting another sub form. What is the exact syntax for the preserve command?

PostPosted: Fri Mar 18, 2005 8:10 pm
by TheMelodyMaker
Preserve isn't really a command so much as an optional keyword used with the ReDim command. Just use it like this:

Code: Select all
ReDim Preserve varname (dimensions)


I do hope that helps. ^_^;

PostPosted: Sat Mar 19, 2005 2:22 pm
by Cap'n Nick
I have a hunch about what's going on but I'll need some more information. Are you by chance using Dim (name of main form) to call the main form from the subforms? This would not call your old main form but would create a blank copy of the main form, resulting in the apparent loss of data you are experiencing. If this is correct then your data is still there. All you need to do is refer to your original instance of it instead of creating a new copy. I would do this by passing a reference to the main form to each of the subforms as they are created.

PostPosted: Sat Mar 19, 2005 8:02 pm
by Kenshin17
You hit her right in the center. That is exactly what is happening. So exactly how do I pass a refrence to the subs?

PostPosted: Sun Mar 20, 2005 12:06 pm
by Cap'n Nick
Set up a variable in each of the subforms to hold the reference. Then set this variable to your main form. When your subform needs to pass information to the main form or call it back up it uses this reference instead of creating a new copy of the main form. Here is how I would do it:

In the subform:

Code: Select all
'A variable to hold the reference to the main form.
Private mainForm As (class of main form)

'The constructor is altered to accept the reference to the main form as the subform is created.  Make sure this variable is passed ByRef.  ByVal will create another copy, leading again to your original problem.
Public Sub New(ByRef MainFormReference As (class of main form))
    .
    (whatever VB.NET already put in here, don't mess with it)
    .
    mainForm = MainFormReference
End Sub


Now you can pass a reference to the main form to the subform when you create it. In fact, VB.NET will generate an error if you attempt to create a subform without passing this reference.

In the main form:

Code: Select all
'Creating a new instance of the subform with a reference to the main form, "Me".
Dim subform As New (class of subform)(Me)


Use the reference you wrote into the subform, mainForm, to manipulate the main form.

In the subform:

Code: Select all
'Brings up the main form.
mainForm.Show()

'Passes data to the main form.
mainForm.FictionalVariable = whatever

'Changes the text of a text box on the main form.
mainForm.TextBoxName.Text  = "whatever"


This is assuming that your main form stays open. If your main form closes and you attempt to call it you will generate an error.

PostPosted: Sun Mar 20, 2005 12:34 pm
by Mithrandir
General Computer Science:
If you pass a ref to a defunct object, you'll get an error. On the other hand, if you pass the actual object, you can avoid this - but you'll have a (object) MB of memory in use each time.

I just LOVE polymorphing methods, and over-writing classes, don't you?

:lol:

PostPosted: Sun Mar 20, 2005 6:48 pm
by Kenshin17
:dance: :dance: :dance: :dance:
:dance: :dance: :dance: :dance:
IT WORKED!!!!!!!! IT WORKED!!!!!!!!!!

Now all those hours of form making are not in vain!!!!
Cap'n Crack you are my hero!!!!!!!!!!
Thanks MeldoyMaker for your help as well. I guess we both learned something ;)

PostPosted: Sun Mar 20, 2005 7:47 pm
by Cap'n Nick
Hooray for programs that do things!

EDIT: Good things! Programs that do good things!

PostPosted: Mon Mar 21, 2005 11:04 am
by Kenshin17
I just ran into a problem. When I try to retern to a form after I have already called it it freezes. Any way around this?

PostPosted: Wed Apr 06, 2005 2:51 pm
by anime4christ
uhm, not sure what you mean. I've been working with VB.NET for a while now. What exactly is going on? How are you returning to the form? In code or windows?

PostPosted: Wed Apr 06, 2005 3:48 pm
by Cap'n Nick
Yes man! We want code so me we help you with your troubles!

PostPosted: Wed Apr 06, 2005 5:09 pm
by Kenshin17
Um...actually I figured it out. My VB teacher gave me some pointers and they work fine. Sorry :sweat: You guys where a big help though :thumb:

PostPosted: Thu Apr 07, 2005 7:59 am
by Cap'n Nick
Ooh, help from an educated professional! That's better than I could do considering that I am self-educated and that I am a professional only through a grand error of the cosmos.