1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Help with Realbasic, Arrays, PopupMenu and a Textbox

Discussion in 'Mac Programming' started by IntelliUser, Nov 7, 2009.

  1. macrumors 6502

    #1
    Hello,

    I'm trying to do something but I need some help.

    Basically I have a PopupMenu, and when one of the values is selected, some other values are displayed in 3 text fields.

    Now I want to do something like this

    Array that contains PopupMenu options, containing " ", "a" and "b"
    Array containing the values displayed in the Text field, containing " ", " ", " ", "bla", "blabla", "blablabla", "da","dada", "dadada".

    Now I want that when a user selects " ", the first 3 values of the second array appear respectively in 3 Text fields. If he selects "a", the second 3 values and so on.

    Note: I want to avoid doing an If statement for each possibility, so that I can just add more values to the two arrays in the future and have the program do the rest.
    How can I do it?
     
  2. macrumors 68000

    #2
    I don't know RB, but it sounds like you want to use an index value.

    If the Popup has the option of returning a numeric value (or deriving it somehow) for the position selected, then that can be used to use gain access to the second array. Assuming your array positions start at zero, it might be 1 in RB, here is what you want. If RB uses 1, I'll leave it to you to figure out the math.

    Here is some pseudo code to get the idea. Notice how the formula for the three assignments repeats.

    Code:
    popupvalue returns 0, text1 = arrary2[popupvalue * 3]
    popupvalue returns 0, text2 = arrary2[popupvalue * 3] + 1
    popupvalue returns 0, text3 = arrary2[popupvalue * 3] + 2
    
    popupvalue returns 1, text1 = arrary2[popupvalue * 3]
    popupvalue returns 1, text2 = arrary2[popupvalue * 3] + 1
    popupvalue returns 1, text3 = arrary2[popupvalue * 3] + 2
    
    popupvalue returns 2, text1 = arrary2[popupvalue * 3]
    popupvalue returns 2, text2 = arrary2[popupvalue * 3] + 1
    popupvalue returns 2, text3 = arrary2[popupvalue * 3] + 2
    
    So instead of if statements, you have

    Code:
    text1 = arrary2[popupvalue * 3]
    text2 = arrary2[popupvalue * 3] + 1
    text3 = arrary2[popupvalue * 3] + 2
    
    Another way to do this is with a dictionary. The dictionary key would equal your popup values and the dictional value would be an ordered array with your replacement values. This is may be less prone to errors.
     
  3. macrumors 68030

    Les Kern

    #3
    Consider going to the RB forums, where there are quite a few people who I'm sure can help. Props to MacRumors users and any answers you get, but that RB forum is at the source.
     
  4. macrumors 6502

    #4
    That is what I have been thinking and trying to do for hours without success, I can't seem to find out how to get the current position from PopupMenu.
    Have't got much experience with dictionaries, but I suppose it would require the popupmenu value, right?

    I can't seem to be able to register there, they won't accept my email, even though I downloaded all the demos available on the site...
     
  5. macrumors 68000

    #5
    I'm not sure but it maybe listindex you are looking for. As in
    Code:
    popupvalue.listindex
    Surely RB has a Help menu. Look there for answers.
     
  6. macrumors 6502

    #6
    This code doesn't work completely:

    Code:
      Dim a() as String
      a = Array ("A", "B", "C")
      
      TextField1.Text = a(PopupMenu1.ListIndex)
      
    TextField1 gets stuck at A. Any idea?
     
  7. macrumors 68000

    #7
    As I said, I'm not an RB programmer. So, no, I have no further ideas.

    Typically when things are not turning out as expected, a common practice is to debug he the code. The simplest technique is to print out the value of concern to see what it is returning.

    If you are learning RB, I suggest you get a book. If you can't afford one, try the library.
     
  8. macrumors regular

    #8
    Where are you putting this code?

    When I put it into the PopupMenu1's change event it works just fine.

    Although I did have to rename EditField1 to TextField1 to match your code.
     
  9. macrumors 6502

    #9
    You're right, I was putting it in the window. Silly me.

    PS: EditField1 is named TextField 1 in the latest version(s?)
     

Share This Page