refresh one form from another

Pork

New member
Joined
Nov 24, 2005
Messages
2
Programming Experience
Beginner
Hello:

I'm new here and I know there are various flavors of this, but what I am looking for is the simplest way of doing this...I'll describe...I have 2 forms...on the main form, I have a list box that displays info from a text file. The second form is where the records get added to the text file. When I click the save button on the second form, I need it to refresh the list box on the first form. All the code to display the info in the list box is there and working great...I just need a refresh.....can I do a mainform.lstbox.refresh() in the click event of the second form??? Or...do I have to pass a value from the second form to the main form...and if so...what's the best way to do this.

Thanks for any help you can offer,

Regards,

Alex
 
The "best" way would be a matter of app architecture and even some preference.
Every way is going to require the second form to have a reference to the mainform.
I am going to assume that the second form is opened by the mainform. In the mainform, create a procedure that refreshes the listBox, declare it Public. In the secondForm, create an overloaded New method that accepts the MainForm instance as a parameter and assigns it to a private variable. Then to refresh the listbox in the secondForm, use the private variable referencing the MainForm to call the procedure that refreshes the listbox. In code:
VB.NET:
'MainForm:
Public Sub RefreshListBox()
    'code to refresh listbox...
End Sub

'opening second form:
Dim frm2 as New SecondForm(Me)
frms2.Show  'or ShowDialog, whatever

'in Second form:
Private frmMain as MainForm
Public Sub New(ByVal main as MainForm)
    MyClass.New()
    frmMain = main
End Sub

'to refresh mainForm's listBox (this code is located in the second Form):
frmMain.RefreshListBox()
As I said, the best way will depend. Here is a great article that may help you understand other ways to handle this problem: Multiple Forms in VB.NET. Part 4 - Accessing Controls and Data ...
 
Back
Top