0

Say I have this:

Dim Editor As frmEditor
Editor.Text = "New Form"
Editor.Controls.Add(richTextBox)

Then later in a sub routine, I do this:

Editor = New frmEditor

Is it possible to save the controls/data of the previously declared Editor for future use? The one which was declared not the one which was instantiated using the New keyword.

Ryklon Zen
  • 173
  • 1
  • 3
  • 19
  • Why not, just not use the same variable? – paparazzo Mar 27 '13 at 20:17
  • It doesn't work. If I'm not mistaken. `Editor = New frmEditor` creates a new object which is different from the previous one, removing all the controls and data? Please correct me if I'm wrong about this. – Ryklon Zen Mar 27 '13 at 20:55
  • 1
    Hi there, could you do something like `Dim oldEditor as frmEditor` then before you do `Editor = New frmEditor` do `oldEditor = Editor`? `oldEditor` should then have a reference to the previous object – nkvu Mar 27 '13 at 21:22
  • No problem - I've also moved the comment to the answer section in case anyone else has a similar question – nkvu Mar 27 '13 at 22:08
  • Editor is reusing the same variable. What part of NOT use the same variable was not clear? – paparazzo Mar 28 '13 at 01:12

2 Answers2

1

[nkvu - moved from comments to answer in case anyone has a similar query....]

Could you do something like:

Dim oldEditor as frmEditor 

Then before you do:

Editor = New frmEditor 

Do this:

oldEditor = Editor

oldEditor should then have a reference to the previous object

nkvu
  • 5,551
  • 2
  • 16
  • 12
1
Dim Editor As frmEditor

... does not create an editor, it declares only an empty variable, therefore ...

Dim Editor As frmEditor
Editor.Text = "New Form"

... will fail!

You must create a form with New:

Dim Editor As frmEditor
Editor = New frmEditor()
Editor.Text = "New Form"

Or

Dim Editor As frmEditor = New frmEditor()
Editor.Text = "New Form"

To answer your question:

Assign the "old" editor to another variable

Dim oldEditor As Editor = frmEditor
frmEditor = New frmEditor()
frmEditor.RtfText = oldEditor.RtfText

Also make a public property that allows you to access what you need to access from outside of the form

Public Property RtfText() As String
    Get
        Return richTextBox.Rtf
    End Get
    Set(ByVal value As String)
        richTextBox.Rtf = value
    End Set
End Property
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188