[RESOLVED] [2005] How to call a public fn that's in one form, from another?
Hi,
Hoping you can help...
1) I have a form, frmDisplay, that displays data in a ListView control.
2) When a user selects a row and clicks btnEdit, a little editng form, frmEdit, pops up with a few combo boxes to allow editing.
3) In frmEdit's btnOkay_Click event, I can save the data to the data source, BUT...
Question
How can I refresh the ListView from frmEdit's btnOkay_Click?
BTW I have a Sub RefreshList in frmDisplay that refreshes the ListVew. That's not the problem. The problem is how to call that code from the other form! Even after making it Public, I couldn't access it, even using frmDisplay.RefreshList().
My workaround for now is to have a btnRefresh on frmDisplay and update the list manually, but that's not a suitable solution.
Your help is much appreciated!
Gregg
Re: [2005] How to call a public fn that's in one form, from another?
If you want to pass data to an object then how do you do it? You set a property or pass a parameter to a method, which may or may not be a constructor.
If you want to retrieve data from an object then how do you do it? You get a property or get the return value of a function.
The fact that your object is a form does not change the above in any way whatsoever. If you're creating an editing form then you pass data to it, call ShowDialog, then retrieve data from it. It's that simple.
Read the "Forms in VB.NET" tutorila in my signature for more information.
Re: [2005] How to call a public fn that's in one form, from another?
Great! I followed the tutorial you mentioned, used a Public Shared list in frmEdit and everything works fine.
I also added a check to make sure that the Okay button was clicked, not Cancel:
VB Code:
Private Sub Edit(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnEdit.Click
Dim frmEditStaticAirPao As New frmEditStaticAirPAO(thisStaticAirReq, _
fnGetIndexOfSelected(PAODisplay))
frmEditStaticAirPao.ShowDialog(Me)
If frmEditStaticAirPao.DialogResult = Windows.Forms.DialogResult.OK Then
Me.thisStaticAirReq = frmEditStaticAirPao.thisReq
End If
RefreshPAODisplay()
End Sub
Thank you heaps!!!
BTW if there are any comments about the above please fire away. I want to improve. :eek2:
Re: [2005] How to call a public fn that's in one form, from another?
You don't need a Shared anything. You can access any Public members of the editing form you like. They don't have to, and absolutely shouldn't, be Shared.
VB Code:
Dim f As New EditingForm
'Copy the existing value from this form to the new form.
f.TextBox1.Text = Me.TextBox1.Text
If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Copy the edited value from the editing form to this form.
Me.TextBox1.Text = f.TextBox1.Text
End If
In this case though you don't even have to get anything from the editing form because you can simply pass it the ListViewItem directly. It can then edit the item and the result will show up in the ListView. The editing form would look something like this:
VB Code:
Public Class EditingForm
Private _editingItem As ListViewItem
Public Sub New(ByVal editingItem As ListViewItem)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Keep a reference to the ListViewItem so that it can be updated if the user presses the OK button.
Me._editingItem = editingItem
'Display the subitems fro editing.
Me.TextBox1.Text = Me._editingItem.SubItems(0).Text
Me.TextBox2.Text = Me._editingItem.SubItems(1).Text
Me.TextBox3.Text = Me._editingItem.SubItems(2).Text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Update the item being edited.
Me._editingItem.SubItems(0).Text = Me.TextBox1.Text
Me._editingItem.SubItems(1).Text = Me.TextBox2.Text
Me._editingItem.SubItems(2).Text = Me.TextBox3.Text
'Dismiss the dialogue and return OK.
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
End Class
You'd then call it from the main form something like this:
VB Code:
Using ef As New EditingForm(Me.ListView1.SelectedItems(0))
ef.ShowDialog()
End Using
It's that easy.
Re: [2005] How to call a public fn that's in one form, from another?
jmcilhinney,
Thanks very much for the examples to follow. I got it all working nicely. :)