Results 1 to 4 of 4

Thread: Should i use a 2 dimensional arrary for the solution?

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Question

    Hello!

    I am currently workin gon a project that lets the user enter spanish words, and it lists them in a listbox and then if the user clicks on the words it will tell them the definition, And i'm having troubles combining the 2 array's, see what i did was first made the program,
    with just a dynamic array, When the user clicks on the cmd button,
    it will ask: How many words do u wish to enter? that sets up the dimensions, and then i use a for next loop to loop threw all of them and adds it to the listbox, and it works fine but who needs that?
    so i am trying to add a defintion thing too! here's the code i have so far:
    Code:
    Dim SpanishWords()
    Option Explicit
    
    Private Sub cmdClose_Click()
    Unload Form1
    
    End Sub
    
    Private Sub cmdEnter_Click()
    Dim WordCount As Integer        'gets number of dimensions
    Dim Words As String                 'gets user to enter words to list box
    Dim i As Integer                          'counter
    Dim Def As String                         'inputbox that prompts user for def of word
    Dim WordCount2 As Integer            'Makes 2 dimensional array for def also
    
        WordCount = InputBox("Type in the number of words you wish to enter to.", "Spanish Words")
            ReDim SpanishWords(1 To WordCount)
        WordCount2 = InputBox("Enter the how many defintions you will be adding to words.")
        
        ReDim Preserve SpanishWords(1 To WordCount, 1 To WordCount2)
        
                For i = 1 To WordCount
                    Words = InputBox("Enter the " & WordCount & " new vocabulary words.")
                    Def = InputBox("Enter Defintion of spanish word")
                    
                    SpanishWords(i) = Words
                    List1.AddItem (Words)
                Next i
    End Sub
    
    Private Sub List1_Click()
        Select Case List1.ListIndex
            
    End Sub
    see i don't even know if i was suppose to use an array or
    not, but i just guessed that, so can someone please tell me what im doing wrong? what i know is wrong is that i
    wanted it to work out like the first, after the user enters
    the word a inputbox comes up and asks the user for def of the word they just enetered, and it goes on to the next and
    next. but i don't know what to do so i can put it in the array as well, so when the user clicks the word in teh list
    box a msg box or anotehr form comes up with the def of the word. sorry if i'm boring you but this is all the better i
    can explain it Thanks for listening

    [Edited by struntz on 08-01-2000 at 12:36 AM]

  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    A User Defined Type would be cleaner


    Code:
    Option Explicit
    Private Type struntz
      Word As String
      Definition As String
    End Type
    
    Dim SpanishWords() As struntz
    
    Private Sub cmdEnter_Click()
    Dim i As Integer
    Dim newWord  As String
    Dim Def As String
    
    i = UBound(SpanishWords()) + 1
    
    ReDim Preserve SpanishWords(i)
        
    
      newWord = InputBox("Enter a  new vocabulary word.")
      Def = InputBox("Enter Defintion of '" & newWord & "'")
      
      SpanishWords(i).Word = newWord
      SpanishWords(i).Definition = Def
      
      List1.AddItem newWord
    
    End Sub
    
    Private Sub Form_Load()
    ReDim SpanishWords(0)
    End Sub
    
    Private Sub List1_Click()
        Text1 = SpanishWords(List1.ListIndex + 1).Definition
            
    End Sub
    Mark
    -------------------

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    struntz, I have written this piece of code that accepts no. of words and no. of definitions for each word from the user, accepts all the words with their definitions and displays the words in a list box. Whenever the user clicks on any word, all its definitions appear in a message box. I hope this was what you were looking for.


    Add a new form to your project, add a listbox to it and put the following code in it. Set the StartUpObject of your project to this form and run the project.



    Code:
    Option Explicit
    
    Dim MyArray() As String       'Array to hold words and definitions
    Dim NoWords As Integer, NoDef As Integer     'No. of words and no. of definitions for each word
    Dim I As Integer, J As Integer      'Loop variables
    
    Private Sub Form_Activate()
    
    List1.Clear    'Clear list for fresh list of words
    
    NoWords = CInt(InputBox("Input no. of words you want to enter", "", 1))    'Accept no. of words from user
    NoDef = CInt(InputBox("Input no. of definitions for each word", "", 1))    'Accept no. of definitions from user
    
    ReDim MyArray(NoWords, NoDef + 1)      'Redim the array as per new dimensions
    
    For I = 1 To NoWords          'Outer loop iterates through words
       MyArray(I, 1) = InputBox("Enter " & CStr(I) & "th Word", "", "Default1")
       List1.AddItem (MyArray(I, 1))
       
       For J = 1 To NoDef         'Inner loop iterates through definitions
          MyArray(I, J + 1) = InputBox("Enter definition for " & MyArray(I, 1))
       Next
       
    Next
    
    End Sub
    
    Private Sub List1_Click()
    
       Dim Q As String
       Q = ""
       
       For J = 1 To NoDef      'Iterates through definitions
          Q = Q & MyArray(List1.ListIndex + 1, J + 1) & vbCrLf     'ListIndex + 1 is index of current word in array
       Next
       
       MsgBox Q
       
    End Sub

    Does it work?

    Mark, I think you have worked more on your solution than I have worked on mine. That's why maybe you have come up with a better one.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Talking Thanks!

    Hey Thanks, yeah that is what iw as looking for i just needed to know what i should do! thanks again!

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