Announcement

Collapse
No announcement yet.

Can we append arrays using ARRAY ASSIGN statements?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Michael Mattias
    replied
    You are using a 0 based array to populate a Combobox Combobox items are 1 based.
    Slight Correction:

    'DDT' uses one-based indexing for statements used to manage its list-type controls such as a COMBOBOX . (Or LISTBOX, or LISTVIEW). Note I have capitalized the control names because technically these are PB-proprietary controls.

    The underlying list-type controls themselves - when sending command messages using the WinApI - use zero-based indexing.

    MCM

    Leave a comment:


  • Tim Lakinir
    replied
    Thank you Sir Biggs
    I will try to keep in line with the original question in all future postings. Thanks Everyone

    Leave a comment:


  • Jim Fritts
    replied
    Yup

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Rodney Hicks View Post
    Well, Steve, someone may have hundreds or thousands of files to scan to get a list, of countries in this case, and they may want to add the names at the end of each document scanned, for reasons which I know not. They may be keeping other data as well, such as quantity of 'countries' found in each document. The OP may have a reason to approach the issue in this manner, where others may have chosen a different solution, such as yours. Besides, it gave Dave Biggs a chance to give a good lesson (to me anyway).
    Yes, it's probably best to keep the original question and Thread Subject in mind.

    Tim asked about adding elements to an array. And specifically he wanted to know about using Array Assign.
    The fact that he used a small array of countries as an example is completely irrelevant.

    Dave provides an excellent piece of education code in answering that query.

    Leave a comment:


  • Steve Hutchesson
    replied
    Rod,

    The country count in the world is under 200, a trivial task to enumerate. You can add every name and still end up with a trivial sized list. Sorting, adding or subtracting and putting all of them into an array.

    Leave a comment:


  • Rodney Hicks
    replied
    Well, Steve, someone may have hundreds or thousands of files to scan to get a list, of countries in this case, and they may want to add the names at the end of each document scanned, for reasons which I know not. They may be keeping other data as well, such as quantity of 'countries' found in each document. The OP may have a reason to approach the issue in this manner, where others may have chosen a different solution, such as yours. Besides, it gave Dave Biggs a chance to give a good lesson (to me anyway).

    Leave a comment:


  • Steve Hutchesson
    replied
    I am fascinated at some of the comments here, its a reasonable question, produce a reasonable answer. For the capacity to have a country list, however many you like, make a text file with a list of countries that are sorted and CRLF terminated. If you need to add another country, just add it to the list and sort it again. As far as putting the list into an array, TALLY will give you the number of entries, DIM an array of that many members and load the list into the array. Likewise if you want to remove a country, find it in the list, delete it and reload the array, what is all the big deal about ?

    Leave a comment:


  • Dave Biggs
    replied
    Tim
    at a later stage, can I append arrays using the ARRAY ASSIGN statements ?
    You can use ARRAY ASSIGN to append info to an existing array.

    You can do it via an absolute array that is positioned 'on top' of the original array elements that need new data.
    As in this version of your Post #3.
    Code:
    #DEBUG DISPLAY ON       ' < especially useful while developing apps that use arrays !!
    #COMPILE EXE
    #RESOURCE MANIFEST, 1, "XPTheme.xml"
    
       GLOBAL hDlg AS DWORD
       GLOBAL CountryArr() AS STRING
    
       %Idc_Cmb = 100
       %Idc_Lab = 108
       %IDC_BtnA = 110
    
    '=================================
       FUNCTION PBMAIN() AS LONG
          LOCAL hfb AS DWORD
    
          FONT NEW "Fbold" ,12,1 TO hfb
          DIALOG NEW PIXELS, 0, "Countries", 300, 300, 500, 300, %WS_SYSMENU, 0 TO hDlg
          DIALOG SET COLOR  hDlg, %RGB_MEDIUMBLUE , %RGB_AZURE
     '      add combobox after creating array
     '      CONTROL ADD COMBOBOX, hDlg, %Idc_Cmb, , 120, 50, 175, 225, %CBS_DROPDOWN OR %WS_VSCROLL
          CONTROL ADD LABEL , hDlg, %Idc_Lab,"Country  ", 30,50, 60, 20 ,  %SS_CENTER
          CONTROL SET COLOR  hDlg, %Idc_Lab, %RGB_DARKBLUE , %RGB_AZURE
          CONTROL SET FONT hDlg,%Idc_Lab, hfb
    
          CONTROL ADD BUTTON, hDlg, %IDC_BtnA, "Add countries to combobox",30,10,150,20
          CONTROL SET COLOR  hDlg, %Idc_BtnA, %RGB_MAGENTA , %RGB_AZURE
    
          DIALOG SHOW MODAL hDlg CALL DlgProc
          FONT END hfb
       END FUNCTION
    
     '===============================================
       CALLBACK FUNCTION DlgProc() AS LONG
         LOCAL  cbTxt AS STRING
         LOCAL cbItem AS LONG
    
          SELECT CASE AS LONG CBMSG
            CASE %WM_INITDIALOG
                    BuildCarr
                    ' Add ComboBox complete with array..    vvvvvvvvvvv
                    CONTROL ADD COMBOBOX, CBHNDL, %Idc_Cmb, CountryArr() , 120, 50, 175, 225, %CBS_DROPDOWN OR %WS_VSCROLL
    
            CASE  %WM_COMMAND
                 IF CB.CTL = %IDC_BtnA AND CB.CTLMSG = %BN_CLICKED THEN
                '   add more countries to combobox
                    BuildAddCarr
                    FUNCTION = 1
                 END IF
    
                 SELECT CASE CB.CTL
                  CASE %Idc_Cmb
                    IF CB.CTLMSG = %CBN_SELENDOK THEN
                       COMBOBOX GET TEXT   CBHNDL, %Idc_Cmb TO cbTxt
                       COMBOBOX GET SELECT CBHNDL, %Idc_Cmb TO cbItem
                       ? " selected : " + cbTxt,,"Item" + STR$(cbItem)
                    END IF
    
                 END SELECT
          END SELECT
    
       END FUNCTION
    '================
    
    ' build the array   ' combobox
    ' Note that Array Assign starts with LBound(Array())  ' ie FIRST Element, not neccessarily 'element 0'
    SUB BuildCarr
       LOCAL jj AS LONG
       REDIM CountryArr(0 TO 11)            ' Create array with 12 elements
       ARRAY ASSIGN CountryArr()="Cabo Verde", "Cambodia" , "Cameroon" , "Canada", "CAR" , _
          "Cayman Islands" , "Chad" , "Chile", "Colombia", "Comoros" , "Congo", "Costa Rica"
    
    END SUB
    '================
    
    ' Add more countries into the combobox
    ' Note that Array Assign starts with LBound(Array())  ' ie FIRST Element, not neccessarily 'element 0'
    SUB BuildAddCarr
      LOCAL jj AS LONG
      LOCAL ctrynam AS STRING
     ' retain the old countries name
       REDIM PRESERVE CountryArr(0 TO 21)   ' Change array to 22 elements
     ' fill additional items
       DIM AddOn(12 TO 21) AS STRING AT VARPTR(CountryArr(12))   ' NB DIM ..AT.. creates an 'absolute array'
        ARRAY ASSIGN AddOn()= "c13","c14","c15","c16","c17","c18","c19","c20","c21","c22"
       CONTROL KILL hDlg, %Idc_Cmb
       CONTROL ADD COMBOBOX, hDlg, %Idc_Cmb, CountryArr(), 120, 50, 175, 225, %CBS_DROPDOWN OR %WS_VSCROLL
    
    END SUB
    '------------------/

    Leave a comment:


  • Jim Fritts
    replied
    The context and continuity is here. His app. It would be the same if he asked "What am I doing wrong here." Your responses to that question is what Mr. Drake understandably was trying to squelch. KISS

    Tim,
    This looks promising.

    or

    This

    or

    This

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by George Bleck View Post
    Agree to disagree Jim. The understood standard edicate for practically every technical forum is NEW topic, NEW thread.

    This is not about "your thread your questions" this is about If someone else where to come across this thread and the initial Q by OP was about Arrays why would someone dig later on for more info about colored buttons when that's what they were searching for. Its about proper organization/categorization of data.
    +1

    Adam even had a standard header on the Forum saying the same thing recently.

    Or how about the first rule in the Forum's Rules & Guidelines:
    • Keep Threads On Topic
      • This will help the forum make more sense as people browse. If the conversation goes off topic, or you want to discuss another topic, please begin another thread in the appropriate location.

    Leave a comment:


  • George Bleck
    replied
    Agree to disagree Jim. The understood standard edicate for practically every technical forum is NEW topic, NEW thread.

    This is not about "your thread your questions" this is about If someone else where to come across this thread and the initial Q by OP was about Arrays why would someone dig later on for more info about colored buttons when that's what they were searching for. Its about proper organization/categorization of data.

    Leave a comment:


  • Jim Fritts
    replied
    Tim,
    You are fine to ask a question concerning your post. Ask any question you want. You started this thread. Disable the XPTheme for the button. Then add the color statement. If that does not work then ownerdraw the button.

    Leave a comment:


  • Stuart McLachlan
    replied
    Tim

    You are using a 0 based array to populate a Combobox Combobox items are 1 based.
    Mixing the lower bounds like that will cause you all sorts of problems later on.
    It has already caused a bug in your BuildAddCarr

    CountryArr(12) is blank.


    In this situation you are better off going with a "1" based array so that your array index and your combobox item number match.

    Leave a comment:


  • Stuart McLachlan
    replied
    Tim,

    Please don't start an entirely new topic in the middle of a thread. It would have been better to start a new thread on "How t change the background color of a button" Also a forum search on "button background color" should provide a few hits

    One possible way is to use a label if you don't need a visual indication of the the click. It fits with the current MS preference for "flat" styles

    Note that the label need %SS_NOTIFY to trap the %STN_CLICKED message

    '
    Code:
    ...
        CONTROL ADD LABEL, hDlg,   %IDC_lblUpdate  ,"Update", 400,340,50,20, %SS_NOTIFY OR _
            %WS_CHILD OR %WS_VISIBLE OR %SS_CENTER OR %SS_CENTERIMAGE, %WS_EX_LEFT OR _
            %WS_EX_LTRREADING
        CONTROL SET COLOR  hDlg, %IDC_lblUpdate, %WHITE, RGB(111, 159, 163)
        CONTROL SET FONT hDlg,  %IDC_lblUpdate, hFont3
    ...
    CALLBACK FUNCTION DataScreenProc() AS LONG
            CASE %WM_COMMAND
                SELECT CASE AS LONG  CB.CTL
                    CASE %IDC_lblUpdate
                        IF CB.CTLMSG = %STN_CLICKED THEN
                            UpdateItem
                        END IF
                END SELECT
        END SELECT
    END FUNCTION

    Leave a comment:


  • Dale Yarker
    replied
    Buttons (and a couple other controls) don't take color that easily. ((so it is not anything done wrong))

    A solution is CONTROL ADD IMGBUTTONX. Make bitmap of button top and put in program as a resource.

    Cheers,

    Leave a comment:


  • Tim Lakinir
    replied
    Thank you Sir Jim

    Here's my code
    Code:
       #COMPILE EXE
      #RESOURCE MANIFEST, 1, "xpTheme.xml"
    
       GLOBAL hDlg AS DWORD
    
       GLOBAL CountryArr() AS STRING
    
       %Idc_Cmb = 100
       %Idc_Lab = 108
       %IDC_BtnA = 110
    
    '=================================
       FUNCTION PBMAIN() AS LONG
          LOCAL hfb AS DWORD
    
          FONT NEW "Fbold" ,12,1 TO hfb
          DIALOG NEW PIXELS, 0, "Countries", 300, 300, 500, 300, %WS_SYSMENU, 0 TO hDlg
          DIALOG SET COLOR  hDlg, %RGB_MEDIUMBLUE , %RGB_AZURE
    
          CONTROL ADD COMBOBOX, hDlg, %Idc_Cmb, , 120, 50, 175, 225, %CBS_DROPDOWN OR %WS_VSCROLL
          CONTROL ADD LABEL , hDlg, %Idc_Lab,"Country  ", 30,50, 60, 20 ,  %SS_CENTER
          CONTROL SET COLOR  hDlg, %Idc_Lab, %RGB_DARKBLUE , %RGB_AZURE
          CONTROL SET FONT hDlg,%Idc_Lab, hfb
    
          CONTROL ADD BUTTON, hDlg, %IDC_BtnA, "Add countries to combobox",30,10,150,20
          CONTROL SET COLOR  hDlg, %Idc_BtnA, %RGB_MAGENTA , %RGB_AZURE
    
          DIALOG SHOW MODAL hDlg CALL DlgProc
          FONT END hfb
       END FUNCTION
    
    
     '===============================================
       CALLBACK FUNCTION DlgProc() AS LONG
         LOCAL  cbTxt AS STRING
    
          SELECT CASE AS LONG CBMSG
            CASE %WM_INITDIALOG
                    BuildCarr
    
            CASE  %WM_COMMAND
                 IF CB.CTL = %IDC_BtnA AND CB.CTLMSG = %BN_CLICKED THEN
                '   add more countries to combobox
                    BuildAddCarr
                    FUNCTION = 1
                 END IF
    
                 SELECT CASE CB.CTL
                  CASE %Idc_Cmb
                    IF CB.CTLMSG = %CBN_SELENDOK THEN
                       COMBOBOX GET TEXT CBHNDL, %Idc_Cmb TO cbTxt
                       ?  " selected : " + cbTxt
                    END IF
    
                 END SELECT
          END SELECT
    
       END FUNCTION
    
    
    '================
    ' build the combobox
    ' Note that Array Assign starts with element 0
    SUB BuildCarr
       LOCAL jj AS LONG
       REDIM CountryArr(0 TO 11)
       ARRAY ASSIGN CountryArr()="Cabo Verde", "Cambodia" , "Cameroon" , "Canada", "CAR" , _
          "Cayman Islands" , "Chad" , "Chile", "Colombia", "Comoros" , "Congo", "Costa Rica"
    
       FOR jj = 0 TO 11
            COMBOBOX ADD hDlg, %Idc_Cmb, CountryArr(jj)
       NEXT jj
       CONTROL REDRAW  hDlg, %Idc_Cmb
    END SUB
    
    
    
     '================
    ' Add more countries into the combobox
    ' Note that Array Assign starts with element 0
    SUB BuildAddCarr
       LOCAL jj AS LONG
       LOCAL ctrynam AS STRING
     ' retain the old countries name
       REDIM PRESERVE CountryArr(0 TO 21)
    
       FOR jj = 13 TO 21
           ctrynam = TRIM$(STR$(jj))
          ARRAY INSERT CountryArr(jj),"C" + ctrynam
          COMBOBOX ADD hDlg, %Idc_Cmb, CountryArr(jj)
       NEXT jj
       CONTROL REDRAW  hDlg, %Idc_Cmb
    
    END SUB

    that's only one problem here is that I can't display the color on the button despite using CONTROL SET COLOR

    Code:
          CONTROL ADD BUTTON, hDlg, %IDC_BtnA, "Add countries to combobox",30,10,150,20
          CONTROL SET COLOR  hDlg, %Idc_BtnA, %RGB_MAGENTA , %RGB_AZURE

    Leave a comment:


  • Jim Fritts
    replied
    The PRESERVE keyword tells the compiler to preserve the values of all
    existing elements in the array. For example, if you REDIM PRESERVE an
    array with 10 elements to 20 elements, the first 10 elements will
    retain their original value.

    ARRAY ASSIGN is used to quickly put values into an array that has already been dimensioned.

    ARRAY ASSIGN would rewrite the array starting with cell 1 not cell 12.

    I think you are looking for ARRAY INSERT.

    ARRAY INSERT CountryArr(12), "c12" <<<< this overwrites Costa Rica since the original array had 12 elements (0-11).
    ARRAY INSERT CountryArr(13), "c13"
    .
    .
    .
    ARRAY INSERT CountryArr(21), "c21"

    Leave a comment:


  • Can we append arrays using ARRAY ASSIGN statements?

    Hi All

    The problem is that I may not know the actual size or the number of array elements of an array. So if the number of elements increases
    at a later stage, can I append arrays using the ARRAY ASSIGN statements ?

    For example, at the start of a program, my code would be for an array CountryArr() of 11 elements
    Code:
    GLOBAL CountryArr() AS STRING
    
       REDIM CountryArr(11)
       ARRAY ASSIGN CountryArr()="Cabo Verde", "Cambodia" , "Cameroon" , "Canada", "CAR" , _
          "Cayman Islands" , "Chad" , "Chile", "Colombia", "Comoros" , "Congo", "Costa Rica"

    Later on I would like to append more countries into this array and expand it to 21 elements
    Code:
    REDIM PRESERVE  CountryArr(1 TO 21)
    can I then assign this array again using the following statement ?

    Code:
      ARRAY ASSIGN CountryArr()="c12" ,"c13" , "c14", .......... "c21"
Working...
X