By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,166 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,166 IT Pros & Developers. It's quick & easy.

strange behavior , has someone an explanation for this ??

P: n/a

i have 3 forms

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show(Me)
End Sub
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form3
frm.Show(Me)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)

MsgBox(Form2.test)
End Sub
End Class
why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing

regards

Michel

May 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
M. Posseth wrote:
i have 3 forms

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show(Me)
End Sub
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form3
frm.Show(Me)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)

MsgBox(Form2.test)
End Sub
End Class
why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing

regards

Michel

Maybe because you are loading Form1 as Form1, but you are instantiating
a new Form2 as frm as a private variable in Form1.

T
May 11 '06 #2

P: n/a

Nope....

just tried it this way , and it behaves exactly the same ,

Someone else willing to shine his light on this ??
Public Class Form1
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim formB As New Form2
formB.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim formC As New Form3
formC.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class
Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
MsgBox(Form2.test)

End Sub
End Class


"tomb" wrote:
M. Posseth wrote:
i have 3 forms

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show(Me)
End Sub
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form3
frm.Show(Me)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)

MsgBox(Form2.test)
End Sub
End Class
why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing

regards

Michel

Maybe because you are loading Form1 as Form1, but you are instantiating
a new Form2 as frm as a private variable in Form1.

T

May 11 '06 #3

P: n/a
Aaarghh :-(
Do i feel stupid ...... Ofcourse now i see what you meant Tom

changed this in form1 and form3

Public Class Form1

Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Friend formB As Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
formB = New Form2
formB.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
MsgBox(Form1.formB.test)

End Sub
End Class
and now it works as expected .....

i wil sit in a dark corner for a while and shame ;-|



"tomb" wrote:
M. Posseth wrote:
i have 3 forms

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show(Me)
End Sub
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form3
frm.Show(Me)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)

MsgBox(Form2.test)
End Sub
End Class
why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing

regards

Michel

Maybe because you are loading Form1 as Form1, but you are instantiating
a new Form2 as frm as a private variable in Form1.

T

May 11 '06 #4

P: n/a
I would thought that it is because Feyenoord has won the both major cups
from Holland and was the winer of the final from the playoffs from the
regular competition and was one of the 16 best teams in the Eurocompetition.

But that is another team, so you cannot make a fool of yourself by doing
something stupid as the management from Ajax did like sacking the trainer.

Have you somewhere
private form1 as form1

for sure you don't have that for form2 in my idea.

I find it strange it runs.

Just my thought,

Cor
"M. Posseth" <MP******@discussions.microsoft.com> schreef in bericht
news:EB**********************************@microsof t.com...

Nope....

just tried it this way , and it behaves exactly the same ,

Someone else willing to shine his light on this ??
Public Class Form1
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim formB As New Form2
formB.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim formC As New Form3
formC.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class
Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
MsgBox(Form2.test)

End Sub
End Class


"tomb" wrote:
M. Posseth wrote:
>i have 3 forms
>
>Public Class Form1
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles Button1.Click
> Dim frm As New Form2
> frm.Show(Me)
> End Sub
> Private _test As String
> Friend Property test() As String
> Get
> Return _test
> End Get
> Set(ByVal value As String)
> _test = value
> End Set
> End Property
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles MyBase.Load
> _test = "hallo"
> End Sub
>End Class
>
>Public Class Form2
> Private _test As String
> Friend Property test() As String
> Get
> Return _test
> End Get
> Set(ByVal value As String)
> _test = value
> End Set
> End Property
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles Button1.Click
> Dim frm As New Form3
> frm.Show(Me)
> End Sub
> Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles MyBase.Load
> _test = "hallo"
> End Sub
>End Class
>
>Public Class Form3
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles Button1.Click
> MsgBox(Form1.test)
>
> MsgBox(Form2.test)
>
>
> End Sub
>End Class
>
>
>why gives pressing the button in form3 the value of form1 but not of
>form2
>??
>
>i wonder what i am missing
>
>regards
>
>Michel
>
>
>

Maybe because you are loading Form1 as Form1, but you are instantiating
a new Form2 as frm as a private variable in Form1.

T

May 11 '06 #5

P: n/a

"M. Posseth" <MP******@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
MsgBox(Form2.test)
End Sub
End Class
why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing


As tomb said in his post, the problem is likely that it creates a new
instance of Form2 while it manages to find the existing instance of Form1.
Pass an instance of Form1 and Form2 to Form3 instead. That way you can be
sure about what object you're really using.

First time I see someone using this feature (accessing forms by name instead
of using a variable). I wasn't to thrilled when I saw that MS had included
that in VB 2005. As you just showed it can lead to really weird behaviour.
VB.NET is a perfectly valid OO language, so why not use it as one instead of
trying to go back to old VB6 behaviour?

/claes


May 11 '06 #6

P: n/a
Sorry, still able to be manager from Ajax.

However did you try it already with the "owner" property.

Cor

"M. Posseth" <MP******@discussions.microsoft.com> schreef in bericht
news:ED**********************************@microsof t.com...
Aaarghh :-(
Do i feel stupid ...... Ofcourse now i see what you meant Tom

changed this in form1 and form3

Public Class Form1

Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Friend formB As Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
formB = New Form2
formB.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
MsgBox(Form1.formB.test)

End Sub
End Class
and now it works as expected .....

i wil sit in a dark corner for a while and shame ;-|



"tomb" wrote:
M. Posseth wrote:
>i have 3 forms
>
>Public Class Form1
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles Button1.Click
> Dim frm As New Form2
> frm.Show(Me)
> End Sub
> Private _test As String
> Friend Property test() As String
> Get
> Return _test
> End Get
> Set(ByVal value As String)
> _test = value
> End Set
> End Property
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles MyBase.Load
> _test = "hallo"
> End Sub
>End Class
>
>Public Class Form2
> Private _test As String
> Friend Property test() As String
> Get
> Return _test
> End Get
> Set(ByVal value As String)
> _test = value
> End Set
> End Property
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles Button1.Click
> Dim frm As New Form3
> frm.Show(Me)
> End Sub
> Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles MyBase.Load
> _test = "hallo"
> End Sub
>End Class
>
>Public Class Form3
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles Button1.Click
> MsgBox(Form1.test)
>
> MsgBox(Form2.test)
>
>
> End Sub
>End Class
>
>
>why gives pressing the button in form3 the value of form1 but not of
>form2
>??
>
>i wonder what i am missing
>
>regards
>
>Michel
>
>
>

Maybe because you are loading Form1 as Form1, but you are instantiating
a new Form2 as frm as a private variable in Form1.

T

May 11 '06 #7

P: n/a

Hello Claes

I have and will never run into this problem myself ,,, although i am a
VB6 progger from origin i also never used and will never use code like i
showed in the example .

Someone showed me this problem and i found it interesting enough to
investigate and threw it in the group as i could not explain this behavior to
him myself

ofcourse when ligthning strook and it was clear to me i felt pretty stupid
for not seeing it in forehand ( before the post )

however i am with Cors thought l ,,, why the $%$%% does this even compile
???
( vb makes it to easy to write S#it )
regards

Michel Posseth


"Claes Bergefall" wrote:

"M. Posseth" <MP******@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
MsgBox(Form2.test)
End Sub
End Class
why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing


As tomb said in his post, the problem is likely that it creates a new
instance of Form2 while it manages to find the existing instance of Form1.
Pass an instance of Form1 and Form2 to Form3 instead. That way you can be
sure about what object you're really using.

First time I see someone using this feature (accessing forms by name instead
of using a variable). I wasn't to thrilled when I saw that MS had included
that in VB 2005. As you just showed it can lead to really weird behaviour.
VB.NET is a perfectly valid OO language, so why not use it as one instead of
trying to go back to old VB6 behaviour?

/claes


May 11 '06 #8

P: n/a
Hello M. Posset,

Let's see:

MsgBox(Form1.test)
MsgBox(Form2.test)

Where are Form1 and Form2 variables instantiated?
I think you're using VB2005 so you're accesing the default instance of the forms.
Why it works for Form1 and not for Form2?
Because when your application starts, it shows a default instance of Form1; when you invoke Form2.test, the default instance of Form2 is created, but not shown, so the Load event doesn't fire.
Try this (in Form3):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
Form2.Show 'Fires Form2.Load event.
MsgBox(Form2.test)
End Sub

This way you can see that you're accesing the default instance of Form2, not the instance you were seeing in the screen.
Have always in mind that the default instance of a form is different from any instance that you create.

Regards.
"M. Posseth" <MP******@discussions.microsoft.com> escribió en el mensaje news:0C**********************************@microsof t.com...
|
| i have 3 forms
|
| Public Class Form1
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| Dim frm As New Form2
| frm.Show(Me)
| End Sub
| Private _test As String
| Friend Property test() As String
| Get
| Return _test
| End Get
| Set(ByVal value As String)
| _test = value
| End Set
| End Property
| Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Load
| _test = "hallo"
| End Sub
| End Class
|
| Public Class Form2
| Private _test As String
| Friend Property test() As String
| Get
| Return _test
| End Get
| Set(ByVal value As String)
| _test = value
| End Set
| End Property
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| Dim frm As New Form3
| frm.Show(Me)
| End Sub
| Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Load
| _test = "hallo"
| End Sub
| End Class
|
| Public Class Form3
|
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| MsgBox(Form1.test)
|
| MsgBox(Form2.test)
|
|
| End Sub
| End Class
|
|
| why gives pressing the button in form3 the value of form1 but not of form2
| ??
|
| i wonder what i am missing
|
| regards
|
| Michel
|
May 11 '06 #9

P: n/a

"M. Posseth" <MP******@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.com...

Hello Claes

I have and will never run into this problem myself ,,, although i am a
VB6 progger from origin i also never used and will never use code like i
showed in the example .
Good to hear :-)
Neither will I (or anyone on my team)

Someone showed me this problem and i found it interesting enough to
investigate and threw it in the group as i could not explain this behavior
to
him myself

ofcourse when ligthning strook and it was clear to me i felt pretty stupid
for not seeing it in forehand ( before the post )

however i am with Cors thought l ,,, why the $%$%% does this even compile
???
( vb makes it to easy to write S#it )


Yes, it was interesting. Couldn't understand how it could compile myself
before I remembered seeing something about this feature in the release notes
for VS 2005. Personally I think MS went a step to far in their desire to
please the VB6 programmers.

/claes

/claes
May 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.