lubracali Posted January 20, 2011 Share Posted January 20, 2011 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 Private Sub CommandButton1_Click() Me.Hide Dim txt(1 To 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( = 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 Quote Link to comment Share on other sites More sharing options...
BlackBox Posted January 20, 2011 Share Posted January 20, 2011 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! Quote Link to comment Share on other sites More sharing options...
lubracali Posted January 20, 2011 Author Share Posted January 20, 2011 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. Private Sub CommandButton1_Click() Me.Hide Dim txt(1 To 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( = 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! Quote Link to comment Share on other sites More sharing options...
BlackBox Posted January 20, 2011 Share Posted January 20, 2011 Thank you very much You're welcome. Quote Link to comment Share on other sites More sharing options...
Jeff H Posted January 20, 2011 Share Posted January 20, 2011 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 [color=blue]Private[/color] [color=blue]Sub[/color] Button1_Click([color=blue]ByVal[/color] sender [color=blue]As[/color] System.[color=#2b91af]Object[/color], [color=blue]ByVal[/color] e [color=blue]As[/color] System.[color=#2b91af]EventArgs[/color]) [color=blue]Handles[/color] Button1.Click [color=blue]Dim[/color] lst [color=blue]As[/color] [color=blue]New[/color] [color=#2b91af]List[/color]([color=blue]Of[/color] [color=blue]Integer[/color]) [color=blue]For[/color] [color=blue]Each[/color] ctrl [color=blue]As[/color] [color=#2b91af]Control[/color] [color=blue]In[/color] [color=blue]Me[/color].Controls [color=blue]If[/color] [color=blue]TypeOf[/color] ctrl [color=blue]Is[/color] [color=#2b91af]TextBox[/color] [color=blue]Then[/color] [color=blue]If[/color] [color=blue]Not[/color] lst.Contains(ctrl.Text) [color=blue]Then[/color] lst.Add(ctrl.Text) [color=blue]End[/color] [color=blue]If[/color] [color=blue]End[/color] [color=blue]If[/color] [color=blue]Next[/color] [color=blue]For[/color] [color=blue]Each[/color] element [color=blue]In[/color] lst MsgBox(element) [color=blue]Next[/color] [color=blue]End[/color] [color=blue]Sub[/color] Quote Link to comment Share on other sites More sharing options...
lubracali Posted January 21, 2011 Author Share Posted January 21, 2011 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 [color=blue]Private[/color] [color=blue]Sub[/color] Button1_Click([color=blue]ByVal[/color] sender [color=blue]As[/color] System.[color=#2b91af]Object[/color], [color=blue]ByVal[/color] e [color=blue]As[/color] System.[color=#2b91af]EventArgs[/color]) [color=blue]Handles[/color] Button1.Click [color=blue]Dim[/color] lst [color=blue]As[/color] [color=blue]New[/color] [color=#2b91af]List[/color]([color=blue]Of[/color] [color=blue]Integer[/color]) [color=blue]For[/color] [color=blue]Each[/color] ctrl [color=blue]As[/color] [color=#2b91af]Control[/color] [color=blue]In[/color] [color=blue]Me[/color].Controls [color=blue]If[/color] [color=blue]TypeOf[/color] ctrl [color=blue]Is[/color] [color=#2b91af]TextBox[/color] [color=blue]Then[/color] [color=blue]If[/color] [color=blue]Not[/color] lst.Contains(ctrl.Text) [color=blue]Then[/color] lst.Add(ctrl.Text) [color=blue]End[/color] [color=blue]If[/color] [color=blue]End[/color] [color=blue]If[/color] [color=blue]Next[/color] [color=blue]For[/color] [color=blue]Each[/color] element [color=blue]In[/color] lst MsgBox(element) [color=blue]Next[/color] [color=blue]End[/color] [color=blue]Sub[/color] 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 Quote Link to comment Share on other sites More sharing options...
SEANT Posted January 21, 2011 Share Posted January 21, 2011 (edited) 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. Edited January 21, 2011 by SEANT added comment Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.