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 ! 12 2285
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
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
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
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
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
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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |