CGC_Codes

C++ Code Example For A List Box

Jan 30th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.82 KB | None | 0 0
  1. class TVendDlg : public TDialog {
  2. public:
  3.     virtual void SetupWindow();
  4.     virtual void VendDel(RTMessage Msg)
  5.       = [ID_FIRST + ID_DELX_];
  6.     virtual void VendChs(RTMessage Msg)
  7.       = [ID_FIRST + ID_CHS1_];
  8.     virtual void VendPrn(RTMessage Msg)
  9.       = [ID_FIRST + ID_VPRN_];
  10.     char Vncode[MAXCCODE];
  11.     char Vnname[MAXCNAME];
  12.     char Vnstreet[MAXCSTREET];
  13.     char Vnstreet2[MAXCSTREET];
  14.     char Vncity[MAXCCITY];
  15.     char Vnstate[MAXCSTATE];
  16.     char Vnzip[MAXCZIP];
  17.     char VnTell1[MAXCTF1];
  18.     char VnFax1[MAXCTF1];
  19.     char Vnatt[MAXATT];
  20.     char VnPaytrm[MAXTERM];
  21.     char VnNote1[MAXNOTE];
  22.     char VnNote2[MAXNOTE];
  23.  
  24. TEdit *Edit1,*Edit2,*Edit3,*Edit4,*Edit5,*Edit6,*Edit7,*Edit8,*Edit9,*Edit10,
  25. *Edit11,*Edit12,*Edit13;
  26.  
  27.     TVendDlg(PTWindowsObject AParent, LPSTR name);
  28.     virtual BOOL CanClose();
  29. };
  30.  
  31. class ListBoxDialog : public TDialog
  32. {
  33. public:
  34.   ListBoxDialog(PTWindowsObject AParent, LPSTR AName)
  35.     : TDialog(AParent, AName) {};
  36.   virtual void SetupWindow();
  37.   virtual void HandleListBoxMsg(RTMessage Msg)
  38.     = [ID_FIRST + ID_LISTBOX];
  39. };
  40.  
  41. void TVendDlg::VendChs(RTMessage)
  42. {
  43.   int       a;
  44.   streambuf *inn = cin.rdbuf();
  45.   ifpstream ifile;
  46.   Globalvar = 0;
  47.   GetApplication()->ExecDialog(new ListBoxDialog(this, "VENDORDIALOG"));
  48.  
  49.     // if the Global variable, “Globalvar” is set to 1 from the
  50.     // “ListBoxDialog::HandleListBoxMsg(RTMessage Msg)” member
  51.     // function, then proceed.
  52.     if( Globalvar == 1) {
  53.  
  54.     // set the global flag, “hasbeenselected”, to signal a vendor
  55.     // has been selected from the list box.
  56.     hasbeenselected = 1;
  57.  
  58.         // display the retrieved vendor data in the vendor data entry
  59.         // screen after the selection in the list box has been clicked
  60.         // by the user. the data for the selected vendor will be
  61.                 // assigned to the edit controls in the vendor data entry
  62.                 // screen.
  63.         ifile.open("vend.txt", ios::in | ios::binary);
  64.         inn = ifile.rdbuf();
  65.         // position the filestream ofthe binary vendor
  66.         // data file to the calculated filestream offset
  67.         // value of the selected list box item.
  68.         inn -> seekpos(offsetvar, ios::in);
  69.         for(a=0; a<MAXCCODE-1; a++) Vncode[a] = ifile.readByte();
  70.         Vncode[MAXCCODE-1] = 0;
  71.         Edit1->SetText(Vncode);
  72.         for(a=0; a<MAXCNAME-1; a++) Vnname[a] = ifile.readByte();
  73.         Vnname[MAXCNAME-1] = 0;
  74.         Edit2->SetText(Vnname);
  75.         for(a=0; a<MAXCSTREET-1; a++) Vnstreet[a] = ifile.readByte();
  76.         Vnstreet[MAXCSTREET-1] = 0;
  77.         Edit3->SetText(Vnstreet);
  78.         for(a=0; a<MAXCSTREET-1; a++) Vnstreet2[a] = ifile.readByte();
  79.         Vnstreet2[MAXCSTREET-1] = 0;
  80.         Edit4->SetText(Vnstreet2);
  81.         for(a=0; a<MAXCCITY-1; a++) Vncity[a] = ifile.readByte();
  82.         Vncity[MAXCCITY-1] = 0;
  83.         Edit5->SetText(Vncity);
  84.         for(a=0; a<MAXCSTATE-1; a++) Vnstate[a] = ifile.readByte();
  85.         Vnstate[MAXCSTATE-1] = 0;
  86.         Edit6->SetText(Vnstate);
  87.         for(a=0; a<MAXCZIP-1; a++) Vnzip[a] = ifile.readByte();
  88.         Vnzip[MAXCZIP-1] = 0;
  89.         Edit7->SetText(Vnzip);
  90.         for(a=0; a<3; a++) VnTell1[a] = ifile.readByte();
  91.         VnTell1[3] = '-';
  92.         for(a=0; a<3; a++) VnTell1[4+a] = ifile.readByte();
  93.         VnTell1[7] = '-';
  94.         for(a=0; a<4; a++) VnTell1[8+a] = ifile.readByte();
  95.         VnTell1[MAXCTF1-1] = 0;
  96.         Edit8->SetText(VnTell1);
  97.         for(a=0; a<3; a++) VnFax1[a] = ifile.readByte();
  98.         VnFax1[3] = '-';
  99.         for(a=0; a<3; a++) VnFax1[4+a] = ifile.readByte();
  100.         VnFax1[7] = '-';
  101.         for(a=0; a<4; a++) VnFax1[8+a] = ifile.readByte();
  102.         VnFax1[MAXCTF1-1] = 0;
  103.         Edit9->SetText(VnFax1);
  104.         for(a=0; a<MAXATT-1; a++) Vnatt[a] = ifile.readByte();
  105.         Vnatt[MAXATT-1] = 0;
  106.         Edit10->SetText(Vnatt);
  107.         for(a=0; a<MAXTERM-1; a++) VnPaytrm[a] = ifile.readByte();
  108.         VnPaytrm[MAXTERM-1] = 0;
  109.         Edit11->SetText(VnPaytrm);
  110.         for(a=0; a<MAXNOTE-1; a++) VnNote1[a] = ifile.readByte();
  111.         VnNote1[MAXNOTE-1] = 0;
  112.         Edit12->SetText(VnNote1);
  113.         for(a=0; a<MAXNOTE-1; a++) VnNote2[a] = ifile.readByte();
  114.         VnNote2[MAXNOTE-1] = 0;
  115.         Edit13->SetText(VnNote2);
  116.         ifile.close();
  117.  
  118.     }
  119.  
  120.   }
  121.  
  122.   VENDORDIALOG DIALOG DISCARDABLE LOADONCALL PURE MOVEABLE 30, 18, 208, 108
  123. STYLE WS_POPUP | WS_DLGFRAME
  124. FONT 10, "COURIER"
  125. BEGIN
  126.   CONTROL "Vendor Name                   Vend. Code   ", 10055, "static", SS_LEFT | WS_CHILD, 20, 3, 188, 8
  127.   CONTROL "&Exit" IDCANCEL, "BUTTON", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 20, 93, 48, 12
  128.   CONTROL "Vendor Listing", 10056, "static", SS_LEFT | WS_CHILD, 75, 93, 200, 8
  129.   CONTROL "LISTBOX" ID_LISTBOX, "LISTBOX", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | 0x3L, 20, 15, 168, 73
  130. END
  131. }
  132.  
  133. oid ListBoxDialog::SetupWindow()
  134. {
  135.   long int   fileoffset, sizeofdatafile;
  136.   int        a,fileinfo,t;
  137.   streambuf  *inn = cin.rdbuf();
  138.   ifpstream  ifile;
  139.  
  140.   // this will loop around the “vend.txt” binary data file of
  141.   // vendors and add a data record to the list box, which includes
  142.   // vendor name and vendor code.
  143.   fileinfo = open("vend.txt", ios::in | ios::binary);
  144.   sizeofdatafile = filelength(fileinfo);
  145.   close(fileinfo);
  146.   ifile.open("vend.txt", ios::in | ios::binary);
  147.   inn = ifile.rdbuf();
  148.   fileoffset  = 0;
  149.   do {
  150.  
  151.         // initialize the char array, “char_array”, with space characters. 
  152.         for(a=0; a<100; a++) char_array[a] = 32;
  153.  
  154.         // read the vendor name and vendor code from the file stream.
  155.         inn -> seekpos(fileoffset, ios::in);
  156.             for(a=0; a<MAXCCODE-1; a++) char_array[32+a] = ifile.readByte();
  157.         inn -> seekpos(fileoffset+MAXCCODE-1, ios::in);
  158.         for(a=0; a<MAXCNAME-1; a++) char_array[a] = ifile.readByte();
  159.  
  160.             // mask out white space characters.
  161.             for(a=0; a<100; a++) {
  162.             if(char_array[a]<33 || char_array[a]>126) char_array[a] = 32;
  163.             }
  164.  
  165.                 // read the sequential position of the record in the binary text file.
  166.                 inn -> seekpos(fileoffset+VENDLEN-5, ios::in);
  167.                 for(a=0; a<5; a++) char_array[70+a] = ifile.readByte();
  168.                 // null space the end of the char array to  suppress trailing random chars.
  169.                 char_array[99] = 0;
  170.  
  171.                     // convert the char array to lower case.
  172.                     strlwr(char_array);
  173.  
  174.                         // add the vendor name and vendor code pair to the list box control.
  175.                         SendDlgItemMsg(ID_LISTBOX, LB_ADDSTRING, 0, (LONG) char_array);
  176.  
  177.   // advance to the next record in the binary text file.       
  178.   fileoffset = fileoffset + VENDLEN;
  179.  
  180.   } while(fileoffset<sizeofdatafile);
  181.  
  182.   ifile.close();
  183.  
  184. }
  185.  
  186. void ListBoxDialog::HandleListBoxMsg(RTMessage Msg)
  187. {
  188.   long int   a, convert_to_number[5];
  189.   DWORD      Idx;
  190.  
  191.   // if the exit button is clicked, then exit and reset global variable to 0.
  192.   if ( Msg.LP.Hi == LBN_SELCANCEL ) Globalvar = 0;
  193.  
  194.     // if a selection is made, then reset the global variable to 1 and proceed to calculate the
  195.     // filestream offset after getting the list box index of the selection, “Idx”.
  196.     if ( Msg.LP.Hi == LBN_SELCHANGE ) {
  197.  
  198.         // initialize the char array, “char_array”, with space characters. 
  199.         for(a=0; a<80; a++) char_array[a] = 32;
  200.  
  201.         Globalvar = 1;
  202.  
  203.         // get the index of the selected list box item.
  204.         Idx = SendDlgItemMsg(ID_LISTBOX, LB_GETCURSEL, 0, 0L);
  205.  
  206.         char_array[79] = 0;
  207.  
  208.         // use the index to retrieve the contents of the selected list box row into a char array, “char_array”.
  209.         SendDlgItemMsg(ID_LISTBOX, LB_GETTEXT, (WORD)Idx, (DWORD) char_array);
  210.  
  211.         // close the list box window after retrieving info into the char array from above.
  212.         CloseWindow();
  213.  
  214.                 // this will take the auto-generated sequential
  215.             // position of the vendor record stored in each record of the binary text file, “vend.txt”
  216.             // from the char array and convert it to a numerical value to be multiplied by the defined constant,
  217.             // “VENDLEN”. this will produce the filestream offset I call “offsetvar”, which is used to locate the
  218.             // vendor data in the member function “VendChs” of the “TVendDlg” dialog class, which will populate
  219.             // the edit controls in the vendor data entry screen.
  220.                 for(a=0; a<5; a++) {
  221.                 convert_to_number[a] = 0;
  222.                 if(char_array[70+a] == 48) convert_to_number[a] = 0;
  223.                 if(char_array[70+a] == 49) convert_to_number[a] = 1;
  224.                 if(char_array[70+a] == 50) convert_to_number[a] = 2;
  225.                 if(char_array[70+a] == 51) convert_to_number[a] = 3;
  226.                 if(char_array[70+a] == 52) convert_to_number[a] = 4;
  227.                 if(char_array[70+a] == 53) convert_to_number[a] = 5;
  228.                 if(char_array[70+a] == 54) convert_to_number[a] = 6;
  229.                 if(char_array[70+a] == 55) convert_to_number[a] = 7;
  230.                 if(char_array[70+a] == 56) convert_to_number[a] = 8;
  231.                 if(char_array[70+a] == 57) convert_to_number[a] = 9;
  232.             }
  233.                 offsetvar = ( (convert_to_number[0] * 10000) + (convert_to_number[1] * 1000) + (convert_to_number [2] * 100) + (convert_to_number [3] * 10 ) + (convert_to_number[4] * 1) ) * VENDLEN;
  234.  
  235.     }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment