Results 1 to 33 of 33

Thread: [RESOLVED] creating a collection array from textbox's

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Resolved [RESOLVED] creating a collection array from textbox's

    I will try and explain this as best as I can. I have a group of boxes witch will all hold data input by the user. I am using an arraylist to store these, or atleast I am hoping to. So, the problem I have is getting the data to actually save to the arraylist. It keeps giving the error saying that type string cannot be converted to array list. My question is, how do I get these to save, I have been say here for about 3 hours trying to figure it out with my limited knowledge...

    Dan

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    I have tried the "add" function The Group save so to speak, just about everything I know for saving, with no prevail

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: creating a collection array from textbox's

    i'd use a list(of T) instead of an arraylist. to add the complete textboxes to a list(of textbox):

    vb Code:
    1. dim textboxes as new list(of textbox)
    2. textboxes.addrange(textbox1, textbox2, etc)
    3.  
    4. msgbox textboxes(0).text

  4. #4
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: creating a collection array from textbox's

    you could use a collection then convert ToArray afterwards

    Code:
    Dim listing As New Collection
    For Each item as Frm1.TextBox
        listing.Add(item.Text)
    Next
    listing.ToArray
    i'd be inclined to stay with the data in a collection than an array though
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    using either one of these options, would I be able to bring the items up in a list view in a particular order?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: creating a collection array from textbox's

    how many textboxes do you have?
    what information do you want in your listview? just the textbox text property?
    is there any other reason for needing a collection?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    right, i have twelve text boxes under the "product" section and another 2 under the "Price" section. Sections are a group of boxes designated to one thing (product for one and price for the other in this case) i want the text property to show in the list view. the list view has to columns, one designated price, the other designated product. as the product and price are paired, it is important that they stay that way hope this is the info you wanted.

    Dan

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: creating a collection array from textbox's

    can you post a screenshot of your form?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    Name:  user input page.bmp
Views: 257
Size:  461.3 KB
    here is the page that I want users to put there values in (11 text baxes not 12 by the way

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    Name:  page for values to be loaded.JPG
Views: 259
Size:  30.8 KB
    and here is the page that I want to load the values to. After the users saves there textbox list, i need it to show up here in the listview on the left

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: creating a collection array from textbox's

    assuming your 11 product textboxes are named txtProduct1 to txtProduct11 + your 11 price textboxes are named txtPrice1 to txtPrice11 then you can use this LINQ:

    vb Code:
    1. Dim products = (From item In Me.Controls _
    2.                         Where TypeOf item Is TextBox And _
    3.                         item.name.contains("txtProduct") _
    4.                         Order By CInt(item.name.replace("txtProduct", "")) _
    5.                         Select item.text).ToArray
    6.  
    7. Dim prices = (From item In Me.Controls _
    8.                         Where TypeOf item Is TextBox And _
    9.                         item.name.contains("txtPrice") _
    10.                         Order By CInt(item.name.replace("txtPrice", "")) _
    11.                         Select item.text).ToArray
    12.  
    13. For x As Integer = 0 To 10
    14.     ListView1.Items.Add(New ListViewItem(New String() {products(x), prices(x)}))
    15. Next

  12. #12
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: creating a collection array from textbox's

    The proper OOP way would be to create a class (Product) with two properties (ProductName and Price).
    vb.net Code:
    1. Public Class Product
    2.  
    3.     Public Sub New(ByVal name As String, ByVal price As Integer)
    4.         Me.ProductName = name
    5.         Me.Price = price
    6.     End Sub
    7.  
    8.     Private _ProductName As String
    9.     Public Property ProductName() As String
    10.         Get
    11.             Return _ProductName
    12.         End Get
    13.         Set(ByVal value As String)
    14.             _ProductName = value
    15.         End Set
    16.     End Property
    17.  
    18.     Private _Price As Integer
    19.     Public Property Price() As Integer
    20.         Get
    21.             Return _Price
    22.         End Get
    23.         Set(ByVal value As Integer)
    24.             _Price = value
    25.         End Set
    26.     End Property
    27.  
    28. End Class

    You can then use a collection of those classes and load them into the listview like this:
    vb.net Code:
    1. Private productList As List(Of Product)
    2.  
    3.     Private Sub LoadIntoListview()
    4.         ListView1.Items.Clear()
    5.  
    6.         Dim lvi As ListViewItem
    7.         For Each p As Product In productList
    8.             lvi = New ListViewItem(p.ProductName)
    9.             lvi.SubItems.Add("$" & p.Price.ToString)
    10.  
    11.             ListView1.Items.Add(lvi)
    12.         Next
    13.     End Sub

    Of course, you need to load the products into the productList first, which is what you asked for.

    You can put the product name textboxes into one List(Of TextBox), and the price textboxes into another (for example in the Form_Load event)
    vb.net Code:
    1. Private names As List(Of TextBox)
    2.     Private prices As List(Of TextBox)
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.  
    6.         names = New List(Of TextBox)
    7.         names.AddRange(New TextBox() {txtName1, txtName2, txtName3, ..., txtName11})
    8.  
    9.         prices = New List(Of TextBox)
    10.         prices.AddRange(New TextBox() {txtPrice1, txtPrice2, txtPrice3, ..., txtPrice11})
    11.  
    12.     End Sub

    Then, when the Save button is clicked, you can fill the productList by looping through the lists:
    vb.net Code:
    1. productList = New List(Of Product)
    2.  
    3.  
    4. Dim name As String
    5.         Dim price As Integer
    6.         Dim product As Product
    7.         For i As Integer = 0 To names.Count - 1
    8.             name = names(i).Text
    9.             price = Integer.Parse(prices(i).Text)
    10.  
    11.             product = New Product(name, price)
    12.             productList.Add(product)
    13.         Next
    (This assumes the prices are all integers and not empty, you would be wise to check that first)

    So, now you have the productList collection filled and you can use the first code to put it into the Listview.


    This may seem like a lot of work compared to paul's method, but it is much more easily extended and it is something that occurs in many applications, so understanding this is important.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: creating a collection array from textbox's

    don't forget, if you use Nick's method, every time 1 of your textbox' contents changes, you need to update your list.

    edit: if you intend to use your list in other areas of your project, as Nick suggested.
    Last edited by .paul.; Jan 12th, 2010 at 06:21 PM.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    Paul, I tried usin your method, but I keep on getting errors as listview1 isn't on the same page as I am having users input the values. If i take the "callback" code and put it on that page, then it is dumb and doesnt know where it is calling from:S

  15. #15
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: creating a collection array from textbox's

    Quote Originally Posted by .paul. View Post
    don't forget, if you use Nick's method, every time 1 of your textbox' contents changes, you need to update your list
    True, but judging from the screenshots, the textboxes are on a separate form, which I suppose he is showing as a dialog. So the user would never be able to change a single textbox and see the value change immediately in the listview; he would always have to open the dialog form, set the values, and then click the save button again, so each time the listview would be completely re-newed.

    I also suppose the dialog form would load the current values. In my case he simply needs to fill the TextBoxes with the properties of each Product in the productList, no need to 'parse' them from the listview directly.

    Perhaps I am assuming too much, but that would be logical.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: creating a collection array from textbox's

    [formName].ListView1.Items.Add(New ListViewItem(New String() {products(x), prices(x)}))

  17. #17
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: creating a collection array from textbox's

    Quote Originally Posted by danielpalfrey View Post
    Paul, I tried usin your method, but I keep on getting errors as listview1 isn't on the same page as I am having users input the values. If i take the "callback" code and put it on that page, then it is dumb and doesnt know where it is calling from:S
    If you use my method, you should give the form with the textboxes a property that returned the productList variable. The 'main' form calling that textboxes form could then retrieve the property, and then it would have all the products.

    In the textboxes form:
    vb.net Code:
    1. Public ReadOnly Property Products() As List(Of Product)
    2.    Get
    3.       Return _productList
    4.    End Get
    5. End Property

    Then, in the main form, you can do this in the method that opens the textboxes form
    vb.net Code:
    1. Dim f As New TextboxesForm
    2. f.ShowDialog()
    3.  
    4. LoadIntoListview(f.Products)
    You should then have the LoadIntoListview method accept a list of products as an argument, instead of using the private "_productList" variable as I did.

    Using pauls method, you can do something similar, by creating a property for the Names and one for the Prices.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    I went with pauls method as it was more at my level of skill, will have a play around with your suggestion too though right, now that I can get the items to display in the list view, I now need to save them, so that the next time they use the app, there still usable and they will not need to re enter any of the info.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    The values are set by the user, but they must be permenant, which is why i was trying to go through my.settings, but the problem now is the conversion to an arra y or something else that is capable of storing the list view items. any suggestions wo uld be greatly appreciated.

    Dan

  20. #20
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: creating a collection array from textbox's

    if you used .Pauls method you will have the 2 arrays you could save, with nicks method you have a collection. I have something of a similar project i'm working on, my solution is nearer to nicks but it does use LINQ to get the data (which in my case is from an xml file generated from another app)
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: creating a collection array from textbox's

    after you've loaded your listview using my previous code, you could amalgamate the 2 arrays + serialize the resulting array as binary:

    vb Code:
    1. Dim newArray(1, 10) As String
    2. For x As Integer = 0 To 10
    3.     newArray(0, x) = products(x)
    4.     newArray(1, x) = prices(x)
    5. Next
    6.  
    7. Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    8. Dim bw As New IO.FileStream("savedItems.bin", IO.FileMode.Create)
    9. formatter.Serialize(bw, newArray)
    10. bw.Close()

    then to load your saved values when you open your listview form:

    vb Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     If IO.File.Exists("savedItems.bin") Then
    3.         Dim savedItems(,) As String
    4.         Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    5.         Dim fs As New IO.FileStream("savedItems.bin", IO.FileMode.Open)
    6.         savedItems = DirectCast(formatter.Deserialize(fs), String(,))
    7.         fs.Close()
    8.  
    9.         For x As Integer = 0 To 10
    10.             ListView1.Items.Add(New ListViewItem(New String() {savedItems(0, x), savedItems(1, x)}))
    11.         Next
    12.     End If
    13. End Sub

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    I will look into this when I get home, thanks for the info and you will all bne getting ratings.

  23. #23
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: creating a collection array from textbox's

    If you don't want to rely on a separate file, then you need to use the My.Settings method. You can create two StringCollection settings, one for the names and one for the prices.

    You'd simply do this for saving
    vb.net Code:
    1. My.Settings.Names = New Specialized.StringCollection()
    2. My.Settings.Prices = New Specialized.StringCollection()
    3.  
    4. For Each str As String in names
    5.    My.Settings.Names.Add(str)
    6. Next
    7. For Each int as Integer in Prices
    8.    My.Settings.Prices.Add(int.ToString)
    9. Next

    For loading, very similar
    vb.net Code:
    1. names = New List(Of String)
    2. prices = New List(Of Integer)
    3.  
    4. For Each str As String In My.Settings.Names
    5.    names.Add(str)
    6. Next
    7. For Each str As String In My.Settings.Prices
    8.    prices.Add(Integer.Parse(str))
    9. Next

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    I will also have a look into this method and see which one works more the way I need/want it to.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    I had a play with your code nick, and edited it here and there, got it working rather well, but I might just go with pauls because pauls save the listview itself which means i can add to the list

  26. #26
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: creating a collection array from textbox's

    I don't see how pauls code saves the listview itself. It is saving two arrays, the names and the prices (added together into a single 2d array but that doesn't really matter). My code does just the same. As far as I can see the only difference is that pauls code needs you to store a file somewhere, whereas my code stores it in the settings (alright, that is also a file, but you already have it anyway so why not save to that?). It really doesn't matter which you choose.

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    I am sorry nick, upon second viewing I relised that pauls is pretty much the same as yours. I was mistaken and I appologise. The advantage of saving the list viewitself means that it can always be added to afterwords, but from what I can tell with saving the "collection" you can't really add to the list afterwords. So I am thinking about changing that a little.

    Dan

  28. #28
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: creating a collection array from textbox's

    You don't need to apologize, I was just confused why you thought it was saving the entire listview, because it's not.

    Also, if you have a collection that is displayed in a ListView, you will always need to update the listview when you change the collection; there is no way around that. So you can certainly add new items to the list afterwards, you just have to either reload the listview (from the now modified collection) or add the item to the listview as well.

    The only way you can see a collection in some control change when you modify the collection is by using data binding, which a listview does not support.

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    hmm, well, I went with pauls Idea in the beggining and pauls idea for saving, but if I go back to add even more to the list view, it overwrites what I already have... Is this the downside to using pauls code as apposed to yours??? or is it just in need of a little tweak??? i donno, but I will look into using your code and see which one works best for what I want

    Dan

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    hehe, nop worries I have it figured out. Thanks ever so much for your help everyone you'll be getting rated!!! Dan

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: creating a collection array from textbox's

    Just to inform you all, I had to make a couple of edits to the code displayed on this page. So I'll post that up for you to see

    Now then, this is the code for saving the user set info, and also for putting it in the list view.
    vb Code:
    1. Dim products = (From item In Me.Controls _
    2.                         Where TypeOf item Is TextBox And _
    3.                         item.name.contains("Product") _
    4.                         Order By CInt(item.name.replace("Product", "")) _
    5.                         Select item.text).ToArray
    6.  
    7.         Dim prices = (From item In Me.Controls _
    8.                                 Where TypeOf item Is TextBox And _
    9.                                 item.name.contains("Price") _
    10.                                 Order By CInt(item.name.replace("Price", "")) _
    11.                                 Select item.text).ToArray
    12.  
    13.         For x As Integer = 0 To 10
    14.             Charges_app.Product.Items.Add(New ListViewItem(New String() {products(x), prices(x)}))
    15.         Next
    16.         My.Settings.listprod = New Specialized.StringCollection()
    17.         My.Settings.listprice = New Specialized.StringCollection()
    18.         For Each str As String In products
    19.             My.Settings.listprod.Add(str)
    20.         Next
    21.         For Each int As Integer In prices
    22.             My.Settings.listprice.Add(int.ToString)
    23.         Next

    This is where the info loads (upon form load) to put the info BACK into the listview.
    vb Code:
    1. For x As Integer = 0 To 10
    2.             Product.Items.Add(New ListViewItem(New String() {My.Settings.listprod(x), My.Settings.listprice(x)}))
    3.         Next

  32. #32
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] creating a collection array from textbox's

    don't forget to clear your listview + your stringcollections before you add your items, or you'll end up with duplicated entries

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    Re: [RESOLVED] creating a collection array from textbox's

    Thanks for the reminder paul, I had indeed forgoten to clear it!!!

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