Results 1 to 15 of 15

Thread: passing my data inputs from frmMain to a secondary form

  1. #1
    New Member
    Join Date
    Jul 12
    Posts
    9

    passing my data inputs from frmMain to a secondary form

    pretty new to this, and i understand a fair bit, but when the prof starts talking about arrays i get lost, i have user inputs on the frmMain like name, address, city, then items like hatsqty, t-shirts qty, etc. and i need to transfer all of that to a form called sales, and eventually to a txt file using I/O (and that i kind of get. what the prof wants us to use on the second form is a list box, i was able to have some sucsess with a test app i made using a list view item, i just need some help to get started, one i get going i can usually figure it out, but right now gettin started is killing me!!! thanks in advance

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    Re: passing my data inputs from frmMain to a secondary form

    How do arrays figure into this? Are you required to use them?

    I was going to point you to a whole thread on the subject of the ways you can pass data between forms, but the search on the forums isn't working, yet, after the big change on Monday. Therefore, no link for you.

    It's hard to say what to recommend, as I don't know what will make sense to you at this point, and what would be something new. Have you covered properties? The typical way to pass things between forms would be to put properties on one form or the other, and pass the data via properties. If you haven't covered that, then how about adding a module to the project and putting the variables in there declared Public? That would make them global variables visible to all forms. It's not a great way to handle things, but it will work pretty well for this situation.
    My usual boring signature: Nothing

  3. #3
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    thanks for the response, actually i am not sure they do at this point, but its what he's been discussing for the last few days so i think they will figure into this somewhere. and a bunch of us get together after class in the lab and they are all trying to do it with arrays. i am familiar with the 2nd option of creating a module and putting public (global?) variables, that was what i did with the test form that i had some sucsess with (except if i entered more than 7 chars it would return ...
    for example 1234567 came back as 1234567 but 12345678 came back as ...1234) and that was with a list view item, but he wants me to use a list box. but at this point i just need to get it to work, and like i said i am familiar with the module aspect and comfortable with doing just that. what i can say is i never got more than one txtbox to work, the only txtbox on frmMain was txtUserName, but when i tried txtAddress, the input would not pass. all help is greatly appreceated!!!

  4. #4
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    Re: passing my data inputs from frmMain to a secondary form

    Nothing much can be said without seeing code. If you have a variable for UserName, and a different variable for Address, they should both work. Since they are not, either you are trying to do this with a single variable, or you are doing something odd.
    My usual boring signature: Nothing

  5. #5
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    All the txt boxes have different names

    txtUserName
    txtAddress
    txrCity

    Etc. I will upload my code ASAP

  6. #6
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    Option Explicit On
    Option Strict On


    Public Class frmSallysSouvenirs


    'Order Form Project: written by Rick Ferguson 7/9/12
    'modified 7/18 added cbo state, cbo hat/tee sizes and colors, splash, 2nd frm (sales)
    'modified 7/23 fixed calculate
    'modified 8/1 installed MenuStrip


    'Collects number of hats and tees, adds the total items and then multiplies by price and outputs to grand total

    'Declare constants
    Const decHats As Double = 15.99D '$15.99
    Const decTees As Double = 9.99D '9.99
    Const SALESTAX As Double = 0.087D '8.7%




    Private Sub chkBoxHats_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBoxHats.CheckedChanged
    If chkBoxHats.Checked = False Then
    txtHats.Enabled = False
    cboHatSize.Enabled = False
    cboHatColor.Enabled = False
    ElseIf chkBoxHats.Checked = True Then
    txtHats.Enabled = True
    cboHatSize.Enabled = True
    cboHatColor.Enabled = True
    MessageBox.Show("Please enter the size, color & quantity of hats")
    txtHats.Focus()
    End If

    End Sub

    Private Sub chkBoxTees_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBoxTees.CheckedChanged

    If chkBoxTees.Checked = False Then
    txtTees.Enabled = False
    cboTSize.Enabled = False
    cboTColor.Enabled = False
    ElseIf chkBoxTees.Checked = True Then
    txtTees.Enabled = True
    cboTSize.Enabled = True
    cboTColor.Enabled = True
    MessageBox.Show("Please enter the size, color & quantity of tees")
    txtTees.Focus()
    End If

    End Sub


    Private Sub txtUserName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUserName.Leave
    'Checking for text input only:
    Dim blnLtrCheck As Boolean

    If txtUserName.Text <> "" Then 'Bad input
    blnLtrCheck = Alphabetic(txtUserName.Text)
    If blnLtrCheck = False Then
    MessageBox.Show("You must enter your name here.", "Input Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    txtUserName.Focus()
    End If
    Else
    MessageBox.Show("You must enter your name here.") 'error msg if you try to tab out without entering data
    txtUserName.Focus()
    txtUserName.SelectAll()
    End If

    txtUserName.Text = StrConv(txtUserName.Text, VbStrConv.ProperCase) 'caps first letter of fname/lname
    End Sub

    Private Sub txtAddress_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAddress.Leave
    'Checking for text input only:
    If txtAddress.Text = "" Then 'Bad input
    MessageBox.Show("You must enter your address here.", "Input Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    txtAddress.Focus()
    txtAddress.SelectAll()
    End If
    txtAddress.Text = StrConv(txtAddress.Text, VbStrConv.ProperCase) 'caps address as needed
    End Sub

    Private Sub txtCity_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCity.Leave
    'Checking for text input only:
    Dim blnLtrCheck As Boolean

    If txtCity.Text <> "" Then 'Bad input
    blnLtrCheck = Alphabetic(txtCity.Text)
    If blnLtrCheck = False Then
    MessageBox.Show("You must enter a City here.", "Input Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    txtCity.Focus()
    End If
    Else
    MessageBox.Show("You must enter a City here.")
    txtCity.Focus()
    txtCity.SelectAll()
    End If
    txtCity.Text = StrConv(txtCity.Text, VbStrConv.ProperCase) 'caps city as needed
    End Sub


    Private Sub txtZip_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtZip.Leave
    'Checking for text input only:
    If txtZip.Text.Length < 5 Then
    MessageBox.Show("Please enter a 5 digit zip code")
    txtZip.Focus()
    txtZip.SelectAll()
    End If

    End Sub
    Private Sub frmSallysSouvenirs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    lblDate.Text = Today.ToString("d")
    txtHats.Enabled = False 'sets txt hats input box to disabled until chkbox is checked
    txtTees.Enabled = False 'sets txt tees input box to disabled until chkbox is checked
    cboHatSize.Enabled = False
    cboHatColor.Enabled = False
    cboTSize.Enabled = False
    cboTColor.Enabled = False
    With cboState.Items 'Populate cboState Items collection
    .Add("AL")
    .Add("AK")
    .Add("AZ")
    .Add("AR")
    .Add("CA")
    .Add("CO")
    .Add("CT")
    .Add("DE")
    .Add("DC")
    .Add("FL")
    .Add("GA")
    .Add("HI")
    .Add("ID")
    .Add("IL")
    .Add("IN")
    .Add("IA")
    .Add("KS")
    .Add("KY")
    .Add("LA")
    .Add("ME")
    .Add("MD")
    .Add("MA")
    .Add("MI")
    .Add("MN")
    .Add("MO")
    .Add("MT")
    .Add("NC")
    .Add("ND")
    .Add("NE")
    .Add("NH")
    .Add("NJ")
    .Add("NM")
    .Add("NV")
    .Add("NY")
    .Add("OH")
    .Add("OK")
    .Add("OR")
    .Add("PA")
    .Add("RI")
    .Add("SC")
    .Add("SD")
    .Add("TN")
    .Add("TX")
    .Add("UT")
    .Add("WA")
    .Add("WI")
    .Add("WV")
    .Add("WY")
    End With
    cboState.SelectedIndex = 44 'Select 1st item for display
    With cboHatSize.Items 'Populate cboHatList Items collection
    .Add("Sizes")
    .Add("Sm 6-7")
    .Add("Med 7-8")
    .Add("Lrg 8-9")
    End With
    cboHatSize.SelectedIndex = 0 'Select 1st item for display

    With cboTSize.Items 'Populate cboTSize Items collection
    .Add("Sizes")
    .Add("Sm 36-38")
    .Add("Med 38-40")
    .Add("Lrg 40-42")
    .Add("XLrg 42-44")
    .Add("XXLrg 44-46")
    End With
    cboTSize.SelectedIndex = 0 'Select 1st item for display

    With cboHatColor.Items 'Populate cboHatColor Items collection
    .Add("Colors")
    .Add("Red")
    .Add("Green")
    .Add("Black")
    .Add("Blue")
    End With
    cboHatColor.SelectedIndex = 0 'Select 1st item for display

    With cboTColor.Items 'Populate cboDropDown Items collection
    .Add("Colors")
    .Add("Red")
    .Add("Green")
    .Add("Black")
    .Add("Blue")
    End With
    cboTColor.SelectedIndex = 0 'Select 1st item for display
    End Sub

    Private Function Alphabetic(ByVal strInput As String) As Boolean
    'This function checks each character of the string parameter for one that is not alphabetic.
    'If it finds one, it returns "False". If not, it returns a "True".
    Dim i As Integer
    Dim strChar As String

    strInput = strInput.ToUpper

    For i = 0 To strInput.Length - 1

    strChar = strInput(i) 'Getting the first character of the string

    'If it's a blank, skip it
    If strChar = " " Then Continue For

    If Not (strChar >= "A" And strChar <= "Z") Then 'Is it alphabetic?
    Return False 'No, so return "False"
    End If

    Next i

    Return True 'Every character passed, so return "True"
    End Function




    Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

    End Sub

    Private Sub cboHatSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboHatSize.SelectedIndexChanged

    End Sub

    Private Sub mnuAbout_Click(sender As System.Object, e As System.EventArgs) Handles mnuAbout.Click
    Dim AboutForm As New frmAbout 'Opens sales frm, that contains list box for data
    frmAbout.ShowDialog()
    End Sub

    Private Sub ClearAllToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ClearAllToolStripMenuItem.Click
    txtUserName.Text = "" 'clears all boxes and output labels for next order, and clears chk boxes
    txtAddress.Text = ""
    cboState.Text = ""
    txtCity.Text = ""
    txtZip.Text = ""
    txtHats.Text = ""
    txtTees.Text = ""
    txtItemsOrdered.Text = ""
    txtSubtotal.Text = ""
    txtTotalSale.Text = ""
    txtGrandTotal.Text = ""
    chkBoxHats.Checked = False
    chkBoxTees.Checked = False
    End Sub

    Private Sub ClearOrderToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ClearOrderToolStripMenuItem.Click
    txtHats.Text = "" 'clears customer input text boxes for the next order and clears chkboxes
    txtTees.Text = ""
    txtItemsOrdered.Text = ""
    txtTotalSale.Text = ""
    txtGrandTotal.Text = ""
    chkBoxHats.Checked = False
    chkBoxTees.Checked = False
    End Sub


    Private Sub HistoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HistoryToolStripMenuItem.Click
    Dim SalesForm As New frmSales 'Opens sales frm, that contains list box for data
    frmSales.ShowDialog()
    End Sub

    Private Sub CalculateToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateToolStripMenuItem1.Click
    'Input quantities are good, so calculate the order:

    Dim intHats As Integer
    Dim intTees As Integer
    Dim dblTotalSale As Double
    Dim dblSalesTax As Double
    Dim dblGrandTotal As Double
    Dim blnHatsOK As Boolean = False
    Dim blnTeesOK As Boolean = False

    'Declare module-level boolean variables:
    Dim blnUserNameOK As Boolean = False
    Dim blnAddressOK As Boolean = False
    Dim blnCityOK As Boolean = False
    Dim blnZipCodeOK As Boolean = False

    ' If chkBoxHats.Checked = False And chkBoxTees.Checked = False Then
    'MessageBox.Show("Please order something!")
    'End If

    Try 'Check the input for hats
    If chkBoxHats.Checked Then
    intHats = CInt(txtHats.Text) 'OK, so convert to a number
    Else
    intHats = 0
    End If

    Catch ex As Exception 'Invalid, so show error msg
    MessageBox.Show("Please enter the quantity of hats.", "Input Error")
    txtHats.Focus()
    Exit Sub
    End Try

    Try 'Check the input for shirts
    If chkBoxTees.Checked Then
    intTees = CInt(txtTees.Text) 'OK, so convert to a number
    Else
    intTees = 0
    End If
    Catch ex As Exception 'Invalid, so show error msg
    MessageBox.Show("Please enter the quantity of T-shirts.", "Input Error")
    txtTees.Focus()
    Exit Sub
    End Try

    If intHats = 0 And intTees = 0 Then
    MessageBox.Show("No Customer Information or Order was placed", "Input Error")
    Exit Sub 'Nothing to calculate:
    Else
    txtItemsOrdered.Text = (intHats + intTees).ToString
    End If


    dblTotalSale = CDec(intHats) * decHats + CDec(intTees) * decTees
    dblSalesTax = dblTotalSale * SALESTAX
    dblGrandTotal = dblTotalSale + dblSalesTax

    'Display the output:
    txtGrandTotal.Text = dblTotalSale.ToString("C") 'These are formatted as currency
    txtTotalSale.Text = dblSalesTax.ToString("C")
    txtGrandTotal.Text = dblGrandTotal.ToString("C")
    txtSubtotal.Text = dblTotalSale.ToString("C")
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close() 'closes frmSally
    End Sub

    Private Sub CustomerDetailsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerDetailsToolStripMenuItem.Click
    Dim CustDetailsForm As New frmCustDetails 'Opens sales frm, that contains list box for data
    frmCustDetails.ShowDialog()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripMenuItem.Click

    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click

    End Sub

    Private Sub HelpToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub ToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs)

    End Sub
    End Class

  7. #7
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    ok, i got this working in a test project, but i can only get it to pass one box "name" to label 1
    but i still have address and city, etc and cant figure out why only 1 box will pass data

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    lblUserName.Focus()
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

    Dim frm As Form2 = New Form2(lblUserName.Text)
    frm.Show()

    End Sub


    End Class


    Public Class Form2
    Dim TextBox1 As Object

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Sub New(ByVal strTextBox As String)

    InitializeComponent()

    Label1.Text = strTextBox


    End Sub

    End Class

    its progress but SLOOOOW ;-)

  8. #8
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    Re: passing my data inputs from frmMain to a secondary form

    There are some good things in what you are doing, though. For one thing, it is really good to see somebody learning with Option Strict ON. That seems to be ignored by lots of classes, yet it is a very useful way to work. Another good point is that you are creating a new instance of Form2 rather than relying on the utterly evil default instance.


    Now that I see how you are doing this, I think there is a better way than what I was suggesting earlier, especially since you aren't using default instances. Since you are creating the variable frm, you now have access to all of the controls on that frm from within Form1. Therefore, rather than passing all the items across in the constructor, you could do something like this:

    frm.Label1.Text = strTextBox.Text

    You could do that for all of the items you wanted to pass, because frm gives you access to all the public and Friend members of the form instance. Since all controls are Friend, you have access to them all. This is a better way to pass things over rather than using global variables. If there are items that don't go into any particular control on the second form, that is where properties come in. After all, .Text is just a property of the label and textbox control. Similarly, you can add properties to a form and access them just as you are accessing the properties of the controls.
    My usual boring signature: Nothing

  9. #9
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    Thanks for the compliments, we have a pretty good professor, I just wish I hadn’t done the course in summer (7 weeks vs 12 weeks). 2 things though, when I tried the strTextBox.Text I received an error
    ‘Text’ is not a member of string, and I don’t know exactly what that means (I’m going to look it up after this) the second is how do I collect data from multiple input boxes from the first form?
    Label 1 grabs the name, but I still need address, city, state, etc. label1(lblUserName) gets the text from the first form but label 2(lblAddress) (thats all i have done so far) still does not grab the text from the first form/2 txtbox and eventually I need it to go to a listbox but one step at a time, and I still want to know how to do this anyway. Thanks for all your help!

  10. #10
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    Thanks for the compliments, we have a pretty good professor, I just wish I hadn’t done the course in summer (7 weeks vs 12 weeks). 2 things though, when I tried the strTextBox.Text I received an error
    ‘Text’ is not a member of string, and I don’t know exactly what that means (I’m going to look it up after this) the second is how do I collect data from multiple input boxes from the first form?
    Label 1 grabs the name, but I still need address, city, state, etc. label1(lblUserName) gets the text from the first form but label 2(lblAddress) (thats all i have done so far) still does not grab the text from the first form/2 txtbox and eventually I need it to go to a listbox but one step at a time, and I still want to know how to do this anyway. Thanks for all your help!

  11. #11
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    Re: passing my data inputs from frmMain to a secondary form

    Nothing better than the double posting that has shown up with the new forum layout. Hopefully, that will go away fairly soon.

    I assumed that strTextBox was a textbox because that was in the name. Based on the error you are getting, strTextBox must be a string, not a Textbox. A string doesn't have a .Text property, which is what causes the error. Of course, that just makes it easier, as the code becomes:

    frm.Label1.Text = strTextBox

    As for the second half of your post, I can't quite understand what you are doing there. In the code you posted, Label1 gets text from an argument to the constructor. You could add a separate argument for each of the other items that form2 needs, but that seems kind of excessive. I was suggesting that you get rid of the arguments to the contructor, and do something like this (on Form1):
    Code:
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    
    Dim frm As Form2 = New Form2()
    
    frm.Label1.Text = labelUserName.Text
    frm.Label2.Text = 'whatever goes in label 2'
    'etc.
    frm.Show()
    
    End Sub
    My usual boring signature: Nothing

  12. #12
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    I don’t know why my teacher calls them labels when they are textboxes but he does and it confounds and confuses me, yes on frm1 there are 2 text boxes
    lblUserName but what I would code is txtUserName
    and
    lblAddress txtAddress
    on
    frm2 I do have 2 labels
    Label1
    Label2
    I am able to pass the text from lblUserName to Label1 no problem, shows up every time
    I can’t get lblAddress to pass to Label2 at all!, when I tried the code you sent it kept throwing exceptions. Sometimes this stuff is really easy and logical, and sometimes it’s just so frustrating. I know once I can get 2 txt boxes to pass to 2 labels, I can do it as many times as I need to, but why cant I get the second text box to pass to the 2nd label? Thanks again!

  13. #13
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    Re: passing my data inputs from frmMain to a secondary form

    What is the code that you are using for passing that second textbox? In what you posted, you were passing one string as an argument to the constructor, which is fine, but what are you doing for the second one?
    My usual boring signature: Nothing

  14. #14
    New Member
    Join Date
    Jul 12
    Posts
    9

    Re: passing my data inputs from frmMain to a secondary form

    thats what i cant figure out how to do, how do i pass the second txtbox(on frm1) to the 2nd label on the 2nd form

  15. #15
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    Re: passing my data inputs from frmMain to a secondary form

    Ok, what were the exceptions you were getting with the code I showed?

    If you were only passing one or two things, then using arguments to the constructor, as you did for the first item, would be reasonable. However, it sounds like you are potentially passing LOTS of things. You could still do that as arguments to the constructor, but it would get kind of crazy.
    My usual boring signature: Nothing

Posting Permissions

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