+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Forum Newbie
    Using
    AutoCAD 2011
    Join Date
    Jan 2011
    Posts
    9

    Default Count the number of different values in 8 textbox

    Registered forum members do not see this ad.

    Hello everyone.

    I am developing a project and I would create a command that scrolls the values of 8 textboxes and tell me how many different values among them there are.

    For example, if the values are (1 - 2 - 2 - 1 - 3 - 5 - 1 - 3) the msgbox should say "4" because the values different from each other are (1,2,3,5).

    Should be inserted into a more complex project, but for now I created a simple project to study the problem, but I have several problems.

    I created 8 TextBoxes and a CommandButton.

    This is the code of the commandbutton

    Code:
    Private Sub CommandButton1_Click()
    Me.Hide
    
    Dim txt(1 To 8) As Double
    
    
    txt(1) = TextBox1.Text
    txt(2) = TextBox2.Text
    txt(3) = TextBox3.Text
    txt(4) = TextBox4.Text
    txt(5) = TextBox5.Text
    txt(6) = TextBox6.Text
    txt(7) = TextBox7.Text
    txt(8) = TextBox8.Text
    
    Dim I As Integer
    Dim J As Integer
    Dim contatore As Variant
    
    
    
    contatore = 1
    
    For J = 1 To 8
    For I = J To 8
        If txt(J) <> txt(I) Then
        contatore = contatore + 1
        Else
        contatore = contatore
        End If
    Next I
    Next J
    MsgBox contatore, vbOKOnly, "contatore"
    
    
    Me.Show
    
    
    
    End Sub
    The problem is : it counts how many times the values are different from each other and not how many values are different.

    I hope someone can help me or even recommend a way

  2. #2
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,940

    Default

    You need to step through the list of texts (one-at-a-time), and store the individual numbers to a variable list, *IF* that number does not already belong to the variable list.

    Then report the value of the variable list, the count of different numbers, etc..

    Hope this helps!
    "Potential has a shelf life." - Margaret Atwood

  3. #3
    Forum Newbie
    Using
    AutoCAD 2011
    Join Date
    Jan 2011
    Posts
    9

    Default

    Quote Originally Posted by RenderMan View Post
    You need to step through the list of texts (one-at-a-time), and store the individual numbers to a variable list, *IF* that number does not already belong to the variable list.

    Then report the value of the variable list, the count of different numbers, etc..

    Hope this helps!
    For sure!

    Thank you very much

    I learned to use the collection object, and I wrote this.

    Code:
    Private Sub CommandButton1_Click()
    Me.Hide
    
    Dim txt(1 To 8) As Double
    Dim database As New Collection
    
    
    txt(1) = TextBox1.Text
    txt(2) = TextBox2.Text
    txt(3) = TextBox3.Text
    txt(4) = TextBox4.Text
    txt(5) = TextBox5.Text
    txt(6) = TextBox6.Text
    txt(7) = TextBox7.Text
    txt(8) = TextBox8.Text
    
    Dim I As Integer
    Dim J As Integer
    
    
    
    
    For J = 1 To 8
    For I = 1 To 8
        If txt(J) <> txt(I) Then
                  If Find(database, txt(J)) Then
                  Else
                  database.Add (txt(J))
                    End If
                Else
        End If
    Next I
    Next J
    
    
    
    
    MsgBox database.Count, vbOKOnly, "contatore"
    
    
    Me.Show
    End Sub
    
    Function Find(database As Collection, valore As Double) As Boolean
    For k = 1 To database.Count
    If database.Item(k) = valore Then
    Find = True
    Exit For
    End If
    Next
    End Function
    Now it works!

  4. #4
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,940

    Default

    Quote Originally Posted by lubracali View Post
    Thank you very much

    You're welcome.
    "Potential has a shelf life." - Margaret Atwood

  5. #5
    Senior Member Jeff H's Avatar
    Using
    MEP 2011
    Join Date
    Oct 2010
    Posts
    172

    Default

    Another thing you can do is most .Net collections have a contains method thats returns a boolean

    This iterates through all the textboxes and adds only if it is not already in the collection


    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
            Dim lst As New List(Of Integer)
     
            For Each ctrl As Control In Me.Controls
     
                If TypeOf ctrl Is TextBox Then
     
                    If Not lst.Contains(ctrl.Text) Then
                        lst.Add(ctrl.Text)
                    End If
     
     
                End If
     
     
            Next
     
         
     
     
            For Each element In lst
                MsgBox(element)
            Next
     
        End Sub
    You can also find your answers @ theswamp.org

  6. #6
    Forum Newbie
    Using
    AutoCAD 2011
    Join Date
    Jan 2011
    Posts
    9

    Default

    Quote Originally Posted by Jeff H View Post
    Another thing you can do is most .Net collections have a contains method thats returns a boolean

    This iterates through all the textboxes and adds only if it is not already in the collection


    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
            Dim lst As New List(Of Integer)
     
            For Each ctrl As Control In Me.Controls
     
                If TypeOf ctrl Is TextBox Then
     
                    If Not lst.Contains(ctrl.Text) Then
                        lst.Add(ctrl.Text)
                    End If
     
     
                End If
     
     
            Next
     
         
     
     
            For Each element In lst
                MsgBox(element)
            Next
     
        End Sub
    I tried this code, but the only element that answers to the method "contains" is the listbox.

    I think it is due to vba in autocad, which is different from vb.net

  7. #7
    Super Member SEANT's Avatar
    Using
    AutoCAD 2012
    Join Date
    Aug 2005
    Location
    Rhode Island
    Posts
    1,968

    Default

    Registered forum members do not see this ad.

    VBA could use the Windows.Scripting.Dictionary object. That has a similar Dictionary.Exists to test if it is already present.

    Though, it may be easier to just take RenderMan’s suggestion, or continue with Collections. I haven’t used Windows.Scripting.Dictionary in a VBA project but I doubt it would have much better performance over a custom “Exist” routine.

    Dictionary objects do benefit from additional storage. If TextBox.Text were set as the Key, then Value could be incremented to keep track of the number of times it appears.
    Last edited by SEANT; 21st Jan 2011 at 01:18 pm. Reason: added comment

Similar Threads

  1. count the number a given text letters e.g. 3No. H's
    By stockers in forum AutoLISP, Visual LISP & DCL
    Replies: 33
    Last Post: 4th Oct 2010, 10:48 am
  2. Titles instead of number values in dynamic blocks
    By tonyd1000 in forum Tutorials & Tips'n'Tricks
    Replies: 2
    Last Post: 15th Apr 2010, 07:32 am
  3. How to Count number of ocurances of a letter in block text
    By stockers in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 5
    Last Post: 23rd Dec 2009, 08:45 am
  4. Count the number of times a text is repeated
    By myself in forum AutoCAD General
    Replies: 3
    Last Post: 5th Aug 2009, 04:43 pm
  5. How to convert Latitude / Longitude values in to Northing / Easting values in meters
    By Chenna Siva Koteswara rao in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 2
    Last Post: 28th Dec 2008, 11:09 pm

Bookmarks

Posting Permissions

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