Hi, i have enquires regarding the calling of form 1 to form 2.
I wan to make use of form 1 function which is to be called from form 2. How do i achieve it?
Can u show mi some examples?
Printable View
Hi, i have enquires regarding the calling of form 1 to form 2.
I wan to make use of form 1 function which is to be called from form 2. How do i achieve it?
Can u show mi some examples?
For example this goes in form1
and you call it in form2 like this in a desired event:VB Code:
Public Function multiply(ByVal x As Integer, ByVal y As Integer) As Integer Return x * y End Function
VB Code:
Dim frm As New Form1 MessageBox.Show(frm.multiply(10, 20))
Like, if i got form 1 clear form hor, which i make is
public sub clearform()
datagrid1 clear()
dataset1.clear()
end sub
Then in form 2, i wan to close this form, which is able to refresh my datagrid and dataset.
How to do it?
Describe it a bit more clear. I am afraid you need more than what i provided for you in previous post.
ok... My form1 is for searching of records, when e user enter the search name, then click search, then it will display a datagrid showing the search record.
When click on e datagrid, then it will show e record in each of e respective textbox, that will be my form2.
When he edit the record, it will save into e database n close e form. Then the form1 will clear all the search record from e datagrid and enable the user to re serach again OR e datagrid will display the edited record.... (I will be doing in either way)
Ok, so why not doing it in this way. When the user clicks on the datagrid then you show From2 modally, that is ShowDialog. So it suspends the rest of the procedure until Form2 is closed. Then after returning you call 'clearform'. So you dont need to refresh Form1 in closing event of Form2.
VB Code:
' This goes where you catch the user click on the datagrid Dim frm as New Form2 frm.ShowDialog ' this is done after form2 is closed Me.clearform() '
i not doing in dialog form lei, i use form one... how to do in form one? Got any other methods?
huh? form one?
Oh, i got it liao, Thanks.. I can clear the form.
But if i wan to close e form2, then i wan display e edited record, how to do ah?
in Form1.....
then in Form2 ( to call the sub in Form1 / to call any functions you have in Form1 ) ....VB Code:
Dim frm2 As New Form2() MyBase.AddOwnedForm(frm2)'/// add it as owned by Form1. frm2.Show() Private Sub testing() MessageBox.Show("i was triggered from Form2!") End Sub
VB Code:
'/// within a button click event etc... Dim frmMain As Form1 = Me.Owner frmMain.testing() '/// call the sub on Form1 here.
I think you have to refetch the data from database. But it really depends on your design. You may use the same instance of your dataset in form2 and updating that will refelect back to form1 too, I don't kow if it makes sense for you.Quote:
Originally posted by berrie_luv
Oh, i got it liao, Thanks.. I can clear the form.
But if i wan to close e form2, then i wan display e edited record, how to do ah?
E form cannot use again... cos when i click e datagrid hor, e textbox onli show e first record, i selected the rest, still show e 1st record. Wat should i do? Help...Quote:
Originally posted by Lunatic3
Ok, so why not doing it in this way. When the user clicks on the datagrid then you show From2 modally, that is ShowDialog. So it suspends the rest of the procedure until Form2 is closed. Then after returning you call 'clearform'. So you dont need to refresh Form1 in closing event of Form2.
VB Code:
' This goes where you catch the user click on the datagrid Dim frm as New Form2 frm.ShowDialog ' this is done after form2 is closed Me.clearform() '
Can u pls help mi... i need to complete this urgently. When selecting the 3rd record or others, it onli show e 1st record...
How to debug this error?
post your code please.
Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
Dim i As Integer
For i = 0 To 7
staffArray(i) = DataGrid1.Item(DataGrid1.CurrentRowIndex, i)
Next
Dim frm As New frmViewStaff()
frm.ShowDialog()
Me.clearForm()
End Sub
This is my codes when e datagrid is clicked.
and in form2 what happens there? send the whole project if you dont mind.
The other form is just view in e textbox loh.... i store them in array from e datagrid then pass to this form2.
Private Sub frmViewStaff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtSur.Text = staffArray(0)
txtName.Text = staffArray(1)
txtStaffNo.Text = staffArray(3)
txtDes.Text = staffArray(4)
txtDep.Text = staffArray(5)
txtExt.Text = staffArray(6)
txtEmail.Text = staffArray(7)
End Sub
Is there any prob with the form1?
I see no problem with your code, if you are passing the staffarray to frmViewStaff correct then there should be no problem. How you do that?
yaya.. but i dun noe y there is error over here. If i never do e codes u given mi, then everything works out fine. Is there any other way compared to e showDialog one?
I guess it has nothing to do with showdialog. You better send me the whole project so i can examine it for you.
Sorry ah, i cannot post e whole pj here.
When i click e datagrid, too many times, it will display that the index was out of bound. Wat happen ah? Then sometimes the selection will display wrong data.
out of bonds of what? the array or related to datagrid?
Yaya, dunnoe why... so weird one. U know?
well if you have followed the rules properly then it sohuld not happen. I know nothing about your desgin and I am affraid i cant help you without knowing exactly what you are doing and how you have coded.
Is there any other method other than the showDialog one?
to show another form from your form you can use Show or ShowDialog. In latter case the execution of code in the first form is suspeneded untill the secod form is closed,but in first the codes after that continues to run.
I tink showDialog is a gd method, but will there be any problem regarding my array? Is array suitable in the showDialog method?
Where you have declared your array? how you pass it to the second form?
I declare the array as module.
Module module1
Public staffArray(7) As String
End Module
Then pass the module to my form2, which is in e form load
Private Sub frmViewStaff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtSur.Text = staffArray(0)
........................................
........................................
end sub
well, i really cant say whats wrong. I just saw a minor point in your code that maybe not important.
txtName.Text = staffArray(1)
txtStaffNo.Text = staffArray(3)
You dont use staffArray(2)? Does your datagrid have 8 or 7 columns?
should be like this, all will be used.
Private Sub frmViewStaff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtSur.Text = staffArray(0)
txtName.Text = staffArray(1)
txtNUSNo.Text = staffArray(2)
txtStaffNo.Text = staffArray(3)
txtDes.Text = staffArray(4)
txtDep.Text = staffArray(5)
txtExt.Text = staffArray(6)
txtEmail.Text = staffArray(7)
End Sub