+ Post New Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
Coding Thread, VB.net Setting colour for array of textboxes in Coding and Web Development; I have a dynamic array of textboxes (lets say 3 for this example though). I want to iterate through each ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242

    VB.net Setting colour for array of textboxes

    I have a dynamic array of textboxes (lets say 3 for this example though). I want to iterate through each and set the backColor property. I'm trying:

    Code:
    Dim value as TextBox
    For Each value In txtMain 'txtMain is the array of TextBoxes
    value.BackColor = My.Settings.frmMainBackColour
    Next
    I have checked my settings and a default BackColour value exists so this is the cause of the following error at run-time:

    Object reference not set to an instance of an object

    I think I've done this before (like yesterday haha) but my mind is on a go slow and I can't remember how I might have overcome it.

    Any ideas?

  2. #2
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    604
    Thank Post
    102
    Thanked 142 Times in 123 Posts
    Rep Power
    51
    If i think right shouldn't it be

    Code:
    Dim value as New TextBox
    just a thought

    Mark

  3. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Quote Originally Posted by sister_annex View Post
    If i think right shouldn't it be

    Code:
    Dim value as New TextBox
    just a thought

    Mark
    Didn't make a difference

  4. #4
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    604
    Thank Post
    102
    Thanked 142 Times in 123 Posts
    Rep Power
    51
    hmmm,

    do you have any more code so I put it in to context?


    Cheers

  5. #5
    Jamo's Avatar
    Join Date
    Jan 2009
    Posts
    1,355
    Thank Post
    66
    Thanked 175 Times in 147 Posts
    Rep Power
    60
    Shouldn't you use one of the containers in VB for that rather than an array? I believe arrays are fixed length in VB so if all of the values aren't populated you will see that error

    Generic Lists in VB.NET - ForEach, FindAll, and Sort Generic Methods

    That way you can grow and shrink the list as required.

    Dim listOfTextboxes As List(Of Textbox)

    listOfTextboxes.Add(textbox1);
    listOfTextboxes.Add(textbox2);
    listOfTextboxes.Add(textbox3);
    listOfTextboxes.Add(textbox4);

    Dim value As Textbox

    For Each value In listOfTextboxes

    value.etc etc

    Next
    Last edited by Jamo; 23rd March 2010 at 04:09 PM.

  6. #6
    RinSewand's Avatar
    Join Date
    Apr 2009
    Location
    Wakefield
    Posts
    264
    Thank Post
    14
    Thanked 46 Times in 43 Posts
    Rep Power
    19

  7. #7

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,578
    Thank Post
    1,473
    Thanked 1,227 Times in 835 Posts
    Rep Power
    753
    It might be that you haven't specified 'Next value'

    Alternatively, try:

    Code:
    Dim x as long
    For x=LBound(txtMain) to UBound(txtMain)
    txtMain(x).BackColor = My.Settings.frmMainBackColour
    Next x

  8. #8

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Quote Originally Posted by JoshJohnson View Post
    It might be that you haven't specified 'Next value'

    Alternatively, try:

    Code:
    Dim x as long
    For x=LBound(txtMain) to UBound(txtMain)
    txtMain(x).BackColor = My.Settings.frmMainBackColour
    Next x
    It wasn't the value bit. Trying the other now...

  9. #9

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Quote Originally Posted by JoshJohnson View Post
    Code:
    Dim x as long
    For x=LBound(txtMain) to UBound(txtMain)
    txtMain(x).BackColor = My.Settings.frmMainBackColour
    Next x
    Same error

  10. #10

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,578
    Thank Post
    1,473
    Thanked 1,227 Times in 835 Posts
    Rep Power
    753
    Quote Originally Posted by Hightower View Post
    It wasn't the value bit. Trying the other now...
    Quote Originally Posted by Hightower View Post
    Same error
    Hmmm I like a challenge, I'm gonna try and recreate this myself and see if I can solve it, I'll let you know if I come up with a solution...

  11. Thanks to LosOjos from:

    Hightower (23rd March 2010)

  12. #11
    Jamo's Avatar
    Join Date
    Jan 2009
    Posts
    1,355
    Thank Post
    66
    Thanked 175 Times in 147 Posts
    Rep Power
    60
    How are you creating this dynamic array? As far as I know VB doesn't have the ability to have dynamic arrays with out using containers such as the one I suggested without keeping track of array bounds etc which gets very messy.

  13. #12

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Code:
    Dim txtMain(1) as TextBox
    Dim iNumTxt as Integer = 3
    ReDim txtMain(iNumTxt)
    
    iNumTxt = 5
    ReDim txtMain(iNumTxt)
    Then to create each text box I need:

    Code:
    iNumTxt += 1
    ReDim txtMain(iNumTxt)
    
    txtMain(variableHoldingNumberOfLatestAddedBox).Name = "GIVE IT A NAME"
    txtMain(...).Text = "GIVE IT SOME TEXT"
    txtMain(...).Properties = "ADD WHATEVER PROPERTIES YOU WANT HERE"
    Me.Controls.Add(txtMain)
    This method works great so far, with the exception being this little stop sign I've come across. Will be some way around it though.

  14. #13
    Jamo's Avatar
    Join Date
    Jan 2009
    Posts
    1,355
    Thank Post
    66
    Thanked 175 Times in 147 Posts
    Rep Power
    60
    The List<Of T> example I gave just now does what you want just wrapped up in an STL container style format without the extra vars.


    Forget that last bit i just deleted I am thinking of C++ vectors! You need to use the List.Item(0) etc to access the items.
    Last edited by Jamo; 23rd March 2010 at 04:41 PM. Reason: VB is not C++..... :p

  15. #14
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    604
    Thank Post
    102
    Thanked 142 Times in 123 Posts
    Rep Power
    51
    Quote Originally Posted by Hightower View Post
    Code:
    Dim txtMain(1) as TextBox
    Dim iNumTxt as Integer = 3
    ReDim txtMain(iNumTxt)
    
    iNumTxt = 5
    ReDim txtMain(iNumTxt)
    Then to create each text box I need:

    Code:
    iNumTxt += 1
    ReDim txtMain(iNumTxt)
    
    txtMain(variableHoldingNumberOfLatestAddedBox).Name = "GIVE IT A NAME"
    txtMain(...).Text = "GIVE IT SOME TEXT"
    txtMain(...).Properties = "ADD WHATEVER PROPERTIES YOU WANT HERE"
    Me.Controls.Add(txtMain)
    This method works great so far, with the exception being this little stop sign I've come across. Will be some way around it though.

    why not,

    Code:
    for x as integer = 0 to inumtxt 'or your maximum number
    dim t as new textbox
    
    with t
    .name = "name"
    .text = "text"
    .properties = "etc"
    end with
    
    me.controls.add(t)
    next
    i'm sure i have done it like that before

    Mark

  16. #15
    Jamo's Avatar
    Join Date
    Jan 2009
    Posts
    1,355
    Thank Post
    66
    Thanked 175 Times in 147 Posts
    Rep Power
    60
    We need to hold onto the references for later use in changing the textbox colour basically.



SHARE:
+ Post New Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. RAID5 Array 'failed'
    By sidewinder in forum Hardware
    Replies: 10
    Last Post: 24th February 2010, 11:03 AM
  2. Colour Scanning Pen: Colour Picker
    By DaveP in forum IT News
    Replies: 0
    Last Post: 25th January 2010, 09:05 PM
  3. Split into array ?
    By mac_shinobi in forum Coding
    Replies: 5
    Last Post: 13th August 2009, 01:23 PM
  4. Moving to a RAID array
    By binky in forum Hardware
    Replies: 7
    Last Post: 28th February 2009, 04:48 PM
  5. Anyone Moved a Raid array.
    By K.C.Leblanc in forum Hardware
    Replies: 10
    Last Post: 15th November 2007, 06:52 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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