|
-
Apr 19th, 2006, 02:27 PM
#1
Thread Starter
Member
[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:
Private Sub cmdNewTicket_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNewTicket.Click
Dim newTicketForm As frmCreateRemainderRawTickets
'This call instantiates the forms collection to the form that I am creating.
'The second time I run this, the initialization portion is not called
'from the form manager.
newTicketForm = CType(FormManager.getForm(FormManager.CREATE_REMAINDER_RAW_TICKETS), frmCreateRemainderRawTickets)
Dim drv As DataRowView
drv = cmbTicketNum.SelectedItem
'I think that this is what is happening.
'When run for the first time,
'The selected item, cmbTicketNum.selecteditem, is the first record because the
'Recordset is re-initialized during the call to the
'Form Manager above.
Dim row As Data.DataRow
row = drv.Row
Dim origCourses As Decimal = CDec(lblCourses.Text)
Dim usedCourses As Decimal = CDec(txtNumberOfCourses.Text)
Dim defaultCourses = origCourses - usedCourses
'Get the width and length to transfer over to other form
Dim nWidth As Decimal = CDec(lblWidth.Text)
Dim nLength As Decimal = CDec(lblLength.Text)
newTicketForm.prime2(tempRemainderTickets, row("PK_Item"), row("sDescription"), defaultCourses, nWidth, nLength, Me)
End Sub
VB Code:
'Second Form
Public Sub prime2(ByVal inTickets As ArrayList, _
ByVal inItemPk As Integer, _
ByVal itemDesc As String, _
ByVal defaultCourses As Decimal, _
ByVal width As Decimal, _
ByVal length As Decimal, _
ByVal inCallingForm As frmConsumeRawMaterial)
autoUpdate = True
callingForm = inCallingForm
tickets = inTickets
itemPk = inItemPk
' Suppress repainting the TreeView until all the objects have been created.
TreeView1.BeginUpdate()
' Clear the TreeView each time the method is called.
TreeView1.Nodes.Clear()
If tickets.Count > 0 Then
Dim ticket As RawTicket
For Each ticket In tickets
Dim node As TreeNode
node = New TreeNode("Ticket #" & ticket.getTicketNum())
TreeView1.Nodes.Add(node)
Next
End If
'lblItemDesc.Text = itemDesc
clearFields()
cboItem.SelectedValue = itemPk
txtNumberofCourses.Text = defaultCourses
txtAverageWidth.Text = width
txtAverageLength.Text = length
' Look up item PK, width, length, and thickness if haven't already.
'If poItem.getItemPK < 0 Then
' setItemInfo()
'End If
' Begin repainting the TreeView.
TreeView1.EndUpdate()
autoUpdate = False
dataValidator = New DataValidator
'callingForm.Hide()
Me.Show()
End Sub
Public Overrides Sub init()
'This is where the passed data to the combobox it cleared when the
'Consume Raw Material form attempts to pass the selected item to the
'Create Remainder Raw Material Tickets form. This event only happens
'the first time which is consistant with the reason the data is there
'the first time but not following times.
DataSetRemainderItem1.Clear()
ItemsDataAdapter1.Fill(DataSetRemainderItem1)
End Sub
-
Apr 19th, 2006, 08:28 PM
#2
Hyperactive Member
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.
-
Apr 19th, 2006, 08:38 PM
#3
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?
-
Apr 20th, 2006, 09:47 AM
#4
Frenzied Member
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 ??
-
Apr 20th, 2006, 04:28 PM
#5
Lively Member
Re: Passing data to second form
 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
-
Apr 20th, 2006, 08:39 PM
#6
Thread Starter
Member
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.
-
Apr 20th, 2006, 09:34 PM
#7
Re: Passing data to second form
 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.
-
Apr 21st, 2006, 04:22 AM
#8
Frenzied Member
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.
-
Apr 21st, 2006, 05:15 AM
#9
Lively Member
Re: Passing data to second form
 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.
-
Apr 21st, 2006, 01:46 PM
#10
Thread Starter
Member
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.
-
Apr 21st, 2006, 02:28 PM
#11
Hyperactive Member
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"--
-
Apr 21st, 2006, 03:23 PM
#12
Thread Starter
Member
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.
-
Apr 21st, 2006, 03:55 PM
#13
Hyperactive Member
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:
'at top of class
private strContainer as string
Public Sub New( Optional ByVal strValueBeingPassed As String = "")
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"--
-
Apr 21st, 2006, 06:09 PM
#14
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.
-
Apr 22nd, 2006, 08:41 AM
#15
Thread Starter
Member
Re: Passing data to second form
Thanks, jmcilhinney, I will look at your tutorial and study this more.
-
Apr 22nd, 2006, 11:42 AM
#16
Re: Passing data to second form
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|