Results 1 to 21 of 21

Thread: combo box!!! >:(

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    combo box!!! >:(

    so, by adding the Return keyword inside my loop, it gives me all the info. for microsoft.
    how do i get the info. for dell or sun? when i select that from the combo box.
    VB Code:
    1. Do Until sr.Peek() = -1
    2.             myObject = New stocks 'Create the new instance.
    3.             myObject.Company() = sr.ReadLine() 'Read the name from the first line.
    4.             myObject.Shares = Convert.ToInt32(sr.ReadLine())  'Read the number of shares from the second line.
    5.             myObject.PurchasePrice = Convert.ToDecimal(sr.ReadLine())  'Read the share price from the third line.
    6.             myObject.PurchaseDate = Date.ParseExact(sr.ReadLine(), "dd/MM/yyyy", Nothing)    'Read the date from the fourth line in the specific format.
    7.             myObjects.Add(myObject) 'Add the new instance to the collection.
    8.             Return
    9.         Loop

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: combo box!!! >:(

    Are we to assume you're reading stock prices out of a text file? I don't think you should have that Return statement if you are looking for that loop to keep going. Calling Return causes the current sub/function to exit, and if it's a function, pass a value back to the calling sub/function.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: combo box!!! >:(

    OK, after reading the other thread, I see what you're doing now. To more expand on what jmcilhinney said, You need to calm down and slow down a bit. Until you know certain methods and events like the back of your hand, you might want to consider a more segmented approach. First thing to do, is read the text from the files, which you've been given code to do. Next, you might want to try iterating through your arraylist and ensuring you've got all your data. Then, start looking at how to access specific pieces of data inside that arraylist. Then, start worrying about how to display them, etc.. etc.. You can't always design the entire program at once and expect all the pieces to come together..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    yes im reading stock prices from a text file.
    but the original problem i had when i DIDNT have the RETURN keyword there was that i kept getting the last 4 lines of text displayed in the textboxes when i selected any name from the combo box.
    my text file contains this:

    Microsoft
    3000
    27.51
    15/11/2005
    Sun Microsystems
    6000
    4.25
    31/12/2005
    Dell
    2500
    30.52
    02/01/2006
    So, everytime i select a company name from the combo box (microsoft, sun, dell) i want the info. from that company to be displayed in the textboxes.
    Instead, i get dell's info. displayed. Even if i select microsoft or sun.
    Do u have a solution for me?
    Thanks

  5. #5
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: combo box!!! >:(

    Hmm, You have this Arraylist called "MyObjects". What is the code you are using to display the company names in the combo box, and also, what is the code you are using to display the selected item.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combo box!!! >:(

    Are you sure what the code you have is doing? It seems that you aren't quite sure what the code in the other thread did. Apparently, it reads every four lines in the textfile, puts the info in an object called "MyObject", then adds that object to an arraylist called "MyObjects". After the file read, you should have all the info you need inside of the MyObjects arraylist. It would just be a matter of looping through the arraylist for each object, and checking the object's name, and seeing if that name matches the name you are choosing in the combobox. What you are trying to do here, for some reason, is read the lines all over again...? Which should not be needed at all. You can check this by displaying the count of the arraylist after you run your read code... If you put the line:
    VB Code:
    1. Messagebox.Show(MyObjects.Count.ToString())
    If you use the code from the past thread, then you should have at least 3 objects for the count after it is read in, if everything runs correctly... so now its a matter of looping through that arraylist for each object, and see if the object's name property is equal to your combobox selected value...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    this is the code to display the info. in the approperiate text boxes.
    finally someone asks me for my code
    what do u c wrong?

    VB Code:
    1. Private Sub cboCompanyName_SelectedIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCompanyName.SelectedIndexChanged
    2.  
    3.         txtPDate.Text = CStr(myObject.PurchaseDate())
    4.         txtShares.Text = CStr(myObject.Shares)
    5.         txtPPrice.Text = CStr(FormatCurrency(myObject.PurchasePrice()))
    6.         txtTValue.Text = FormatCurrency(myObject.TotalValue())
    7.         txtWorth.Text = CStr(FormatPercent(myObject.TotalValue() / myObject.TotalValue()))

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combo box!!! >:(

    You have to loop through... in that event.. for every object in your arraylist... and I dont see where you have the object name as part of the object???
    VB Code:
    1. 'in your changed event, just hand-coded using guesses of your code...
    2. For Each Obj as MyObject in MyObjects
    3.       If Cstr(Obj.CompanyName) = cboCompanyName.Text then '<-- MyObject.CompanyName is the company text of your object, didnt see that in the code above?
    4.              txtPDate.Text = CStr(Obj.PurchaseDate())
    5.              txtShares.Text = CStr(Obj.Shares)
    6.              txtPPrice.Text = CStr(FormatCurrency(Obj.PurchasePrice()))
    7.              txtTValue.Text = FormatCurrency(Obj.TotalValue())
    8.              txtWorth.Text = CStr(FormatPercent(Obj.TotalValue() / Obj.TotalValue()))
    9.              Exit For
    10.       End If
    11. Next
    *also, all of the CStr shouldnt be necessary. If they are already a string, no need to cast them into a string. For the ones you need to cast, you can use just .ToString()
    Last edited by gigemboy; Jan 25th, 2006 at 12:45 AM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    Quote Originally Posted by gigemboy
    Are you sure what the code you have is doing? It seems that you aren't quite sure what the code in the other thread did. Apparently, it reads every four lines in the textfile, puts the info in an object called "MyObject", then adds that object to an arraylist called "MyObjects". After the file read, you should have all the info you need inside of the MyObjects arraylist. It would just be a matter of looping through the arraylist for each object, and checking the object's name, and seeing if that name matches the name you are choosing in the combobox. What you are trying to do here, for some reason, is read the lines all over again...? Which should not be needed at all. You can check this by displaying the count of the arraylist after you run your read code... If you put the line:
    VB Code:
    1. Messagebox.Show(MyObjects.Count.ToString())
    If you use the code from the past thread, then you should have at least 3 objects for the count after it is read in, if everything runs correctly... so now its a matter of looping through that arraylist for each object, and see if the object's name property is equal to your combobox selected value...
    when i put that line into my loop, i get msgbox saying 1, then 2, then 3.

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combo box!!! >:(

    Thats what its supposed to say... (although it was just supposed to go after the loop ) Shows you have three objects in the arraylist, now its a matter of looping through the arraylist, see my post above....

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    Quote Originally Posted by gigemboy
    You have loop through... in that event.. for every object in your arraylist... and I dont see where you have the object name as part of the object???
    VB Code:
    1. 'in your changed event, just hand-coded using guesses of your code...
    2. For Each Obj as MyObject in MyObjects
    3.       If Cstr(Obj.CompanyName) = cboCompanyName.Text then '<-- MyObject.CompanyName is the company text of your object, didnt see that in the code above?
    4.              txtPDate.Text = CStr(Obj.PurchaseDate())
    5.              txtShares.Text = CStr(Obj.Shares)
    6.              txtPPrice.Text = CStr(FormatCurrency(Obj.PurchasePrice()))
    7.              txtTValue.Text = FormatCurrency(Obj.TotalValue())
    8.              txtWorth.Text = CStr(FormatPercent(Obj.TotalValue() / Obj.TotalValue()))
    9.              Exit For
    10.       End If
    11. Next
    *also, all of the CStr shouldnt be necessary. If they are already a string, no need to cast them into a string. For the ones you need to cast, you can use just .ToString()
    in this line, myObject has a line under it saying "myObject is not defined"
    VB Code:
    1. For Each Obj As myObject In myObjects

  12. #12
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combo box!!! >:(

    Well in your previous code, you were declaring a new instance of MyObject, and adding it to an array... so that is there somewhere? How were you doing it before?

    **EDIT - I see that MyObject is Stocks, so change it to read "For each obj as Stocks in MyObjects"

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    hmm..when i select micrsoft from the combo box, nothing shows in the text boxes, the same when i select sun.
    but, when i select dell, it displays all its info. in the text boxes.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    anyone out there to help me? PLEASE!!

  15. #15
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combo box!!! >:(

    Well are you sure that the name in your combo boxes match exactly to the name you are loading from the file? Casing would matter. Make sure that the casing is the same for the name in the combo box as well as in the file.... (or you can use .ToLower to make it all lowercase in the comparisons)

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    its the exact same....im soo mad!! cause everything else works well except this!!
    AHHHHHH

  17. #17
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combo box!!! >:(

    Do you have a name property in your Object? Because I dont see where you were adding that to the object (if you read the comments section in my last code post)....

    EDIT - I see MyObject.Company, so use that in the comparison (if you havent done so already)

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    this is wat i have in the comparison line:
    VB Code:
    1. If CStr(myObject.Company) = (cboCompanyName.Text) Then
    still nothing...
    u know, it only displays the info. for dell. when i select microsoft, or sun, the boxes remain empty!

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    hey, gig, do u want me to post wat i have so far?
    Here is all my code...
    VB Code:
    1. Dim myObjects As New ArrayList
    2.     Dim sr As New IO.StreamReader("portfolio1.txt")
    3.     Dim objstocks As stocks
    4.     Dim myObject As stocks
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.  
    8.         Do Until sr.Peek() = -1
    9.             myObject = New stocks 'Create the new instance.
    10.             myObject.Company() = sr.ReadLine() 'Read the name from the first line.
    11.             myObject.Shares = Convert.ToInt32(sr.ReadLine())  'Read the number of shares from the second line.
    12.             myObject.PurchasePrice = Convert.ToDecimal(sr.ReadLine())  'Read the share price from the third line.
    13.             myObject.PurchaseDate = Date.ParseExact(sr.ReadLine(), "dd/MM/yyyy", Nothing)    'Read the date from the fourth line in the specific format.
    14.             myObjects.Add(myObject) 'Add the new instance to the collection.
    15.             txtPortfolioV.Text = FormatCurrency(myObject.PurchasePrice * myObject.Shares)
    16.         Loop
    17.         sr.Close()
    18.  
    19.     End Sub
    20.  
    21.     Private Sub cboCompanyName_SelectedIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCompanyName.SelectedIndexChanged
    22.  
    23.         For Each obj As stocks In myObjects
    24.             If CStr(myObject.Company) = (cboCompanyName.Text) Then
    25.                 txtPDate.Text = CStr(myObject.PurchaseDate())
    26.                 txtShares.Text = CStr(myObject.Shares)
    27.                 txtPPrice.Text = FormatCurrency(myObject.PurchasePrice())
    28.                 txtTValue.Text = FormatCurrency(myObject.TotalValue())
    29.                 txtWorth.Text = FormatPercent(myObject.TotalValue() / myObject.TotalValue())
    30.                 Exit For
    31.             End If
    32.         Next
    33.     End Sub

  20. #20
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: combo box!!! >:(

    A lot of this can be resolved if you use a few simple debugging techniques... put a breakpoint in the selectedindexchanged event, step line by line and see the values as they change... this is a must-have knowledge for any programmer. Look at what myobject.company is and what cbocompanyname.text is in the locals window for each loop, etc...

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    98

    Re: combo box!!! >:(

    ahh...its over, im done with this. i e-mailed the program to my instructor already...it became annoying to deal with. besides, im a networking guy; not a programmer!
    thanks for everyone's help...

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