How to create a Public Form property

  • Thread starter Thread starter Sarah Smith
  • Start date Start date
S

Sarah Smith

Hello,

I've been trying to create a public property on one form that is
"Setable" from another form.

I have a Sub Main() which creates 2 public instances of the two forms
that make up the project and then loads the first main form using
ShowDialog.

The menu on this form lets you display the second form.

From the second form, I'd like to be able to set a property I've added
to the first form and then close the second form.

(This is a database detail/list view.)

I have created the property in the first form like this:

Public Property intRecordNumber() As Integer

Get
intRecordNumber = intSelectedRow
End Get

Set(ByVal Value As Integer)
nRecord = Value
Call DisplayRecord()
End Set

End Property

VS complains that I am making a reference to a "non-shared member" but
both frmMain and frmListView were declared publicly in the code module
that contains Sub Main().

Can anyone help??

SS.
 
intRecordNumber is the property I've added to a form - FrmMain - and
I'm trying to make publicly available to other forms.

Like this:

Public Property intRecordNumber() As Integer


nRecord is a form level variable in the form with the property which
should be given a value in the property Set method:

Set(ByVal Value As Integer)
nRecord = Value
Call DisplayRecord()
End Set

Here's the code again:

Public Property intRecordNumber() As Integer

Get
intRecordNumber = intSelectedRow
End Get

Set(ByVal Value As Integer)
nRecord = Value
Call DisplayRecord()
End Set

End Property

I'm used to VB6, so I'm not sure about "Shared" properties or
variables ...

SS.
 
These look fine. Can you show the Main() code where you create the Form
instances and try to set the properties?
 
Sarah:

This compiles fine for me:

Private nRecord As Integer
Private intSelectedRow As Integer

Public Property intRecordNumber() As Integer
Get
Return intSelectedRow
End Get
Set(ByVal Value As Integer)
nRecord = Value
Call DisplayRecord()
End Set
End Property
Private Sub DisplayRecord()
'Do something in here which sets the value of intSelectedRow
End Sub

However, you may want to opt for something a little more traditional..

Private nRecord As Integer
Private intSelectedRow As Integer

Public Property RecordNumber() As Integer
Get
Return nRecord
End Get
Set(ByVal Value As Integer)
nRecord = Value
End Set
End Property

Syntactically everything looks good unless you have Private Shared Property
or something like that...
 
These look fine. Can you show the Main() code where you create the Form
instances and try to set the properties?

Hi,

Thanks for looking at this.

I have a code module with Sub Main() and this is the startup object
for the project:

Public FrmMain As New FrmMain
Public frmList As New frmListView


Public Sub Main()

FrmMain.ShowDialog() 'show the main form


End Sub


FrmMain has a menu, which displays the frmListView form (with a grid
attached to a DataView object. (This works fine).

I want to be able to select a row in the grid, then tap a button
'select' to close the List form and return to the detailed form
(FrmMain) - and go to the selected record.

I thought if I created a public property, I could 'Set' the record
number and call a sub routine to locate and display the record.

Something like:

FrmMain.intSelectedRow = intRow
Me.Close() 'close the list view form


SS.
 
Back
Top