VS 2008 [RESOLVED] Class Help-VBForums
Results 1 to 24 of 24

Thread: [RESOLVED] Class Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Resolved [RESOLVED] Class Help

    Hi,

    I have a class called 'Box' which inherits 'TextBox' and has one string property.

    I've added multiples of the class to the Form, and I want to then display each of the strings in each 'Box' in textbox1.

    Obviously, each new class is called, Box1, Box2, Box3 etc, so I know I can access the string with something like:

    textbox1.text = Box1.text + Box2.text + Box3.text

    But, if I start adding more 'Box'es to the form, how can i automatically 'address' the nex 'Box'es?

    I thought about a For, Each approach but couldn't get it going, any ideas?

    Thanks,

    Aaron.

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

    Re: Class Help

    here's how. this uses a property called NewProperty + if you change that to your property name it'll work:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     TextBox1.Lines = Me.Controls.OfType(Of Box).Select(Function(b) b.NewProperty).Reverse.ToArray
    3. End Sub

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,112

    Re: Class Help

    I would put all the controls into a List (of Box). If you add them in the right order (adding 1, then 2, then 3, and so on), the index into the List is the proper class...with the slight exception that you have left out a Box0, so maybe you either need to add one, or the boxes will be index+1

    EDIT: I was posting as .Paul. was posting, and hadn't seen that. I would still go with a List (of Box), in spite of what he said, but let me clarify it a bit: I have recently done something like this. Once the number of controls got up to some level, performance suffered. Therefore, I built all the controls, put them into a list, and toggled the visibility of the ones I wanted rather than creating and destroying objects. This greatly improved performance, and is something you might consider.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    Thanks guys,

    .paul. i used your method, that's just what i was after, thanks.

    how does that function work?

    thanks,

    aaron.

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

    Re: Class Help


  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    Thanks for the link, although, I tried the code in my project at work, which is where i really want to get using it; and i get the following error:

    'oftype' is not a member of 'system.windows.forms.control.controlcollection'.

    Any ideas?

    Ta,

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

    Re: Class Help

    which version of vb.net are you using at work?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    Same as at home, vb 2008, only at home i'm on Vista, and work is XP? It does work on a new project on the work pc, but this particular project i'm using wasn't written by me...

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

    Re: Class Help

    which version of the .Net framework does your work app target?
    to work, it must be 3.5

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    is that under references>system.core?

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

    Re: Class Help

    Project-->Properties-->Compile-->Advanced Compile Options-->Target CPU

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

    Re: Class Help

    Sorry, but I don't like paul's solution. A couple reasons:

    1. There's no way to control the order in which the boxes are traversed and thus which order the lines in the TextBox appear, beside the order in which the buttons were added. Should you ever want to swap box1 and box2, the only way would be to remove them and add them in the reverse order back to the form.

    2. The OP obviously doesn't understand how to approach this common problem and I think we can therefore safely assume his VB.NET skills aren't very developed yet. Throwing a one-liner at him is not going to make him understand, at all. He will copy/paste it and never think about it again, until he needs a similar solution to a different problem. Then he still doesn't know how to tackle the problem. He hasn't learned anything.


    What I would suggest is this:

    1. Create a List(Of Box) that acts as a collection of Box objects. You can add the boxes to this list manually (in code) in an appropriate place, such as the Form_Load event. You can thus control the order of the Box objects here as well, by controlling the order in which you add them.

    2. Then, for each (there you go ) Box in the list, append the NewProperty property to the TextBox's Text property.

    vb.net Code:
    1. Private boxes As List(Of Box)
    2.  
    3. Private Sub Form1_Load(...) Handles MyBase.Load
    4.     'Create new List and add boxes
    5.     boxes = new List(Of Box)()
    6.     boxes.Add(Box1)
    7.     boxes.Add(Box3)
    8.     boxes.Add(Box4)
    9.     boxes.Add(Box2)
    10.     '...
    11.    
    12. End Sub
    13.  
    14. Private Sub Button1_Click(...) Handles Button1.Click
    15.     TextBox1.Clear()
    16.    
    17.     For Each b As Box In boxes
    18.         TextBox1.Text &= b.NewProperty
    19.         TextBox1.Text &= Environment.NewLine
    20.     Next
    21. End Sub

    For step 2, there are better alternatives. Now you are adding strings together in a loop many times, and each time you do this a new String has to be created (this is how strings work) which can bog down memory if you do it way too much. It will not be a problem at all in this case but it's something to keep in mind. A better solution is to use a StringBuilder to which you append the text, and then set the TextBox1.Text to the StringBuilders ToString function, which finally adds all the strings together 'in one go' (without having the same problem).

    Another alternative is to add each NewProperty to an array of strings and assign that to the Lines property.
    vb.net Code:
    1. Private Sub Button1_Click(...) Handles Button1.Click
    2.     Dim texts As New List(Of String)()
    3.     For Each b As Box In boxes
    4.         texts.Add(b.NewProperty)
    5.     Next
    6.    
    7.     TextBox1.Lines = texts.ToArray()
    8. End Sub
    To shorten this a little, you can use LINQ, which is what paul was using for his one-liner. You need .NET Framework 3.5 or up for this though:
    vb.net Code:
    1. Private Sub Button1_Click(...) Handles Button1.Click
    2.     Dim texts = From b In boxes Select b.NewProperty   
    3.     'OR
    4.     'Dim texts = boxes.Select(Function(b) b.NewProperty)
    5.     TextBox1.Lines = texts.ToArray()
    6. End Sub
    paul made one final shortcut by not explicitly creating his own list of Boxes but using the Controls collection of the form, which already contains the boxes (along with all other controls). He uses the OfType method to select only the controls of type Box, then selects their NewProperty just like I did.
    Now at least you can understand where paul's code came from.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    Thanks for that, it makes sense and it's leading me on to the next part of my project, in that now I can access all of the controls in one go, how can I pass information back to each control depending on their names, without manually having to code each control?

    i.e., if i have box1, box2, can i pass set a different value in each of their properties, say with a for-next loop?

    Thanks for all the help so far,

  14. #14

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    Sorry, I meant something like this:

    Code:
    for i as integer = 0 to 9
    
    box(i).text = i
    
    next
    I know that you could do something like this in VB6, but I know it doesn't work in vb.net,

  16. #16

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    Got it working with this, the only thing is, after run-time, the text property stays as what was previous - is this normal?
    vb Code:
    1. Private boxes As List(Of Box)
    2.         boxes = New List(Of Box)
    3.         boxes.Add(box1)
    4. '
    5.         For i As Integer = 0 To 0
    6.             boxes(i).text = "hello"
    7.         Next

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

    Re: Class Help

    vb Code:
    1. For i As Integer = 0 To 0
    ???

    try this:

    vb Code:
    1. For i As Integer = 0 To boxes.count - 1

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    I just put the one box, which is why it went 0 to 0.

    With this approach, I would obviously have to add each box one at a time, is there a way to add them all in automatically?

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

    Re: Class Help

    as well as the .add method, the generic list also has an .addrange method, where you can add a collection of items:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim boxes As New List(Of Box)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         boxes.AddRange(New Box() {Box1, Box2, Box3})
    7.     End Sub
    8.  
    9. End Class

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    95

    Re: Class Help

    could you do the whole lot in one go, without having to specify box1, box2, etc....

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

    Re: Class Help

    yes. you can add: me.controls.oftype(of box).toarray, which will add them all, but as other members have pointed out in this thread, the box's might be added in a different order to what you're expecting

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

    Re: Class Help

    Unless you have hundreds of boxes (tip: you shouldn't) I see no reason why you'd want to avoid typing it out. It will take you a couple minutes if you type really slowly, at least much less time than waiting for a reply here. In the mean time you have all the control you want, you control the order of the boxes manually.

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

    Re: Class Help

    how many box's do you have? do you add them at runtime or do they exist at design time?

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.