carry values across forms

G

gordon

Hi,

I have a series of forms that collect information from a user. When I write
out my information to a text file after the last file, the values of the
previous forms seem to be blank. I would like to retain the values of
names.textbox1.text and names.textbox2.text for the output file.

Any help greatly appreciated.




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

TextBox1.Focus()

Dim names As New Names

Dim sw As New IO.StreamWriter(filer, True) ' True means append

Dim strName As String = names.TextBox1.Text

Dim strValue As String = names.TextBox2.Text

Dim today As Date = Now()

Dim strdate As String = today.ToString("d")

Dim golfScore As String = TextBox1.Text

sw.WriteLine(String.Format("{0},{1},{2},{3}", strName, strValue, strdate,
golfScore))

sw.Flush()

sw.Close()



names.Show()

Me.Hide()

End Sub
 
C

Cor Ligthert

Gordon,

How do you want to have values in this properties.

Dim names As New Names
Dim sw As New IO.StreamWriter(filer, True) ' True means append
Dim strName As String = names.TextBox1.Text
Dim strValue As String = names.TextBox2.Text

Or you should really do something against all rules, than the textbox1 and
textbox2 in the form/panel/groupbox Names are empty.

Cor
 
G

gordon

Thanks Cor

But I dont quite understand your meaning.

Names is the form that contains textboxe1 and textbox2. There is a button
on that form that allows me to move to another form - scores.
I can then input some values into textbox1 in scores and then I want to
write these to the text file.

so I want the values in the names.textbox1.text and names.textbox2.text to
be available for output as a result of clicking on a button in scores.

Is that what you mean?

Doug
 
C

Cor Ligthert

Gordon

Have a look at the code I showed you as you had made it..
Dim names As New Names
I assume that Names is a Form. You create a completly New form what is
normally completly empty.
Dim strName As String = names.TextBox1.Text
Dim strValue As String = names.TextBox2.Text

You transfering than the values from empty textboxes in two string values.
That has not so much sense.

Why do you create that new form anyhow, I assume that that button is on the
form on which are as well those two textboxes..

Cor
 
C

Cor Ligthert

Gordon,

As a side step

dim a as New A

is working in VBNet 2003

I don't think that anywhere is guaranteed that this will work in whatever
future version as well.

For VBNet it does at least in one thing not fulfil a programming rule.
It has to be understandable for somebody else who is well known with the
program language.

I hope this gives an idea.

Cor
 
G

gordon

I understand what you are saying in relation to transferrring code from one
version to another. My programming language is SAS and it is important to
code well. I am learning Vb.net and am trying to understand the language by
working through some small examples.


However, for my example, I am unable to get the code to work.

Let me put my need antother way.

I have two forms - i want to collect data from the first form and use it in
the second form.

My first form is called names and has textbox1 and textbox2 and a button to
take me to the second form called scores. The second form has more
information that is gathered and once a button is clicked it is appended to
the information from the textbox1 and textbox2 and then written out to a
text file.

If my first form is as I say - textbox1 and textbox2, how can i reference
these values elsewhere in my application?

Thanks

sorry If i have missed the point - but as I say i am new to vb.net.
 
C

Cor Ligthert

Gordon,

In VBNet is this one of the most difficult points. Even more because a lot
of people try to use the VB6 methods.

To do what you ask i reply what you told.
You have a main form that has the major textboxes
You have second form that you use to get the scores in a textbox and write
those to a file together with the two values from your mainform..

Although that I can tell you how to get the names from those textboxes on
form2, is that not a good method.

Better is to pass those values to your second form.
The best thing is to do that in the constructor, however that is as well a
little bit complicated when you are new.
Therefore I tell it you in the most easiest way.

You create on your second form two fields
Friend TheName as String ('str in advance is not done)
Friend TheValue as String (and not Name and Value because that are both
often used names in classes)

In your first form you set now where you open that second form

frm2 as new Form2
frm2.TheName = textbox1.text
frm2.TheValue = textbox2.text
frm2.showdialog 'now the process it stays at form2 because of that
showdialog and the process on form1 stops.
frm2.dispose 'this is one of the exceptions where dispose is adviced.

Now you can do what you want in Form2, except that you do not use that new
form Names however TheName and TheValue direct.

I hope this helps,

Cor







I assume that you did it on your mainform
 
G

gordon

Cor,

Thanks again. You help so many people on this discussion group. I
understand that you are a professional vb.net programmer and it must make
you cringe sometimes with the questions on this group (such as mine). Your
kind assistance is appreciated, as is your willingness to follow-up with
additional information.

Thanks again - my simple forms are now working - but I promise I will read
up on the constructor!!

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top