How to get a form's property value from a class?

D

Dean Slindee

I have a form whose Property value I need to get at from a class (contained
in another project, same solution).

Here is the form's property:

Private booIsInsert As Boolean = False

Public Property IsInsert() As Boolean

Get

Return booIsInsert

End Get

Set(ByVal Value As Boolean)

booIsInsert = Value

End Set

End Property



Here is the class's function that needs the property value:

Public Shared Sub FormDataChanged(ByVal sender As System.Object)

Dim frmParent As Form

frmParent = sender.ParentForm

If Not frmParent.IsInsert Then 'SYNTAX ERROR line

Dim ctl As Control

ctl = CType(frmParent.Controls.Item("btnUpdate"), Control)

Call FrameButtonPaintFlat(frmParent, CType(ctl, Control)) 'green
frame around button

End If



Syntax error message: "IsInsert is not a member of
System.Windows.Forms.Form"

I understand the error, but is there *any* way to reference the frmParent
instance and get the IsInsert property value?

Or some other approach?



Thanks in advance.

Dean S
 
R

RobinS

You probably need to add a reference to
the project containing the class in which
you want to access that form's property.
(Did you get that?)

To be clearer (I hope), an example:

1) Project 1, has Form1 with property IsInsert.
2) Project 2, has Class1 that wants to access
Form1's property IsInsert.

Open Project2's properties and click on References.
THen click on Add. Choose the Projects tab, and
select Project1.

Then on the bottom of the screen, where it has
Imported Namespaces, find Project1 in the list
and check it (you'll have to click it twice).
Not Project1.My or Project1.My.Resources,
just Project1.

*Now* you should be able to access Form1.IsInsert.

Good luck.
Robin S.
 
M

Michael C

Dean Slindee said:
I have a form whose Property value I need to get at from a class (contained
in another project, same solution).

I'd suggest you're doing something wrong. In all my years of programming I
don't think I've ever done this or needed to do this. The form should pass
whatever information it needs to the class so the class can perform its
task. Can you explain more what you're doing?

If you really need to do this I'm presuming Robin's answer won't help
because the project with the form already has a reference to the project
with the class, so you can reference the other way. But what you can do is
define an interface in the project with the class and have the form
implement that interface.

Michael
 
C

Cor Ligthert [MVP]

Dean,

I have almost the same idea as Michael, even more, it seems that you want to
give the property at a certain moment to the class. I think that than a
method is more sufficient because than the given data can be processed.

Cor
 
R

RobinS

If you want project1 to reference something in project2,
you must add a reference to project1, even if project2
has a reference to project1. It's project-specific, and
project1 doesn't know about project2's reference to project1.
Did I use the word project in there enough times? ;-)

Robin S.
--------------------
 
M

Michael C

RobinS said:
If you want project1 to reference something in project2,
you must add a reference to project1, even if project2
has a reference to project1. It's project-specific, and
project1 doesn't know about project2's reference to project1.
Did I use the word project in there enough times? ;-)

For a start you can't add a reference to an exe project. Even if both
projects were dlls I doubt you could add a circular reference. Even if you
could this would be bad practice. Which would you compile first?

Michael
 
R

RobinS

I assumed he was talking about having multiple project
inside one solution. If I misunderstood, then never mind. :)

Robin S.
----------------------------------
 
M

Michael C

RobinS said:
I assumed he was talking about having multiple project
inside one solution. If I misunderstood, then never mind. :)

He was. For projects in the same solution I doubt you can add a circular
reference.

Michael
 
R

RobinS

Michael C said:
He was. For projects in the same solution I doubt you can add a circular
reference.

Michael

Yes, you're absolutely right. I must have had some temporary
insanity going on there, to think that would work. My mistake.
Thanks for clarifying.

Robin S.
 

Ask a Question

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

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

Ask a Question

Top