Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't Return a value from a called Form 1

Status
Not open for further replies.

mushin

Programmer
Oct 4, 2000
49
Hi

This is a prototype to return a lookup value to a calling form.



Enter some value on screen
Form1 calls form2
Data from form1 is modified
form2 exits
Form1 wants to display what was entered in form2.

Form1 Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tmpForm2 As New Form2
myText = tb.Text ' set var = screen var
tmpForm2.GetText(myText) ' call lookup form
tb.Text = myText ' set screen var to returned value
End Sub


Form2 code:

Public Class Form2
Inherits System.Windows.Forms.Form
Private mstrText1 As String
Public Function GetText(ByVal DefaultText As String) As String
tb2.Text = DefaultText ' set screen var to passed value
Me.ShowDialog()
Return mstrText1
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mstrText1 = tb2.Text ' set value to be returned from value entered in screen var
Me.Hide()
End Sub
End Class


The data is passed ok to form2 and displays
I modify the data in form2 and exit

Form1 does NOT show the returned data...

I culled the gist of this from posts to this forum that seemed to satisfy the original poster, but this is nor working correctly.

Is the calling portion correct ?
It was not part of the post so maybe I screwed it up ?

* extraneous code ommitted (region, etc)

 
You aren't setting the variable myText to anything on Form2. You're just calling a Function on Form2 with your variable, and then using that same variable.
 
Code:
Private Sub Button1_Click(ByVal sender As System.Object,        ByVal e As System.EventArgs) Handles Button1.Click
        Dim tmpForm2 As New Form2
        myText = tb.Text ' set var = screen var
        [B][I]tb.Text = tmpForm2.GetText(myText)[/I][/B]  ' call lookup form
End Sub
 
Perhaps I am missing something here.....

Form1 sends the value of myText to form2 as mstr
Form2 sets the textbox tb2 to that value by virtue of
the byVal clause,

Public Function GetText(ByVal DefaultText As String) As String
tb2.Text = DefaultText

I modify tb2 and set mstrText1 = tb2.text in the button_click event. The return mstrText1.

MyText is unknown to form2 except as a value passed thru.



 
You are missing a BIG something.
Let me describe you how your program works.
In form1 you have a variable called "myText". When you send it to form2, form2 gets a copy of myText. Although a string is a reference type, it often acts like a value type.
If in form2.GetText you write "ByRef" instead of "ByVal", then form2 will get a pointer to form1.myText and will be able to change it. If you leave the "ByVal", form2 will not be able to change form1.myText at all.
Even if you use "ByRef", writing "tb2.Text = DefaultText" only will not change DefaultText if tb2.Text is changed (again, string acts as a value type).
Last thing, if you write "Return mstrText1" but in form1 you don't write "myText = tmpForm2.GetText(myText)", then the returned value is lost somewhere in your PC memory.
 
mushin said:
MyText is unknown to form2 except as a value passed thru.

Exactly. You are passing a MyText to Form2. That's all you are doing. You aren't doing anything with it's return value. Your function in form2 is returning an empty string--but you aren't even using that string anyways.

Here is the jist of what you are doing:

Code:
I have a string called "Frank"
Hey Form2, do something with this string called "Frank"
Now, I'm going to set my Textbox's Text equal to "Frank"

What you need to do is something like:
Code:
I have a string called "Frank".
Hey Form2, the string you need to work with is "Frank".  While you work, I will sit here and wait.
After you're done, I'll set my Textbox's Text property to what you tell me to set it as, via your function.
 
Thanks again both of you, The function works as expected
with the change to the calling syntax.....

One more question though....

Why does form2 not close and disappear when I execute the
trigger code after the assignment

me.close()

It does the assignment but stays open until I X out ?
Then it returns my value appropriately...

 
Where did you write me.close()?
If you write it after Me.ShowDialog() then this call is waiting until you press the X.
 
Here is the revised code for both forms:

I removed me.close() in the button1_click event, but I still have to click X twice to make this form2 fade into oblivion
so I can view my results......

Public Class Form1
Inherits System.Windows.Forms.Form
Public myText As String
Dim myInt As Int64

'region code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tmpForm2 As New Form2
myText = tb.Text
tmpForm2.GetText(myText)
tb.Text = tmpForm2.GetText(myText) ' call lookup
myInt = Convert.ToInt64(tb.Text)
End Sub
End Class




Public Class Form2
Inherits System.Windows.Forms.Form
Private mstrText As String
Public Function GetText(ByVal DefaultText As String) As String
tb2.Text = DefaultText
Me.ShowDialog()
Return mstrText
End Function

'region code

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mstrText = tb2.Text
End Sub
End Class
 
You called ShowDialog twice. You are colsing two forms that look the same.
Code:
 tmpForm2.GetText(myText)
        tb.Text = tmpForm2.GetText(myText)  ' call lookup
Remove the first line.
 
Thanks for your time with this......

It's working fine now and I can go on with
actually turning this scenario into what I needed in the first place.......

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top