Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I make a form load fresh? 1

Status
Not open for further replies.

RebLazer

Programmer
Jun 7, 2002
438
0
0
US
I have two WinForms: MainMenu and Edit_Select. Edit_select is created at the class level of Edit_Select as follows:
Public Shared es_form As New Edit_select

The sequence of events goes like this: When the app is started, MainMenu is loaded. MainMenu form allows the user to enter search criteria. After the user does that and clicks the submit button on MainMenu, the next form is displayed with this line:
Edit_select.es_form.Show()

"edit_select_load" (handles mybase.load) is then executed.

Good.

Here's my problem. The first time the app runs, it works fine. But subsequent times that the user uses the MainMenu, after
Edit_select.es_form.Show() is executed,
"edit_select_load" (handles mybase.load) is not executed at all.

The result is that the second search (and beyond) all use the initial search criteria.

How do I get "edit_select_load" (handles mybase.load) to execute the second time around, as it does with the initial search? What can I do to trick the SHOW() method into calling "edit_select_load" (handles mybase.load) with each search - not just the first one?

Thanks so much - have a good weekend!
Lazer
 
You should create a new Edit_Select form every time it is needed. There's really no reason to only have a single instance of it. I'd get rid of the shared [tt]es_form[/tt] entirely. Then when the user clicks your submit button you would:
[tt]dim frm as New Edit_select
frm.Show()[/tt]

 
Rosenk,

Thanks! And how do I destroy the older instances? ...or I guess they'll just get GC'ed when they go out of scope...

Lazer
 
They should get garbage collected if you don't have any further references to them. If you want to free up non-memory resources used by the form you can call [tt]Dispose()[/tt] on it manually, but it shouldn't really be necessary.
 
Rosenk,

So if I replace [tt]Edit_select.es_form.Show()[/tt] with
[tt]dim es_form as New Edit_select
es_form.Show()[/tt]

in the MainMenu, what's the best way to refer to it in Class Edit_select? (Currently I have that
[tt]Public Shared es_form As New Edit_select[/tt]
at the top of edit_select - what should I replace that with?)

Thanks very much,
Lazer
 
What are you using that for?

I'm tempted to say that you should just get rid of that entirely, but I have the feeling that you're trying to use a reference to that form elsewhere in your application.

If you're looking to use it in the MainMenu form, I would make [tt]es_form[/tt] be an instance variable, and just refer to that.
 
Yes, I am referencing it elsewhere in my app. I need to be able to go back and read the contents of a couple textboxes in the instance.

When you say "an instance variable" - do mean I should write a constructor for Edit_Select with es_form an instance variable? Please explain.

Thanks,
Lazer
 
You should always call dispose on the form when you get done using it. Calling dispose does not cause the GC to collect it anyways, the system decides that. If you don't call dispose and try to instance the form again to soon and with the same name you might get strange results.
 
Well, if you're sure that there's only ever going to be one at a time, you could replace your [tt]es_form[/tt] class variable with:

[tt]Public Shared es_form As Edit_select[/tt]

Then you would set es_form in the [tt]New[/tt] method of your [tt]Edit_select[/tt] class, like:
[tt]Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
es_form = Me
End Sub[/tt]

That would make [tt]Edit_select.es_form[/tt] always refer to the most recently created [tt]Edit_select[/tt] form instance. (Keep in mind that until one is created, [tt]Edit_selet.es_form[/tt] will be [tt]Nothing[/tt]

That's one way to do what I think you're looking to do, at least.
 
Hi RebLazer,

I guess I understood what you are trying to do.
What rosenk is suggesting is correct. This is how I did........

I created a separate Edit form. Also I created a data class, just for carrying data from one form to other.

In the form from where you will pass the data ,I declared an object to data class

Public Shared editData As ACADDataClass = New ACADDataClass ' Data Class

IN the click event I assign the data that I need to carry to edit form as below
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
editData.CenterID = strCenterID
editData.BldgCode = intBldgCode
editData.AreaID = Me.ACADgrid.Item(ACADgrid.CurrentRowIndex, 0)
editData.Floor = Me.ACADgrid.Item(ACADgrid.CurrentRowIndex, 1)
editData.RowID = Me.ACADgrid.CurrentRowIndex
Dim editForm As New frmEditACAD
editForm.Show()
End Sub

And now in the edit form, where user makes changes and saves the data, I need to refresh the parent form. My parent form uses the load data button event to refresh the data. So i used the same click event of the parent form by calling it from the edit form.

So I declared an object for the load data button.
Public Shared aBtn As Button = New Button 'To perform Click event of Load from frmEdit

And in the edit form I called the click event

frmSUComp.aBtn.PerformClick()

frmSUComp is the parent form.

Note: When I say parent form, its not MDI form, its just the calling form to edit form.

I hope this might help you

 
Thanks, Vinidel!

Rosenk, that was exactly what I needed - it works (finally!!!) Thanks so much [star]!!!

But what does [tt]es_form = Me[/tt] do exactly?

Lazer
 
[tt]es_form = Me[/tt] assigns the current instance of Edit_select (the one being created), [tt]Me[/tt], to the class ([tt]Shared[/tt]) variable [tt]es_form[/tt].

It being a class variable means that:
1) All instances share it.
2) It can be accessed without an instance (like [tt]Edit_select.es_form[/tt])

Hope that explains it well enough.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top