Results 1 to 16 of 16

Thread: [RESOLVED] Passing data to second form

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Resolved [RESOLVED] Passing data to second form

    I have been trying to figure this out for the past day or so and need some help. I am passing data from a combobox in one form to a combobox in a second form.

    The first time I click on the "cmdNewTicket", everything shows up just fine in the new form EXCEPT the data in the combobox.

    The second time I click on the "cmdNewTicket" cmd button, everything works exactly as it should...even the combobox.

    Here's the code:

    VB Code:
    1. Private Sub cmdNewTicket_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNewTicket.Click
    2.                 Dim newTicketForm As frmCreateRemainderRawTickets
    3.                           'This call instantiates the forms collection to the form that I am creating.
    4.                           'The second time I run this, the initialization portion is not called
    5.                           'from the form manager.
    6.  
    7.                 newTicketForm = CType(FormManager.getForm(FormManager.CREATE_REMAINDER_RAW_TICKETS), frmCreateRemainderRawTickets)
    8.  
    9.                 Dim drv As DataRowView
    10.                 drv = cmbTicketNum.SelectedItem
    11.  
    12.                 'I think that this is what is happening.  
    13.                                 'When run for the first time,
    14.                 'The selected item, cmbTicketNum.selecteditem, is the first record because the
    15.                 'Recordset is re-initialized during the call to the
    16.                 'Form Manager above.
    17.  
    18.                 Dim row As Data.DataRow
    19.                 row = drv.Row
    20.  
    21.                 Dim origCourses As Decimal = CDec(lblCourses.Text)
    22.                 Dim usedCourses As Decimal = CDec(txtNumberOfCourses.Text)
    23.                 Dim defaultCourses = origCourses - usedCourses
    24.  
    25.                 'Get the width and length to transfer over to other form
    26.                 Dim nWidth As Decimal = CDec(lblWidth.Text)
    27.                 Dim nLength As Decimal = CDec(lblLength.Text)
    28.                 newTicketForm.prime2(tempRemainderTickets, row("PK_Item"), row("sDescription"), defaultCourses, nWidth, nLength, Me)
    29.         End Sub

    VB Code:
    1. 'Second Form
    2.         Public Sub prime2(ByVal inTickets As ArrayList, _
    3.          ByVal inItemPk As Integer, _
    4.          ByVal itemDesc As String, _
    5.          ByVal defaultCourses As Decimal, _
    6.          ByVal width As Decimal, _
    7.             ByVal length As Decimal, _
    8.          ByVal inCallingForm As frmConsumeRawMaterial)
    9.  
    10.                 autoUpdate = True
    11.                 callingForm = inCallingForm
    12.                 tickets = inTickets
    13.                 itemPk = inItemPk
    14.  
    15.                 ' Suppress repainting the TreeView until all the objects have been created.
    16.                 TreeView1.BeginUpdate()
    17.  
    18.                 ' Clear the TreeView each time the method is called.
    19.                 TreeView1.Nodes.Clear()
    20.  
    21.                 If tickets.Count > 0 Then
    22.                         Dim ticket As RawTicket
    23.                         For Each ticket In tickets
    24.                                 Dim node As TreeNode
    25.                                 node = New TreeNode("Ticket #" & ticket.getTicketNum())
    26.                                 TreeView1.Nodes.Add(node)
    27.                         Next
    28.                 End If
    29.  
    30.                 'lblItemDesc.Text = itemDesc
    31.                 clearFields()
    32.                 cboItem.SelectedValue = itemPk
    33.                 txtNumberofCourses.Text = defaultCourses
    34.  
    35.                 txtAverageWidth.Text = width
    36.                 txtAverageLength.Text = length
    37.  
    38.                 ' Look up item PK, width, length, and thickness if haven't already.
    39.                 'If poItem.getItemPK < 0 Then
    40.                 '       setItemInfo()
    41.                 'End If
    42.  
    43.                 ' Begin repainting the TreeView.
    44.                 TreeView1.EndUpdate()
    45.                 autoUpdate = False
    46.                 dataValidator = New DataValidator
    47.  
    48.                 'callingForm.Hide()
    49.                 Me.Show()
    50.         End Sub
    51.  
    52.         Public Overrides Sub init()
    53.                 'This is where the passed data to the combobox it cleared when the
    54.                 'Consume Raw Material form attempts to pass the selected item to the
    55.                 'Create Remainder Raw Material Tickets form.  This event only happens
    56.                 'the first time which is consistant with the reason the data is there
    57.                 'the first time but not following times.
    58.  
    59.                 DataSetRemainderItem1.Clear()
    60.                 ItemsDataAdapter1.Fill(DataSetRemainderItem1)
    61.  
    62.  
    63.         End Sub

  2. #2
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Passing data to second form

    Why not try putting all your public declared sub procedures to Modules because what I see theoretically is that the sub procedure you are calling from Form1 is at Form2 which is about to be called.

    Remember that it is good practice to store all your public declared sub procedures to your modules.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing data to second form

    It is good practice to avoid using global members as much as possible. Some times it is appropriate but it is an over-used practice. Public members of modules are akin to public shared members of a class and should be used sparingly.

    There is only one mention of a combo box in your posted code and that is setting the SelectedValue so it's fairly difficult to know how your combo box is affected. Have you set breakpoints in your methods and stepped through them line by line to see what is actually happening?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: Passing data to second form

    It is good practice to avoid using global members as much as possible.
    can you explain y ?? what is the defect in defining several global functions in a module if many forms and sub routines uses them from everypart of the program ??

  5. #5
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: Passing data to second form

    Quote Originally Posted by maged
    can you explain y ?? what is the defect in defining several global functions in a module if many forms and sub routines uses them from everypart of the program ??
    With all those global functions with global variables exposed, how can you be sure that the values held in them are accurate?

    I would say that considering the rules of OOP, you want to limit your global procedures and variables. It relates closesly, I think, to encapsulation.

    Here is a link that seems to support what I'm thinking. I'm no expert but maybe this makes a convicing argument to avoid a multitude of global routines and variables, etc.

    http://www.c-sharpcorner.com/Languag...ionInCSGAG.asp

    Just a thought
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Re: Passing data to second form

    Thank you for your responses. Unfortunately, I am working on cleaning up this code. The programmer that was working on it quit and I am left with this.

    I agree with you about limiting the global routines, however, at this point, I am not at liberty to "rewrite" the code the way I think it should be. There are too many forms in the program that are linked by the "Form Manager". I think I will try what TommyGrayson mention for at least the two forms that have this problem.

    I have set breakpoints but the only difference is that the during the first call, the form is initialized. If you like, I will include the portion of code called to initialize the form. (It's not that much, really.). I have to wait until I get back at the office...I was out all day today. Thanks for your responses, though. Keep in touch .... tomorrow (Friday) I'll let you know how that went.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing data to second form

    Quote Originally Posted by maged
    can you explain y ?? what is the defect in defining several global functions in a module if many forms and sub routines uses them from everypart of the program ??
    Like I said, limit use of globals as much as possible and use them only when appropriate. I use them myself in the appropriate situations, but many people use them unnecessarily because they're too lazy or or don't know how to pass values from object to object in the proper way. Just because two forms need to access the same data doesn't mean that that data needs to be accessible globally.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: Passing data to second form

    but many people use them unnecessarily because they're too lazy or or don't know how to pass values from object to object in the proper way. Just because two forms need to access the same data doesn't mean that that data needs to be accessible globally.
    in this case i agree, by the way i was only referring to global procedures (subs and functions). i didn't mean global variables.

    in my case when a segment of code is repeated over the whole project, (like acquiring new connection ) , in such case i creates a global function that gets the username,password and return a sql connection object.

    there is no need for me to initialize sql connection object a 1000 times in my software.

  9. #9
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: Passing data to second form

    Quote Originally Posted by maged
    in this case i agree, by the way i was only referring to global procedures (subs and functions). i didn't mean global variables.

    in my case when a segment of code is repeated over the whole project, (like acquiring new connection ) , in such case i creates a global function that gets the username,password and return a sql connection object.

    there is no need for me to initialize sql connection object a 1000 times in my software.
    But of course, that is appropriate use. I think we were just suggesting caution.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Re: Passing data to second form

    I know that in the past (and i still do this) I discourage the use of repeated functions/code/security, etc... Instead, I use a class module for these that may be used. However, my programmer liked to treat the form as an object and pass the objects using this method. Problem is, I'm not used to using the forms in this manner and don't quite understand this method.

  11. #11
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    In A House :)
    Posts
    291

    Re: Passing data to second form

    all other arguments aside, have you tried passing the value to the called form via the forms constructor?
    --"Tap Dancing On The Brittle Edge Of Sanity"--

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Re: Passing data to second form

    No, I haven't. Do you mean have all the data that I am bringing from form 1 to form 2 passed through to the forms constructor? I am not sure where to start.

  13. #13
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    In A House :)
    Posts
    291

    Re: Passing data to second form

    in 2002/03 if you open up the forms windows designer generated code go to public sub new(list of parameters here) and simply add it as a parameter, you also need a private class wide variable to hold it. sort of like

    VB Code:
    1. 'at top of class
    2. private strContainer as string
    3.  
    4.  
    5.    Public Sub New( Optional ByVal strValueBeingPassed As String = "")
    6.  
    7. strContainer=strValueBeingPassed

    we do this all the time to pass info from form to form.

    forgot to add

    dim frm2 as new formWhatEva(cboBoxValueToPass) to form1 (or your main form)

    this assumes your passing a string...dopey me, time to go home now.
    Last edited by DirtyHowi; Apr 21st, 2006 at 04:01 PM.
    --"Tap Dancing On The Brittle Edge Of Sanity"--

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing data to second form

    A form is an object like any other in .NET. People seem determined to treat them differently but they are just the same as any other object. If you want to get data into an object you either set a property or pass a parameter to a method, which includes constructors. If you want to get data out of an object you get a property or the return value of a function. That's the general rule for objects and, as forms are objects, that's the rule for forms. You should read the "Forms in VB.NET" tutorial in my signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    48

    Re: Passing data to second form

    Thanks, jmcilhinney, I will look at your tutorial and study this more.

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing data to second form

    Quote Originally Posted by NLISI
    Thanks, jmcilhinney, I will look at your tutorial and study this more.
    The tutorial is nothing to do with me. I just provided the link that someone else found. I get no credit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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