Results 1 to 31 of 31

Thread: How to call from 1 form to another form?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68

    How to call from 1 form to another form?

    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?

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    For example this goes in form1
    VB Code:
    1. Public Function multiply(ByVal x As Integer, ByVal y As Integer) As Integer
    2.         Return x * y
    3. End Function
    and you call it in form2 like this in a desired event:
    VB Code:
    1. Dim frm As New Form1
    2. MessageBox.Show(frm.multiply(10, 20))
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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?

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Describe it a bit more clear. I am afraid you need more than what i provided for you in previous post.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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)

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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:
    1. ' This goes where you catch the user click on the datagrid
    2. Dim frm as New Form2
    3. frm.ShowDialog
    4. ' this is done after form2 is closed
    5. Me.clearform()
    6. '
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    i not doing in dialog form lei, i use form one... how to do in form one? Got any other methods?

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    huh? form one?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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?

  10. #10
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    in Form1.....
    VB Code:
    1. Dim frm2 As New Form2()
    2. MyBase.AddOwnedForm(frm2)'/// add it as owned by Form1.
    3. frm2.Show()
    4.  
    5. Private Sub testing()
    6.     MessageBox.Show("i was triggered from Form2!")
    7. End Sub
    then in Form2 ( to call the sub in Form1 / to call any functions you have in Form1 ) ....
    VB Code:
    1. '/// within a button click event etc...
    2. Dim frmMain As Form1 = Me.Owner
    3. frmMain.testing() '/// call the sub on Form1 here.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  11. #11
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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?
    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.
    Last edited by Lunatic3; Oct 5th, 2003 at 10:47 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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:
    1. ' This goes where you catch the user click on the datagrid
    2. Dim frm as New Form2
    3. frm.ShowDialog
    4. ' this is done after form2 is closed
    5. Me.clearform()
    6. '
    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...

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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?

  14. #14
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    post your code please.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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.

  16. #16
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    and in form2 what happens there? send the whole project if you dont mind.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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?

  18. #18
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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?

  20. #20
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I guess it has nothing to do with showdialog. You better send me the whole project so i can examine it for you.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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.

  22. #22
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    out of bonds of what? the array or related to datagrid?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    Yaya, dunnoe why... so weird one. U know?

  24. #24
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    Is there any other method other than the showDialog one?

  26. #26
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    I tink showDialog is a gd method, but will there be any problem regarding my array? Is array suitable in the showDialog method?

  28. #28
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Where you have declared your array? how you pass it to the second form?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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

  30. #30
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width