Results 1 to 16 of 16

Thread: Clearing a Collection

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    Clearing a Collection

    How do you clear a collection consisting of labels and text boxes?

    me.collection1.clear
    or
    Clear(collection1)

    do not work. Thanks.

  2. #2
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    It is kind of odd that Collections don't have the Clear() method. Oh well, just do..

    collection1 = New Collection()


  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Maybe this will work

    Code:
    Collection = null;
    Dont gain the world and lose your soul

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Ok, I STILL cannot get the stupid clear collection thing. Here is exactly what I need to do.

    When the user clicks the ClearButton,
    The InputTextBox should clear along with all of the display labels
    The focus should return to the InputTextBox and
    The ClearLabel should be disabled.
    The Clear Routine must be written using collections and a For/Each loop.
    To do this, create a collection of all display labels and use the loop to clear them.
    Last edited by andrew104; Dec 5th, 2002 at 12:25 AM.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can always just loop through:
    VB Code:
    1. '_Col is the collection
    2.  
    3.     Public Sub Clear()
    4.         Dim cnt As Integer
    5.         Dim cntMax As Integer
    6.  
    7.         cntMax = _Col.Count
    8.         For cnt = cntMax To 1 Step -1
    9.             _Col.Remove(cnt)
    10.         Next
    11.  
    12.     End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    YES! that worked wonderfully. Thank you very much. I have a question for you then.... I've finally got the program complete except for probably the easiest part - count the total number of characters (alpha, A-Z) entered in the text box. I've had a lot of suggestions, but they either don't work, or I don' t understand them. Thanks for the help.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Dim re As New Regex("[a-zA-Z]")
    2.         Me.Text = re.Matches(TextBox1.Text).Count

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    I don't understand your code at all Edneeis. It doesn't work either. It says "regex", whatever that is is not a defined type. And "Me.type" is invalid as well.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Sorry I forgot the imports:

    Imports System.Text.RegularExpressions

    Or use this code instead:

    Dim re As New System.Text.RegularExpressions.Regex("[a-zA-Z]")
    Me.Text = re.Matches(TextBox1.Text).Count

    Where is Me.Type?

    Its Me.TEXT

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Regex is a class that does all sorts of string functions and matching. There I am having it search for all characters A-Z both lower and upper case. The Me.Text bit just puts the character count in the form caption. You'll need to do whatever you want with it, but re.Matches(TextBox1.Text).Count will return the count and of course instead of textbox1 you'll have to put the string you want to count the characters from.

    The Regex class is kind of a strange one but its very powerful.

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Aha! I finally understand it. Sorry for being so stupid. One more question though... how can I make it detect both cases? I see you have done it for all characters (A-Z), but how do I do that for just "a" and "st", etc... ?

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You aren't stupid just learning something new. To find other characters or set of characters just declare a new regex and pass in the characters you want to find. If it is a list of characters then put it in [] otherwise don't.

    So Regex("[st]") will find any s in the source and any t as individual charactes.
    Or Regex("st") will find any 'st' in the source as a set of characters together. It uses the literal.

    VB Code:
    1. Dim re As New Regex("a")
    2.         Me.Text = re.Matches(TextBox1.Text).Count
    3.         re = New Regex("st")
    4.         Me.Text &= " " & re.Matches(TextBox1.Text).Count

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    NEVERMIND - GOT IT!

    ONE MORE QUESTION! I guess they never stop. How can I disable the backspace key?

  14. #14

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Here's what I've done for it to count upper and lower. I hope this is correct? It seems to work ok...

    VB Code:
    1. 'count the number of times a is typed
    2.         Dim acount As New System.Text.RegularExpressions.Regex("[aA]{1}")
    3.         ACountLabel.Text = acount.Matches(InputTextBox.Text).Count

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Looks fine, but you don't need the {1} unless you don't want to count 'aa' otherwise nevermind.

  16. #16
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    The only way I can see to "disable" the backspace key is to set KeyPreview of the form to true and then catch the KeyPress event of the control and each time check if it is the backspace key that is pressed. If it is you set the e.Handled to true and the control will not recieve the backspace.

    I think the keycode for backspace is 8....

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