executing function on form 1 from form 2

mikerob283

Member
Joined
Feb 15, 2005
Messages
12
Programming Experience
Beginner
executing events on form 1 from form 2

Pay close attention to comments please. There are alot of things that would be benificial if I could execute events from satellite forms.
Basically this is the real deal I have a datagrid and some labels on form 1.
I click a button on form 1 to open form 2 which has a few search options to help refresh the datagrid with different queries. After choosing a certain company or user on form 2 I need to be able to click apply (executes datagrid's refresh with new query string and keeps form 2 open) or ok (does same as apply but closes form 2 also)

Problem: the lblStatus on form 1 does not have the change I sent to it from form 2's button_click(). That would be because form 1 has to "refresh"? how would I do that? This is the only thing I'm stuck on

below is a short example of what is going on.

VB.NET:
 Form1 
 
lblStatus.Text = ""
 
Sub bMakeChange_Click()
Dim frm2 As New Form2
frm2.OpenDialog()
End Sub
 
Sub Refresh_lblStatus()	 'Option 1 I tried, this is called from 
Dim frm2 As New Form2	 'bOk_Click() on form 2
lblStatus.Text = frm2.Status.Text
End Sub
 
-----------------------------------------------
 
Form2
 
'stuff here
 
Sub bOk_Click()
Dim Status
Status.Text = 1
Dim frm1 As New Form1
frm1.Refresh_lblStatus()				 'Did not work what am I missing?
frm1.lblStatus.Text = Status.Text 'I also tried this (without) the line above, 
Me.Close()								'but form1 has to refresh to have
End Sub									 'changes applied correct?

Thanks,
Michael

I have searched on this but one article I went to wouldn't refresh the new labels on form 1 and form 1 is the main form so if it closes the whole program closes... that's why I have it initially open visible=false and load up a frmlogin.showdialog()
 
Last edited:

mikerob283

Member
Joined
Feb 15, 2005
Messages
12
Programming Experience
Beginner
Ah made Sub Refresh_lblStatus(), Public Sub Refresh_lblStatus() and that made it show up in form 2 as frm2.Refresh_lblStatus() but it still don't refresh the lbls on form 1
 

mikerob283

Member
Joined
Feb 15, 2005
Messages
12
Programming Experience
Beginner
ok I changed that to frm2.Show() instead... but still I have 3 ways currently programmed in to transfer the text from the text on form 2 to form 1...

form 2 loads the text into public variables in a module, form 1 pulls from this module the variables... when the ok/apply is clicked...

form 2 directly places the text from form2 into the lbl on form 1 when the ok / apply is clicked
frm1.lblStatus.Text = frm2.Status.Text

form 2 executes the public event frm1.Refresh_Data() on form 1 when the ok/ apply button is clicked and that event does the above two things

with that said...
I tested this out, I created a button on form 1 called "refresh" and pulled everything from the refresh_data() to inside the "refresh" button...

when I got done with form 2 and clicked ok/apply I was staring at form 1 so I clicked the "refresh" button and bam the lbls changed... but that's not what I want. I want to be able to click ok on form 2 and have it change lbls on form 1, there has to be a way... I see it every day in programs I use every day.
 
Joined
Feb 11, 2005
Messages
12
Programming Experience
5-10
This will work:

VB.NET:
Module Module1
	Public frm1 As New Form1
	Public frm2Open As Boolean
	Sub main()
		frm1.ShowDialog()
	End Sub
End Module

Public Class Form1
	 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		If Not frm2Open Then
			frm2Open = True
			Dim frm2 As New Form2
			frm2.Show()
		End If
	End Sub
End Class

Public Class Form2
	Private Sub UpdateForm1()
		frm1.Label1.Text = TextBox1.Text
	End Sub

	Private Sub butOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butOK.Click
		UpdateForm1()
		Me.Close()
	End Sub

	Private Sub butApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butApply.Click
		UpdateForm1()
	End Sub

	Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
		frm2Open = False
	End Sub
End Class
 
Top Bottom