473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get info from another form...

Rob
Let's say you open Form1 that contains TabControl1

There are several tabs on TabControl1

Now you open a new Form2 that contains a User Control

How can you determine the Selected tab in Form1 from the User Control in
Form2 ?

Hope this is clear enough...

Thanks !
May 25 '07 #1
12 2332
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1

There are several tabs on TabControl1

Now you open a new Form2 that contains a User Control

How can you determine the Selected tab in Form1 from the User Control in
Form2 ?

Hope this is clear enough...

Thanks !
IMO the proper way would be to expose the selected tab via a property
in Form1.

' Typed in message
Public Class Form1

Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property

End Class

Public Class Form2

Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub

End Class

Thanks,

Seth Rowe

May 25 '07 #2
Rob
Thanks Seth,

But I am getting an error in in the Set statement...
> Me.TabControl1.SelectedTab = TabPage
It says "TabPag is a type and cannot be used as an expression"

Rob

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@q66g2000hsg.googlegr oups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
>Let's say you open Form1 that contains TabControl1

There are several tabs on TabControl1

Now you open a new Form2 that contains a User Control

How can you determine the Selected tab in Form1 from the User Control in
Form2 ?

Hope this is clear enough...

Thanks !

IMO the proper way would be to expose the selected tab via a property
in Form1.

' Typed in message
Public Class Form1

Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property

End Class

Public Class Form2

Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub

End Class

Thanks,

Seth Rowe

May 25 '07 #3
Rob
Hmmm...

My TabPage was dynamically added (which I did not state)...so the problem
is probably on my end.
"Rob" <ro***@yahoo.comwrote in message
news:P8******************************@comcast.com. ..
Thanks Seth,

But I am getting an error in in the Set statement...
>> Me.TabControl1.SelectedTab = TabPage

It says "TabPag is a type and cannot be used as an expression"

Rob

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@q66g2000hsg.googlegr oups.com...
>On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
>>Let's say you open Form1 that contains TabControl1

There are several tabs on TabControl1

Now you open a new Form2 that contains a User Control

How can you determine the Selected tab in Form1 from the User Control in
Form2 ?

Hope this is clear enough...

Thanks !

IMO the proper way would be to expose the selected tab via a property
in Form1.

' Typed in message
Public Class Form1

Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property

End Class

Public Class Form2

Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub

End Class

Thanks,

Seth Rowe


May 25 '07 #4
On May 25, 11:43 am, "Rob" <r...@yahoo.comwrote:
Thanks Seth,

But I am getting an error in in the Set statement...
Me.TabControl1.SelectedTab = TabPage

It says "TabPag is a type and cannot be used as an expression"

Rob

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@q66g2000hsg.googlegr oups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
There are several tabs on TabControl1
Now you open a new Form2 that contains a User Control
How can you determine the Selected tab in Form1 from the User Control in
Form2 ?
Hope this is clear enough...
Thanks !
IMO the proper way would be to expose the selected tab via a property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe
Me.TabControl1.SelectedTab = TabPage
That should say Me.TabControl1.SelectedTab = value

Like I said - I typed that in the message to just give you an idea of
how it should go - and not necessarily a working solution.

Thanks,

Seth Rowe

May 25 '07 #5
On May 25, 11:53 am, "Rob" <r...@yahoo.comwrote:
Hmmm...

My TabPage was dynamically added (which I did not state)...so the problem
is probably on my end.

"Rob" <r...@yahoo.comwrote in message

news:P8******************************@comcast.com. ..
Thanks Seth,
But I am getting an error in in the Set statement...
> Me.TabControl1.SelectedTab = TabPage
It says "TabPag is a type and cannot be used as an expression"
Rob
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
news:11**********************@q66g2000hsg.googlegr oups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
>There are several tabs on TabControl1
>Now you open a new Form2 that contains a User Control
>How can you determine the Selected tab in Form1 from the User Control in
Form2 ?
>Hope this is clear enough...
>Thanks !
IMO the proper way would be to expose the selected tab via a property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe
My TabPage was dynamically added (which I did not state)...so the problem
is probably on my end.
I shouldn't matter - after all every control is both dynamically
created and added in the InitializeComponent() method. The problem is
my poor "air coding" which had a typo. Also, if you don't plan on
selecting a different tab from Form2, you should probably just make
the property readonly and only expose the Get accessor.

Thanks,

Seth Rowe

May 25 '07 #6
Rob
Seth,

I think I am missing a really "big" concept regarding scope of variables and
instances...

In your example, are you not creating a "new" instance of Form1 when you
call the sub in Form 2 ?
Therefore, aren't you always going to get the default value of whatever
control's property you are returning ?

I want to know the selected tab page of a form whose instance already
exists....

It appears that I am constantly boxing myself into a corner (I can't get
there from here)....
(In MS Access, as long as the form was open, you could issue
Forms.frmWhatever.contol and alway get your answer.)

For example...
Let's say you begin in Form1
There is a Button1 on Form1
Button1 opens an instance of Form2
Button1 also adds a User control to Form2 (This User Control contains a
ButtonU)
On Form2 there is a TextBox1 and a Panel containing the User Control
This is where I run into the dead-end...
From the ButtonU in the user control I want to update the Textbox1 on Form2,
but I cannot appear to "get back" to update that control (i.e.,
frmForm2.TextBox1.Text is not declared) Again, I do not want to update a
"new" instance of Form2, I want to update an existing instance (the one that
was opened from Button1 on Form 1).
Furthermore, I may also want to update somthing in Form1 from within the
User control in Form2, but those "existing" controls appear to be
inaccessible as well.

Is there a way to make all "open instance" form data Public ? If so, is
this a bad idea ?

Also, let me say thanks again for all your input into this newsgroup. You
are a tremendous resource !

Rob


"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
On May 25, 11:43 am, "Rob" <r...@yahoo.comwrote:
>Thanks Seth,

But I am getting an error in in the Set statement...
> Me.TabControl1.SelectedTab = TabPage

It says "TabPag is a type and cannot be used as an expression"

Rob

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@q66g2000hsg.googleg roups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
>There are several tabs on TabControl1
>Now you open a new Form2 that contains a User Control
>How can you determine the Selected tab in Form1 from the User Control
in
Form2 ?
>Hope this is clear enough...
>Thanks !
IMO the proper way would be to expose the selected tab via a property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe

> Me.TabControl1.SelectedTab = TabPage

That should say Me.TabControl1.SelectedTab = value

Like I said - I typed that in the message to just give you an idea of
how it should go - and not necessarily a working solution.

Thanks,

Seth Rowe

May 26 '07 #7
On May 26, 7:34 am, "Rob" <r...@yahoo.comwrote:
Seth,

I think I am missing a really "big" concept regarding scope of variables and
instances...

In your example, are you not creating a "new" instance of Form1 when you
call the sub in Form 2 ?
Therefore, aren't you always going to get the default value of whatever
control's property you are returning ?

I want to know the selected tab page of a form whose instance already
exists....

It appears that I am constantly boxing myself into a corner (I can't get
there from here)....
(In MS Access, as long as the form was open, you could issue
Forms.frmWhatever.contol and alway get your answer.)

For example...
Let's say you begin in Form1
There is a Button1 on Form1
Button1 opens an instance of Form2
Button1 also adds a User control to Form2 (This User Control contains a
ButtonU)
On Form2 there is a TextBox1 and a Panel containing the User Control
This is where I run into the dead-end...
From the ButtonU in the user control I want to update the Textbox1 on Form2,
but I cannot appear to "get back" to update that control (i.e.,
frmForm2.TextBox1.Text is not declared) Again, I do not want to update a
"new" instance of Form2, I want to update an existing instance (the one that
was opened from Button1 on Form 1).
Furthermore, I may also want to update somthing in Form1 from within the
User control in Form2, but those "existing" controls appear to be
inaccessible as well.

Is there a way to make all "open instance" form data Public ? If so, is
this a bad idea ?

Also, let me say thanks again for all your input into this newsgroup. You
are a tremendous resource !

Rob

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...
On May 25, 11:43 am, "Rob" <r...@yahoo.comwrote:
Thanks Seth,
But I am getting an error in in the Set statement...
Me.TabControl1.SelectedTab = TabPage
It says "TabPag is a type and cannot be used as an expression"
Rob
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>news:11**********************@q66g2000hsg.googleg roups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
There are several tabs on TabControl1
Now you open a new Form2 that contains a User Control
How can you determine the Selected tab in Form1 from the User Control
in
Form2 ?
Hope this is clear enough...
Thanks !
IMO the proper way would be to expose the selected tab via a property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe
Me.TabControl1.SelectedTab = TabPage
That should say Me.TabControl1.SelectedTab = value
Like I said - I typed that in the message to just give you an idea of
how it should go - and not necessarily a working solution.
Thanks,
Seth Rowe- Hide quoted text -

- Show quoted text -
Forms in Access are what called singleton forms. Only one instance can
be open at a time - therefore you can always use the
Forms.MyForm.MyField. In .Net you can create any number of the same
form which means you can't use the Forms.MyForm.MyField syntax. Here's
why:

Consider this code which creates 25 Form1's and assigns a value to
it's text property

For i As Integer = 0 to 24
Dim f As New Form1()
f.Text = String.Format("Form Number {0}", i.ToString())
f.Show()
Next i

Now that we have 25 forms, which one will return it's text value when
we do this (if it was a valid statement)?

MessageBox.Show(Forms.Form1.Text)

You see, there is no way for the framework to know which form you
wanted to grab, so this statement is not included in the framework.

In your case you have two options, one is to create an instance of
Form1 and keep a reference to it by using a property or variable. For
example:

Public Class Form2

Dim MyTrackedForm1 As Form1

Public Sub New()
InitializeComponent()
MyTrackedForm1 = New Form1()
' Set other properties for MyTrackedForm1
End Sub

Public Sub Button_Click(.....)
MessageBox.Show(MyTrackedForm1.Text)
End Sub

End Class

Now you create an instance of Form1 in the beginning of an instance of
Form2, and store a reference to it in the MyTrackedForm1 variable.
Then any time you need to access one of it's properties/methods you
just use the MyTrackedForm1 variable to access the instance you
created. This approach works great when you only have one form
accessing an instance of Form1, but if you have multiple different
forms (like a form2, form3, form4, etc) all trying to access a certain
Form1's members, you would have to use a global variable (which I
dislike) and remember to only use it.

This global variable also doesn't stop Form1 from being instantiated
multiple times, but this next approach does. What we are going to do
is use the Singleton design pattern and make it so that only one Form1
can exist at a time, and all requests will go to that certain Form1.
If you want to know more about Singleton I would recommend you do some
searching on google or wikipedia - others have done a better job
explaining it's strength and weaknesses better than I could. Anyways,
basically we want to make it so that noone can instantiate our form
directely - we do this with a private (or protected) constructor like
so:

Private Sub New()
InitializeComponent()
End Sub

Now that no outside objects can create Form1 we need to give them a
way to access Form1, we do this with a variable/property pair. And
since we want this property to be visible to everyone without needed
Form1 to be instantiated, we use a shared property and variable.

Private _Instance As Form1
Public Property Instance As Form1
Get
If _Instance Is Nothing Then
_Instance = New Form1()
' Set other properties if necessary
End If
Return _Instance
End Get
End Property

If you look the property checks to see whether or not Form1 is already
created in the get accessor. If it is, it return the current Form1, if
not however it creates a new instance of Form1 and return's it.

Now to use this all you need to do is use this Singleton pattern on
any form you want to be a single instance form. Then just expose any
necessary fields by use of properties. From what I can tell you want
to have a singleton instance of Form2 and expose the text property of
it's textbox1. This basic form design should be:

Public Class Form2

Private _Instance As Form2
Public Readonly Property Instance As Form2
Get
If _Instance Is Nothing Then _Instance = New Form2()
Return _Instance
End Get
End Property

Private Sub New()
InitializeComponent()
End Sub

Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set (ByVal value As String)
TextBox1.Text = value
End Set
End Property

End Class

Then in the Button1 click event in Form1 you can just do this:

Form2.Instance.Controls.Add(New UserControl1)
Form2.Instance.ShowDialog(Me)

And in the ButtonU click event in the UserControl you can do this:

Form2.Instance.TextBoxText = "My new value"

And you should be set!
Thanks,

Seth Rowe

May 26 '07 #8
On May 26, 7:34 am, "Rob" <r...@yahoo.comwrote:
Seth,

I think I am missing a really "big" concept regarding scope of variables and
instances...

In your example, are you not creating a "new" instance of Form1 when you
call the sub in Form 2 ?
Therefore, aren't you always going to get the default value of whatever
control's property you are returning ?

I want to know the selected tab page of a form whose instance already
exists....

It appears that I am constantly boxing myself into a corner (I can't get
there from here)....
(In MS Access, as long as the form was open, you could issue
Forms.frmWhatever.contol and alway get your answer.)

For example...
Let's say you begin in Form1
There is a Button1 on Form1
Button1 opens an instance of Form2
Button1 also adds a User control to Form2 (This User Control contains a
ButtonU)
On Form2 there is a TextBox1 and a Panel containing the User Control
This is where I run into the dead-end...
From the ButtonU in the user control I want to update the Textbox1 on Form2,
but I cannot appear to "get back" to update that control (i.e.,
frmForm2.TextBox1.Text is not declared) Again, I do not want to update a
"new" instance of Form2, I want to update an existing instance (the one that
was opened from Button1 on Form 1).
Furthermore, I may also want to update somthing in Form1 from within the
User control in Form2, but those "existing" controls appear to be
inaccessible as well.

Is there a way to make all "open instance" form data Public ? If so, is
this a bad idea ?

Also, let me say thanks again for all your input into this newsgroup. You
are a tremendous resource !

Rob

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...
On May 25, 11:43 am, "Rob" <r...@yahoo.comwrote:
Thanks Seth,
But I am getting an error in in the Set statement...
Me.TabControl1.SelectedTab = TabPage
It says "TabPag is a type and cannot be used as an expression"
Rob
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>news:11**********************@q66g2000hsg.googleg roups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
There are several tabs on TabControl1
Now you open a new Form2 that contains a User Control
How can you determine the Selected tab in Form1 from the User Control
in
Form2 ?
Hope this is clear enough...
Thanks !
IMO the proper way would be to expose the selected tab via a property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe
Me.TabControl1.SelectedTab = TabPage
That should say Me.TabControl1.SelectedTab = value
Like I said - I typed that in the message to just give you an idea of
how it should go - and not necessarily a working solution.
Thanks,
Seth Rowe- Hide quoted text -

- Show quoted text -
I forgot to answer this in my last post...
Is there a way to make all "open instance" form data Public ?
Yes, check out the control's modifier property - you can change it
from private to public.
If so, is this a bad idea ?
Yes! IMO you should only give access to just the fields you need to
expose. If you fully expose TextBox1 on Form2, what happens to the
rest of you program if some other form calls this:

Form2.Instance.Controls.Remove(Form2.Instance.Text box1)
Form2.Instance.Textbox1.Dispose()

For safeties sake, I highly recommend you following the property
pattern and not just set the modifers to public.

Thanks,

Seth Rowe

May 26 '07 #9
Rob
Seth,

Thanks so much again... I am still trying to digest it... but...

Regarding... below...

Should the following be "Public" ? Otherwise I am getting object reference
errors...

Private Sub New()
InitializeComponent()
End Sub

Thanks,
Rob

------------------------------
Public Class Form2

Private _Instance As Form2
Public Readonly Property Instance As Form2
Get
If _Instance Is Nothing Then _Instance = New Form2()
Return _Instance
End Get
End Property

Private Sub New()
InitializeComponent()
End Sub

Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set (ByVal value As String)
TextBox1.Text = value
End Set
End Property

End Class



"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On May 26, 7:34 am, "Rob" <r...@yahoo.comwrote:
>Seth,

I think I am missing a really "big" concept regarding scope of variables
and
instances...

In your example, are you not creating a "new" instance of Form1 when you
call the sub in Form 2 ?
Therefore, aren't you always going to get the default value of whatever
control's property you are returning ?

I want to know the selected tab page of a form whose instance already
exists....

It appears that I am constantly boxing myself into a corner (I can't get
there from here)....
(In MS Access, as long as the form was open, you could issue
Forms.frmWhatever.contol and alway get your answer.)

For example...
Let's say you begin in Form1
There is a Button1 on Form1
Button1 opens an instance of Form2
Button1 also adds a User control to Form2 (This User Control contains a
ButtonU)
On Form2 there is a TextBox1 and a Panel containing the User Control
This is where I run into the dead-end...
From the ButtonU in the user control I want to update the Textbox1 on
Form2,
but I cannot appear to "get back" to update that control (i.e.,
frmForm2.TextBox1.Text is not declared) Again, I do not want to update
a
"new" instance of Form2, I want to update an existing instance (the one
that
was opened from Button1 on Form 1).
Furthermore, I may also want to update somthing in Form1 from within the
User control in Form2, but those "existing" controls appear to be
inaccessible as well.

Is there a way to make all "open instance" form data Public ? If so, is
this a bad idea ?

Also, let me say thanks again for all your input into this newsgroup.
You
are a tremendous resource !

Rob

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@g4g2000hsf.googlegr oups.com...
On May 25, 11:43 am, "Rob" <r...@yahoo.comwrote:
Thanks Seth,
>But I am getting an error in in the Set statement...
> Me.TabControl1.SelectedTab = TabPage
>It says "TabPag is a type and cannot be used as an expression"
>Rob
>"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>>news:11**********************@q66g2000hsg.google groups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
>There are several tabs on TabControl1
>Now you open a new Form2 that contains a User Control
>How can you determine the Selected tab in Form1 from the User
Control
in
Form2 ?
>Hope this is clear enough...
>Thanks !
IMO the proper way would be to expose the selected tab via a
property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe
> Me.TabControl1.SelectedTab = TabPage
That should say Me.TabControl1.SelectedTab = value
Like I said - I typed that in the message to just give you an idea of
how it should go - and not necessarily a working solution.
Thanks,
Seth Rowe- Hide quoted text -

- Show quoted text -

Forms in Access are what called singleton forms. Only one instance can
be open at a time - therefore you can always use the
Forms.MyForm.MyField. In .Net you can create any number of the same
form which means you can't use the Forms.MyForm.MyField syntax. Here's
why:

Consider this code which creates 25 Form1's and assigns a value to
it's text property

For i As Integer = 0 to 24
Dim f As New Form1()
f.Text = String.Format("Form Number {0}", i.ToString())
f.Show()
Next i

Now that we have 25 forms, which one will return it's text value when
we do this (if it was a valid statement)?

MessageBox.Show(Forms.Form1.Text)

You see, there is no way for the framework to know which form you
wanted to grab, so this statement is not included in the framework.

In your case you have two options, one is to create an instance of
Form1 and keep a reference to it by using a property or variable. For
example:

Public Class Form2

Dim MyTrackedForm1 As Form1

Public Sub New()
InitializeComponent()
MyTrackedForm1 = New Form1()
' Set other properties for MyTrackedForm1
End Sub

Public Sub Button_Click(.....)
MessageBox.Show(MyTrackedForm1.Text)
End Sub

End Class

Now you create an instance of Form1 in the beginning of an instance of
Form2, and store a reference to it in the MyTrackedForm1 variable.
Then any time you need to access one of it's properties/methods you
just use the MyTrackedForm1 variable to access the instance you
created. This approach works great when you only have one form
accessing an instance of Form1, but if you have multiple different
forms (like a form2, form3, form4, etc) all trying to access a certain
Form1's members, you would have to use a global variable (which I
dislike) and remember to only use it.

This global variable also doesn't stop Form1 from being instantiated
multiple times, but this next approach does. What we are going to do
is use the Singleton design pattern and make it so that only one Form1
can exist at a time, and all requests will go to that certain Form1.
If you want to know more about Singleton I would recommend you do some
searching on google or wikipedia - others have done a better job
explaining it's strength and weaknesses better than I could. Anyways,
basically we want to make it so that noone can instantiate our form
directely - we do this with a private (or protected) constructor like
so:

Private Sub New()
InitializeComponent()
End Sub

Now that no outside objects can create Form1 we need to give them a
way to access Form1, we do this with a variable/property pair. And
since we want this property to be visible to everyone without needed
Form1 to be instantiated, we use a shared property and variable.

Private _Instance As Form1
Public Property Instance As Form1
Get
If _Instance Is Nothing Then
_Instance = New Form1()
' Set other properties if necessary
End If
Return _Instance
End Get
End Property

If you look the property checks to see whether or not Form1 is already
created in the get accessor. If it is, it return the current Form1, if
not however it creates a new instance of Form1 and return's it.

Now to use this all you need to do is use this Singleton pattern on
any form you want to be a single instance form. Then just expose any
necessary fields by use of properties. From what I can tell you want
to have a singleton instance of Form2 and expose the text property of
it's textbox1. This basic form design should be:

Public Class Form2

Private _Instance As Form2
Public Readonly Property Instance As Form2
Get
If _Instance Is Nothing Then _Instance = New Form2()
Return _Instance
End Get
End Property

Private Sub New()
InitializeComponent()
End Sub

Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set (ByVal value As String)
TextBox1.Text = value
End Set
End Property

End Class

Then in the Button1 click event in Form1 you can just do this:

Form2.Instance.Controls.Add(New UserControl1)
Form2.Instance.ShowDialog(Me)

And in the ButtonU click event in the UserControl you can do this:

Form2.Instance.TextBoxText = "My new value"

And you should be set!
Thanks,

Seth Rowe

May 26 '07 #10
On May 26, 9:36 am, "Rob" <r...@yahoo.comwrote:
Seth,

Thanks so much again... I am still trying to digest it... but...

Regarding... below...

Should the following be "Public" ? Otherwise I am getting object reference
errors...

Private Sub New()
InitializeComponent()
End Sub

Thanks,
Rob

------------------------------

Public Class Form2

Private _Instance As Form2
Public Readonly Property Instance As Form2
Get
If _Instance Is Nothing Then _Instance = New Form2()
Return _Instance
End Get
End Property

Private Sub New()
InitializeComponent()
End Sub

Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set (ByVal value As String)
TextBox1.Text = value
End Set
End Property

End Class

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@q75g2000hsh.googlegr oups.com...
On May 26, 7:34 am, "Rob" <r...@yahoo.comwrote:
Seth,
I think I am missing a really "big" concept regarding scope of variables
and
instances...
In your example, are you not creating a "new" instance of Form1 when you
call the sub in Form 2 ?
Therefore, aren't you always going to get the default value of whatever
control's property you are returning ?
I want to know the selected tab page of a form whose instance already
exists....
It appears that I am constantly boxing myself into a corner (I can't get
there from here)....
(In MS Access, as long as the form was open, you could issue
Forms.frmWhatever.contol and alway get your answer.)
For example...
Let's say you begin in Form1
There is a Button1 on Form1
Button1 opens an instance of Form2
Button1 also adds a User control to Form2 (This User Control contains a
ButtonU)
On Form2 there is a TextBox1 and a Panel containing the User Control
This is where I run into the dead-end...
From the ButtonU in the user control I want to update the Textbox1 on
Form2,
but I cannot appear to "get back" to update that control (i.e.,
frmForm2.TextBox1.Text is not declared) Again, I do not want to update
a
"new" instance of Form2, I want to update an existing instance (the one
that
was opened from Button1 on Form 1).
Furthermore, I may also want to update somthing in Form1 from within the
User control in Form2, but those "existing" controls appear to be
inaccessible as well.
Is there a way to make all "open instance" form data Public ? If so, is
this a bad idea ?
Also, let me say thanks again for all your input into this newsgroup.
You
are a tremendous resource !
Rob
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>news:11**********************@g4g2000hsf.googlegr oups.com...
On May 25, 11:43 am, "Rob" <r...@yahoo.comwrote:
Thanks Seth,
But I am getting an error in in the Set statement...
Me.TabControl1.SelectedTab = TabPage
It says "TabPag is a type and cannot be used as an expression"
Rob
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>news:11**********************@q66g2000hsg.googleg roups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
There are several tabs on TabControl1
Now you open a new Form2 that contains a User Control
How can you determine the Selected tab in Form1 from the User
Control
in
Form2 ?
Hope this is clear enough...
Thanks !
IMO the proper way would be to expose the selected tab via a
property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe
Me.TabControl1.SelectedTab = TabPage
That should say Me.TabControl1.SelectedTab = value
Like I said - I typed that in the message to just give you an idea of
how it should go - and not necessarily a working solution.
Thanks,
Seth Rowe- Hide quoted text -
- Show quoted text -
Forms in Access are what called singleton forms. Only one instance can
be open at a time - therefore you can always use the
Forms.MyForm.MyField. In .Net you can create any number of the same
form which means you can't use the Forms.MyForm.MyField syntax. Here's
why:
Consider this code which creates 25 Form1's and assigns a value to
it's text property
For i As Integer = 0 to 24
Dim f As New Form1()
f.Text = String.Format("Form Number {0}", i.ToString())
f.Show()
Next i
Now that we have 25 forms, which one will return it's text value when
we do this (if it was a valid statement)?
MessageBox.Show(Forms.Form1.Text)
You see, there is no way for the framework to know which form you
wanted to grab, so this statement is not included in the framework.
In your case you have two options, one is to create an instance of
Form1 and keep a reference to it by using a property or variable. For
example:
Public Class Form2
Dim MyTrackedForm1 As Form1
Public Sub New()
InitializeComponent()
MyTrackedForm1 = New Form1()
' Set other properties for MyTrackedForm1
End Sub
Public Sub Button_Click(.....)
MessageBox.Show(MyTrackedForm1.Text)
End Sub
End Class
Now you create an instance of Form1 in the beginning of an instance of
Form2, and store a reference to it in the MyTrackedForm1 variable.
Then any time you need to access one of it's properties/methods you
just use the MyTrackedForm1 variable to access the instance you
created. This approach works great when you only have one form
accessing an instance of Form1, but if you have multiple different
forms (like a form2, form3, form4, etc) all trying to access a certain
Form1's members, you would have to use a global variable (which I
dislike) and remember to only use it.
This global variable also doesn't stop Form1 from being instantiated
multiple times, but this next approach does. What we are going to do
is use the Singleton design pattern and make it so that only one Form1
can exist at a time, and all requests will go to that certain Form1.
If you want to know more about Singleton I would recommend you do some
searching on google or wikipedia - others have done a better job
explaining it's strength and weaknesses better than I could. Anyways,
basically we want to make it so that noone can instantiate our form
directely - we do this with a private (or protected) constructor like
so:
Private Sub New()
InitializeComponent()
End Sub
Now that no outside objects can create Form1 we need to give them a
way to access Form1, we do this with a variable/property pair. And
since we want this property to be visible to everyone without needed
Form1 to be instantiated, we use a shared property and variable.
Private _Instance As Form1
Public Property Instance As Form1
Get
If _Instance Is Nothing Then
_Instance = New Form1()
' Set other properties if necessary
End If
Return _Instance
End Get
End Property
If you look the property checks to see whether or not Form1 is already
created in the get accessor. If it is, it return the current Form1, if
not however it creates a new instance of Form1 and return's it.
Now to use this all you need to do is use this Singleton pattern on
any form you want to be a single instance form. Then just expose any
necessary fields by use of properties. From what I can tell you want
to have a singleton instance of Form2 and expose the text property of
it's textbox1. This basic form design should be:
Public Class Form2
Private _Instance As Form2
Public Readonly Property Instance As Form2
Get
If _Instance Is Nothing Then _Instance = New Form2()
Return _Instance
End Get
End Property
Private Sub New()
InitializeComponent()
End Sub
Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set (ByVal value As String)
TextBox1.Text = value
End Set
End Property
End Class
Then in the Button1 click event in Form1 you can just do this:
Form2.Instance.Controls.Add(New UserControl1)
Form2.Instance.ShowDialog(Me)
And in the ButtonU click event in the UserControl you can do this:
Form2.Instance.TextBoxText = "My new value"
And you should be set!
Thanks,
Seth Rowe- Hide quoted text -

- Show quoted text -
No - if it's public that would allow outside object to create your
form. The point of the singleton pattern is to allow only the Instance
property to create the form.

What "object reference errors" are you getting? I'm guessing you are
trying to instantiate Form2? If so you are missing the point of the
singleton pattern. Instead of instantiating form2 (Dim f as new
Form2()), you should be accessing it through the instance property
(Form2.Instance.Text = Hello World!)

Thanks,

Seth Rowe

May 26 '07 #11
Rob
Hi Seth,

I made some small changes that appear to make things "work".... I hope
these changes are OK...

Thank you once again for exposing me to a different way to accomplish things
!

You are amazing !

Rob
Public Class JobOpSelection
Private Shared FInstance As JobOpSelection = Nothing
Private Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub

Public Shared ReadOnly Property Instance()
Get
If (FInstance Is Nothing) Then FInstance = New JobOpSelection()
Return FInstance
End Get
End Property

Public Property ContentTextBox1() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal value As String)
Me.TextBox1.Text = value
End Set
End Property

Public Property CntJobOp() As String
Get
Return Me.txtCntJobOp.Text
End Get
Set(ByVal value As String)
Me.txtCntJobOp.Text = value
End Set
End Property

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On May 26, 9:36 am, "Rob" <r...@yahoo.comwrote:
>Seth,

Thanks so much again... I am still trying to digest it... but...

Regarding... below...

Should the following be "Public" ? Otherwise I am getting object
reference
errors...

Private Sub New()
InitializeComponent()
End Sub

Thanks,
Rob

------------------------------

Public Class Form2

Private _Instance As Form2
Public Readonly Property Instance As Form2
Get
If _Instance Is Nothing Then _Instance = New Form2()
Return _Instance
End Get
End Property

Private Sub New()
InitializeComponent()
End Sub

Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set (ByVal value As String)
TextBox1.Text = value
End Set
End Property

End Class

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@q75g2000hsh.googleg roups.com...
On May 26, 7:34 am, "Rob" <r...@yahoo.comwrote:
Seth,
>I think I am missing a really "big" concept regarding scope of
variables
and
instances...
>In your example, are you not creating a "new" instance of Form1 when
you
call the sub in Form 2 ?
Therefore, aren't you always going to get the default value of
whatever
control's property you are returning ?
>I want to know the selected tab page of a form whose instance already
exists....
>It appears that I am constantly boxing myself into a corner (I can't
get
there from here)....
(In MS Access, as long as the form was open, you could issue
Forms.frmWhatever.contol and alway get your answer.)
>For example...
Let's say you begin in Form1
There is a Button1 on Form1
Button1 opens an instance of Form2
Button1 also adds a User control to Form2 (This User Control contains
a
ButtonU)
On Form2 there is a TextBox1 and a Panel containing the User Control
This is where I run into the dead-end...
From the ButtonU in the user control I want to update the Textbox1 on
Form2,
but I cannot appear to "get back" to update that control (i.e.,
frmForm2.TextBox1.Text is not declared) Again, I do not want to
update
a
"new" instance of Form2, I want to update an existing instance (the
one
that
was opened from Button1 on Form 1).
Furthermore, I may also want to update somthing in Form1 from within
the
User control in Form2, but those "existing" controls appear to be
inaccessible as well.
>Is there a way to make all "open instance" form data Public ? If so,
is
this a bad idea ?
>Also, let me say thanks again for all your input into this newsgroup.
You
are a tremendous resource !
>Rob
>"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>>news:11**********************@g4g2000hsf.googleg roups.com...
On May 25, 11:43 am, "Rob" <r...@yahoo.comwrote:
Thanks Seth,
>But I am getting an error in in the Set statement...
> Me.TabControl1.SelectedTab = TabPage
>It says "TabPag is a type and cannot be used as an expression"
>Rob
>"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>>news:11**********************@q66g2000hsg.google groups.com...
On May 25, 10:45 am, "Rob" <r...@yahoo.comwrote:
Let's say you open Form1 that contains TabControl1
>There are several tabs on TabControl1
>Now you open a new Form2 that contains a User Control
>How can you determine the Selected tab in Form1 from the User
Control
in
Form2 ?
>Hope this is clear enough...
>Thanks !
IMO the proper way would be to expose the selected tab via a
property
in Form1.
' Typed in message
Public Class Form1
Public Property SelectedTab As TabPage
Get
Return Me.TabControl1.SelectedTab
End Get
Set (ByVal value As TabPage)
Me.TabControl1.SelectedTab = TabPage
End Set
End Property
End Class
Public Class Form2
Public Sub MyMethod()
Dim f As New Form1()
f.Show()
Dim tp As TabPage = f.SelectedTab
MessageBox.Show(tp.Name)
End Sub
End Class
Thanks,
Seth Rowe
> Me.TabControl1.SelectedTab = TabPage
That should say Me.TabControl1.SelectedTab = value
Like I said - I typed that in the message to just give you an idea
of
how it should go - and not necessarily a working solution.
Thanks,
Seth Rowe- Hide quoted text -
>- Show quoted text -
Forms in Access are what called singleton forms. Only one instance can
be open at a time - therefore you can always use the
Forms.MyForm.MyField. In .Net you can create any number of the same
form which means you can't use the Forms.MyForm.MyField syntax. Here's
why:
Consider this code which creates 25 Form1's and assigns a value to
it's text property
For i As Integer = 0 to 24
Dim f As New Form1()
f.Text = String.Format("Form Number {0}", i.ToString())
f.Show()
Next i
Now that we have 25 forms, which one will return it's text value when
we do this (if it was a valid statement)?
MessageBox.Show(Forms.Form1.Text)
You see, there is no way for the framework to know which form you
wanted to grab, so this statement is not included in the framework.
In your case you have two options, one is to create an instance of
Form1 and keep a reference to it by using a property or variable. For
example:
Public Class Form2
Dim MyTrackedForm1 As Form1
Public Sub New()
InitializeComponent()
MyTrackedForm1 = New Form1()
' Set other properties for MyTrackedForm1
End Sub
Public Sub Button_Click(.....)
MessageBox.Show(MyTrackedForm1.Text)
End Sub
End Class
Now you create an instance of Form1 in the beginning of an instance of
Form2, and store a reference to it in the MyTrackedForm1 variable.
Then any time you need to access one of it's properties/methods you
just use the MyTrackedForm1 variable to access the instance you
created. This approach works great when you only have one form
accessing an instance of Form1, but if you have multiple different
forms (like a form2, form3, form4, etc) all trying to access a certain
Form1's members, you would have to use a global variable (which I
dislike) and remember to only use it.
This global variable also doesn't stop Form1 from being instantiated
multiple times, but this next approach does. What we are going to do
is use the Singleton design pattern and make it so that only one Form1
can exist at a time, and all requests will go to that certain Form1.
If you want to know more about Singleton I would recommend you do some
searching on google or wikipedia - others have done a better job
explaining it's strength and weaknesses better than I could. Anyways,
basically we want to make it so that noone can instantiate our form
directely - we do this with a private (or protected) constructor like
so:
Private Sub New()
InitializeComponent()
End Sub
Now that no outside objects can create Form1 we need to give them a
way to access Form1, we do this with a variable/property pair. And
since we want this property to be visible to everyone without needed
Form1 to be instantiated, we use a shared property and variable.
Private _Instance As Form1
Public Property Instance As Form1
Get
If _Instance Is Nothing Then
_Instance = New Form1()
' Set other properties if necessary
End If
Return _Instance
End Get
End Property
If you look the property checks to see whether or not Form1 is already
created in the get accessor. If it is, it return the current Form1, if
not however it creates a new instance of Form1 and return's it.
Now to use this all you need to do is use this Singleton pattern on
any form you want to be a single instance form. Then just expose any
necessary fields by use of properties. From what I can tell you want
to have a singleton instance of Form2 and expose the text property of
it's textbox1. This basic form design should be:
Public Class Form2
Private _Instance As Form2
Public Readonly Property Instance As Form2
Get
If _Instance Is Nothing Then _Instance = New Form2()
Return _Instance
End Get
End Property
Private Sub New()
InitializeComponent()
End Sub
Public Property TextBoxText As String
Get
Return TextBox1.Text
End Get
Set (ByVal value As String)
TextBox1.Text = value
End Set
End Property
End Class
Then in the Button1 click event in Form1 you can just do this:
Form2.Instance.Controls.Add(New UserControl1)
Form2.Instance.ShowDialog(Me)
And in the ButtonU click event in the UserControl you can do this:
Form2.Instance.TextBoxText = "My new value"
And you should be set!
Thanks,
Seth Rowe- Hide quoted text -

- Show quoted text -

No - if it's public that would allow outside object to create your
form. The point of the singleton pattern is to allow only the Instance
property to create the form.

What "object reference errors" are you getting? I'm guessing you are
trying to instantiate Form2? If so you are missing the point of the
singleton pattern. Instead of instantiating form2 (Dim f as new
Form2()), you should be accessing it through the instance property
(Form2.Instance.Text = Hello World!)

Thanks,

Seth Rowe

May 27 '07 #12
Glad I helped! What you have looks good to me - though I didn't look
at it too close. Below is a link to one of Jon Skeet's articles that
talks about a better implementation of the singleton pattern. The
basic pattern I showed you above should be fine for your project, but
for multithreaded apps I seem to always use the pattern Jon shows in
the link.

http://www.yoda.arachsys.com/csharp/singleton.html

Thanks,

Seth Rowe

May 27 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Timo J | last post by:
Hi - Im sitting and trying to understand this OOP - and need to create a class wich can do the following.. ShowBlocks() - Displays the data wich is inside it - If empty creates a form wich sends...
1
by: Kenshin | last post by:
Hey! I have another script where i pull all the information from the database and I want to send it to the person. What they do is they enter in their email, and if the email matches, than it will...
2
by: Jean | last post by:
I have two form and I want to display in the first form some of the info from the second one. Here is how I tried to do it: <<<<ok button of form2>>>>> Dim Myform1 As New Form1...
3
by: Tom Turner | last post by:
Here's the background on my situation. The question follows --- We have 600 units of mail going from our business to various Post Offices every morning. Every unit is accompanied by a paper...
3
by: Frankie | last post by:
Hi, I am working on a form in which in a certain field a number of an employee can be entered. After entering this number I would like to show in the same form (real time) the name etc. of this...
7
by: Terry | last post by:
I have a Mainform with a Statusbar. When opening another form or doing some processing I want to display info in the Statusbar of the Mainform. I have read a lot of articles on this & have come up...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
3
by: corear | last post by:
I have a new case tracking system. The cases table has these fields: CaseID (Primary Key) StatusID (looked up in another table - Open, Pending, or Closed) Status Comment (memo field) Date&Time...
8
by: Neekos | last post by:
I have a form called frmPaxInfo, and on this form is a subform called subfrmPaxInfo. This subform (which is filtered by cabin) contains the passengers name along with all charges associated with each...
3
by: Jeff | last post by:
I know how to use php/mysql, but the html aspect of what Im trying to do (if its even possible this way) is what Im unsure of. I want to have a button that a user can hit to valid mysql info...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.