PowerBuilder Manual Book Part 8
PowerBuilder Curriculum Part 8
VI. Custom functions
1. gf_get_exe
(1), define external functions in gobal external functions
1 |
Function ULong GetModuleFileName(ULong hModule, Ref String lpFileName, ULong nSize) Library "kernel32.dll" Alias For "GetModuleFileNameA" |
(2), create custom functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* \>=================================================== ===================== \> function:gf_file_getexe \>------------------------------------------------ -------------------- \> Description: Get exe name \>------------------------------------------------ -------------------- \> parameters: \> showexesuffix: integer type (0, do not display exe suffix, 1, display exe suffix) \>------------------------------------------------ -------------------- \> Return: string type (the function returns the name of the exe successfully) \>=================================================== ===================== */ String str_temp,separator String str_list[] Integer i = 1, ll_pos, ll_max String ls_fileName GetModuleFileName(Handle(This), ls_fileName, 100) separator = '\' ll_pos = Pos(ls_fileName,separator,1) Do If ll_pos = 0 Then str_list[i] = ls_fileName Else str_list[i] = Left(ls_fileName,ll_pos - 1) i++ (3), Call the Function 2. gf_file_write (1), Create a custom Function ls_fileName = Mid(ls_fileName,ll_pos + Len(separator)) ll_pos = Pos(ls_fileName,separator,1) If ll_pos = 0 Then str_list[i] = ls_fileName End If Loop While ll_pos <> 0 ll_max = UpperBound(str_list) If showexesuffix = 1 Then Return Mid(str_list[ll_max],1,Pos(str_list[ll_max],'.') - 1) Else Return str_list[ll_max] End If |
(3), call the function
1 2 3 |
String ls_exe ls_exe = gf_get_exe(0) //Do not display exe suffix ls_exe = gf_get_exe(1) //display exe suffix |
2. gf_file_write
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/* \>=================================================== =================== \> Function: gf_file_write \>------------------------------------------------ -------------------- \> Description: file write \>------------------------------------------------ -------------------- \> parameters: \> string fileway (the way to open the file) \> string filepath (file path and file name to write) \> string filecontent (content to write) \>------------------------------------------------ -------------------- \> Return: integer (0: success, -1: failure.) ==================================================== =================== */ Long ll_file, ll_FLength, ll_i, ll_count String ls_content Choose Case fileway Case 'w' //Stream mode, write, all users can read and write the file, overwrite ll_file = FileOpen(filepath, streamMode!, Write!, Shared!, Replace!) Case 'w+' //Stream mode, write, all users can read and write the file, append ll_file = FileOpen(filepath, streamMode!, Write!, Shared!, append!) Case 'wl' //line mode, write, all users can read and write the file, overwrite ll_file = FileOpen(filepath,LineMode!,Write!,Shared!,Replace!) Case 'wl+' //line mode, write, all users can read and write the file, append ll_file = FileOpen(filepath,LineMode!,Write!,Shared!,append!) Case Else MessageBox('Prompt', 'gf_file_write: The incoming parameter is wrong!') Return - 1 End Choose If ll_file > 0 Then (2), Call the Function 3. gf_file_read (1), Create a custom Function ll_FLength = Len(filecontent) If ll_FLength > 32765 Then If Mod(ll_FLength,32765) = 0 Then ll_count = ll_FLength / 32765 Else ll_count = ll_FLength / 32765 + 1 End If Else ll_count = 1 End If For ll_i = 1 To ll_count ls_content = Left(filecontent,32765) FileWrite(ll_file, ls_content) filecontent = Right(filecontent, Len(filecontent) - 32765) Next FileClose(ll_file) Else MessageBox('prompt', 'gf_file_write: failed to open the file!') Return - 1 End If Return 0 |
(2), call the function
1 2 3 4 |
gf_file_write('w', getcurrentdirectory() + '\1.txt', 'stream mode override') gf_file_write('w+', getcurrentdirectory() + '\1.txt', 'stream mode append') gf_file_write('wl', getcurrentdirectory() + '\1.txt', 'Line mode override') gf_file_write('wl+', getcurrentdirectory() + '\1.txt', 'line mode append') |
3. gf_file_read
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/* \>=================================================== ===================== \> Function: gf_file_read \>------------------------------------------------ -------------------- \> Description: file read \>------------------------------------------------ -------------------- \> parameters: \> string fileway (the way to open the file) \> string filepath (file path and file name to read) \> ref string filecontent[] (content to be returned) \>------------------------------------------------ -------------------- \> Return: integer (0: success, -1: failure.) \>=================================================== ===================== */ Long ll_file, ll_FLength, ll_i, ll_j, ll_count String ls_content Choose Case fileway Case 'r' //Stream mode, read, all users can read and write the file ll_file = FileOpen(filepath, streamMode!, Read!, Shared!) If ll_file > 0 Then ll_FLength = Len(filepath) If ll_FLength > 32765 Then (2), Call the Function If Mod(ll_FLength,32765) = 0 Then ll_count = ll_FLength / 32765 Else ll_count = ll_FLength / 32765 + 1 End If Else ll_count = 1 End If For ll_i = 1 To ll_count FileRead(ll_file, ls_content) filecontent[1] += ls_content Next Else MessageBox('prompt', 'gf_file_read: failed to open the file!') Return - 1 End If Case 'rl' //line mode, read, all users can read and write the file ll_file = FileOpen(filepath,LineMode!,Read!,Shared!) If ll_file > 0 Then ll_j = 1 ll_i = FileRead(ll_file,filecontent[ll_j]) Do Until String(ll_i) = '-100' ll_j = ll_j + 1 ll_i = FileRead(ll_file,filecontent[ll_j]) the Loop FileClose(ll_file) Else MessageBox('prompt', 'gf_file_read: failed to open the file!') Return - 1 End If Case Else MessageBox('Prompt', 'gf_file_read: wrong parameter passed in') Return - 1 End Choose Return 0 |
(2), call the function
1 2 3 4 5 6 7 8 9 10 |
(1), Read the file In stream mode String ls_data[] gf_file_read('r', getcurrentdirectory() + '\1.txt', ls_data[] ) MessageBox('Stream mode return value', ls_data[1]) (2), Read the file In Line mode String ls_data[] gf_file_read('rl', getcurrentdirectory() + '\1.txt', ls_data[] ) MessageBox('line mode return value', ls_data[1]) MessageBox('line mode return value', ls_data[2]) MessageBox('line mode return value', ls_data[3]) |
4. gf_string_cut
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
/* \>=================================================== ===================== \> function: gf_string_cut. \>------------------------------------------------ -------------------- \> Description: String cutting \>------------------------------------------------ -------------------- \> parameters: \> way: string (optional parameter: array: split the string and return an array. string: split the string and return a string) \> data:string (string data to be split) \> conditions: string (string split conditions) \> value[] (value after string splitting) \>------------------------------------------------ -------------------- \> Return: integer (0: success, -1: failure.) \>=================================================== ===================== */ Long ll_empty_pos, i = 1 String ls_value_string, ls_value_array[] Choose Case way Case 'array' ll_empty_pos = Pos(Data,conditions) If ll_empty_pos > 0 Then Do ls_value_array[i] = Trim(Mid(Data,1,ll_empty_pos - 1)) Data = Trim(Mid(Data,ll_empty_pos + 1)) ll_empty_pos = Pos(Data,conditions) i++ If ll_empty_pos = 0 Then ls_value_array[i] = Trim(Mid(Data,0)) End If Loop While ll_empty_pos > 0 Value[] = ls_value_array[] Else MessageBox('Prompt', 'The string passed in by gf_string_cut was not found!') Return - 1 End If Case 'string' ll_empty_pos = Pos(Data,conditions) If ll_empty_pos > 0 Then Do ls_value_string += Trim(Mid(Data,1,ll_empty_pos - 1)) Data = Trim(Mid(Data,ll_empty_pos + 1)) ll_empty_pos = Pos(Data,conditions) If ll_empty_pos = 0 Then ls_value_string += Trim(Mid(Data,0)) End If Loop While ll_empty_pos > 0 Value[1] = ls_value_string Else MessageBox('Prompt', 'The string passed in by gf_string_cut was not found!') Return - 1 End If Case Else MessageBox('prompt', 'gf_string_cut incoming parameter error!') Return - 1 End Choose Return 0 |
(2), call the function
1 2 3 4 5 6 7 8 9 10 |
String ls_data, ls_value[] ls_data = '123,456,789' //1. Cut the string and return the cut string gf_string_cut('string',ls_data,',',ls_value[]) MessageBox('',ls_value[1]) //123456789 //2. Cut the string and return the array gf_string_cut('array',ls_data,',',ls_value[]) MessageBox('', ls_value[1]) //123 MessageBox('',ls_value[2]) //456 MessageBox('', ls_value[3]) //789 |
5. gf_string_replace
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* \>=================================================== ===================== \> function:gf_string_replace \>------------------------------------------------ -------------------- \> Description: String replacement \>------------------------------------------------ -------------------- // parameters: // string data string data // string as_old character to be replaced // string as_new new value to replace \>------------------------------------------------ -------------------- \> Return: string Replaced characters \>=================================================== ===================== */ Long ll_oldlen, ll_newlen, ll_pos ll_pos = Pos(Data,as_old) If ll_pos > 0 Then ll_oldlen = Len(as_old) ll_newlen = Len(as_new) Do While ll_pos > 0 Data = Replace(Data,ll_pos,ll_oldlen,as_new) ll_pos = Pos(Data,as_old,ll_pos + ll_newlen) Loop End If Return Data |
(2), call the function
1 2 3 |
String ls_data ls_data = gf_string_replace('1234561','1','6') MessageBox('return value', ls_data) //623456 |
6. gf_adodb_blob_write
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/* \>=================================================== ===================== \> function: gf_adodb_blob_write. \>------------------------------------------------ -------------------- \> Description: Large file write (binary data stream) \>------------------------------------------------ -------------------- \> parameters: \> string filepath (file path and file name to write) \> blob filecontent (binary data file content to write) \>------------------------------------------------ -------------------- \> Return: integer (0: success, -1: failure.) \>=================================================== ===================== */ Long li_rtn, ll_file String ls_data OLEObject ADODB ADODB = Create OLEObject li_rtn = ADODB.ConnectToNewObject("ADODB.Stream") If li_rtn = 0 Then ADODB.Type = 1 //Set ADODB type (1, binary data, 2, text data) ADODB.Mode = 3 //Set read and write mode (1, read, 2, write, 3, read and write) ADODB.Open() //Open ADODB ADODB.Position = 0 //Position=0 to set Charset ADODB.Write(filecontent) //Write and WriteText methods input bytes or text to Stream. ADODB.savetofile(filepath,2) //savetofile(1, write only once. 2, can overwrite and write multiple times) ADODB.flush() //Empty buffer data ADODB.Close() //Close the stream Destroy ADODB Else MessageBox('Prompt', 'Unable to connect to [ADODB.Stream]!~r~nFailed to read the file!') Return - 1 End If Return 0 |
(2), call the function
1 |
gf_adodb_blob_write(getcurrentdirectory()+'\a.txt',Blob('123')) |
7. gf_adodb_blob_read
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* \>=================================================== ===================== \> function: gf_adodb_blob_read. \>------------------------------------------------ -------------------- \> Description: large file read (binary data stream) \>------------------------------------------------ -------------------- \> parameters: \> string filepath (file path and file name to read) \>------------------------------------------------ -------------------- \> Return: blob (blob data read from file) \>=================================================== ===================== */ Long li_rtn Blob lb_data OLEObject ADODB ADODB = Create OLEObject li_rtn = ADODB.ConnectToNewObject("ADODB.Stream") If li_rtn = 0 Then ADODB.Type = 1 //Set ADODB type (1, binary data, 2, text data) ADODB.Mode = 3 //Set read and write mode (1, read, 2, write, 3, read and write) ADODB.Open() //Open ADODB ADODB.LoadFromFile(filepath) //load file ADODB.Position = 0 //Position=0 to set Charset lb_data = ADODB.Read() //Read and ReadText methods read bytes or text to Stream. ADODB.flush() //Empty buffer data ADODB.Close() //Close the stream Destroy ADODB Else MessageBox('Prompt', 'Unable to connect to [ADODB.Stream]!~r~nFailed to read the file!') End If Return lb_data |
(2), call the function
1 2 3 |
Blob lb_data lb_data = gf_adodb_blob_read(getcurrentdirectory()+'\a.txt') MessageBox('blob',String(lb_data)) |
8. gf_adodb_string_write
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* \>=================================================== ===================== \> function: gf_adodb_string_write. \>------------------------------------------------ -------------------- \> Description: large file write (text stream) \>------------------------------------------------ -------------------- \> parameters: \> string filepath (file path and file name to write) \> string filecontent (text stream content to write) \>------------------------------------------------ -------------------- \> Return: integer (0: success, -1: failure.) \>=================================================== ===================== */ Long li_rtn, ll_file String ls_data OLEObject ADODB ADODB = Create OLEObject li_rtn = ADODB.ConnectToNewObject("ADODB.Stream") If li_rtn = 0 Then ADODB.Type = 2 //Set ADODB type (1, binary data, 2, text data) ADODB.Mode = 3 //Set read and write mode (1, read, 2, write, 3, read and write) ADODB.Open() //Open ADODB ADODB.Position = 0 //Position=0 to set Charset ADODB.WriteText(filecontent) //Write and WriteText methods input bytes or text to Stream Book. ADODB.savetofile(filepath,2) //savetofile(1, write only once. 2, can overwrite and write multiple times) ADODB.flush() //Empty buffer data ADODB.Close() //Close the stream Destroy ADODB Else MessageBox('Prompt', 'Unable to connect to [ADODB.Stream]!~r~nFailed to read the file!') Return - 1 End If Return 0 |
(2), call the function
1 |
gf_adodb_string_write(getcurrentdirectory()+'\a.txt','123') |
9. gf_adodb_string_read
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* \>=================================================== ===================== \> function: gf_adodb_string_read. \>------------------------------------------------ -------------------- \> Description: large file read (text stream) \>------------------------------------------------ -------------------- \> parameters: \> string filepath (file path and file name to read) \>------------------------------------------------ -------------------- \> Return: string (the blob data read from the file) \>=================================================== ===================== */ Long li_rtn String ls_data OLEObject ADODB ADODB = Create OLEObject li_rtn = ADODB.ConnectToNewObject("ADODB.Stream") If li_rtn = 0 Then ADODB.Type = 2 //Set ADODB type (1, binary data, 2, text data) ADODB.Mode = 3 //Set read and write mode (1, read, 2, write, 3, read and write) ADODB.Open() //Open ADODB ADODB.LoadFromFile(filepath) //load file ADODB.Position = 0 //Position= 0 to set Charset ls_data = ADODB.ReadText() //Read and ReadText methods read bytes or text to Stream. ADODB.flush() //Empty buffer data ADODB.Close() //Close the stream Destroy ADODB Else MessageBox('Prompt', 'Unable to connect to [ADODB.Stream]!~r~nFailed to read the file!') End If Return ls_data |
(2), call the function
1 2 3 |
String ls_data ls_data = gf_adodb_string_read(getcurrentdirectory()+'\a.txt') MessageBox('string',ls_data) |
10. gf_remove_letter
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* \>=================================================== ===================== \> Function: gf_remove_letter \>------------------------------------------------ -------------------- \> Description: Remove letters from a string \>------------------------------------------------ -------------------- \> parameters: \> string ls_data string to remove letters \>------------------------------------------------ -------------------- \> return: string \>=================================================== ===================== */ String str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' String ls_data_new Long i If ls_data = '' Or IsNull(ls_data) Then ls_data_new = '' Else For i = 1 To Len(ls_data) If Pos(str,Mid(ls_data,i,1)) = 0 Then ls_data_new += Mid(ls_data,i,1) End If Next End If Return ls_data_new |
(2), call the function
1 |
MessageBox('example', gf_remove_letter('12abc3sdfsg4')) |
11. gf_last_month
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* >==================================================== ==================== > Function: gf_last_month >----------------------------------------------- ------------------- > Description: Get the time from the beginning of the previous month to the end of the month >----------------------------------------------- ------------------- > Parameters: > ref string lrt_start > ref string lrt_end >----------------------------------------------- ------------------- > return value: (none) >==================================================== ==================== */ String ls_by2 String ls_by, ls_sy ls_by2 = Mid(String(Date(Today())),1,7) + '-01' ls_by = String(RelativeDate(Date(ls_by2), - 1)) ls_sy = String(Year(Date(ls_by2))) + '-' + String(Month(Date(ls_by2)) - 1) + '-01' lrt_start = ls_sy + '00:00:00' lrt_end = ls_by + '23:59:59' |
(2), calling letter
1 2 3 4 5 |
// call example String ls_start, ls_end of_last_month(ls_start, ls_end) MessageBox('(date+time) at the beginning of the month', ls_start) MessageBox('at the end of the month (date+time)', ls_end) |
12. gf_datetime_string
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/* >==================================================== ==================== > Function: gf_datetime_string >----------------------------------------------- ------------------- > Description: Convert time to string >----------------------------------------------- ------------------- > Parameters: > string as_datetime > string as_select >----------------------------------------------- ------------------- > return value: string >==================================================== ==================== */ String ls_y, ls_m, ls_d, ls_h, ls_i, ls_s Long ll_y, ll_m, ll_d, ll_h, ll_i, ll_s String ls_format, ls_return as_datetime = Trim(as_datetime) If Pos(as_datetime,'-') > 0 Then ls_format = '-' ll_y = Pos(as_datetime,'-') ll_m = Pos(as_datetime,'-',ll_y + 1) ll_d = Pos(as_datetime,' ',ll_m + 1) ll_h = Pos(as_datetime,':') ll_i = Pos(as_datetime, ':', ll_h + 1) ElseIf Pos(as_datetime,'/') > 0 Then ls_format = '/' ll_y = Pos(as_datetime,'/') ll_m = Pos(as_datetime,'/',ll_y + 1) ll_d = Pos(as_datetime,' ',ll_m + 1) ll_h = Pos(as_datetime,':') ll_i = Pos(as_datetime, ':', ll_h + 1) ElseIf Pos(as_datetime,'.') > 0 Then ls_format = '.' ll_y = Pos(as_datetime,'.') ll_m = Pos(as_datetime,'.',ll_y + 1) ll_d = Pos(as_datetime,' ',ll_m + 1) ll_h = Pos(as_datetime,':') ll_i = Pos(as_datetime, ':', ll_h + 1) End If ls_y = Mid(as_datetime,1,ll_y - 1) ls_m = Mid(as_datetime,ll_y + 1,ll_m - 1 - ll_y) ls_d = Mid(as_datetime,ll_m + 1,ll_d - 1 - ll_m) ls_h = Mid(as_datetime,ll_d + 1,ll_h - 1 - ll_d) ls_i = Mid(as_datetime,ll_h + 1,ll_i - 1 - ll_h) ls_s = Mid(as_datetime,ll_i + 1) Choose Case Lower(as_select) Case 'y' //year ls_return = ls_y Case 'm' // month ls_return = ls_m Case 'd' //day ls_return = ls_d Case 'h' // when ls_return = ls_h Case 'i' // points ls_return = ls_i Case 's' //seconds ls_return = ls_s Case 'y-m' //year-month ls_return = ls_y + ls_format + ls_m Case 'y-m-d' //year-month-day ls_return = ls_y + ls_format + ls_m + ls_format + ls_d Case 'y-m-d h' // year-month-day hour ls_return = ls_y + ls_format + ls_m + ls_format + ls_d + ' ' + ls_h Case 'y-m-d h:i' //year-month-day hour:minute ls_return = ls_y + ls_format + ls_m + ls_format + ls_d + ' ' + ls_h + ':' + ls_i Case 'y-m-d h:i:s' //year-month-day hour:minute:second ls_return = ls_y + ls_format + ls_m + ls_format + ls_d + ' ' + ls_h + ':' + ls_i + ':' + ls_s Case 'firstday' // get the first day of the month ls_return = ls_y + ls_format + ls_m + ls_format + '01' Case 'lastday' // Get the last day of the month Integer li_year,li_month li_year = Integer(ls_y) li_month = Integer(ls_m) If li_month = 12 Then li_month = 1 li_year++ Else li_month = li_month + 1 End If ls_return = String(RelativeDate(Date(String(li_year) + '-' + String(li_month) + '-01'), - 1)) End Choose Return ls_return |
(2), call the function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
String ls_datetime ls_datetime = '2021-01-01 11:12:13' gf_datetime_string(ls_datetime,'Y') //get the year gf_datetime_string(ls_datetime,'M') //get month gf_datetime_string(ls_datetime,'D') //get date gf_datetime_string(ls_datetime,'H') //When getting gf_datetime_string(ls_datetime,'I') //get points gf_datetime_string(ls_datetime,'S') //Get seconds gf_datetime_string(ls_datetime,'Y-M') // get year + month gf_datetime_string(ls_datetime,'Y-M-D') // get year + month + day gf_datetime_string(ls_datetime,'H-I') //Get hour + minute gf_datetime_string(ls_datetime,'H-I-S') // get hour + minute + second gf_datetime_string(ls_datetime,'Y-M-D H') // get year + month + day time gf_datetime_string(ls_datetime,'Y-M-D H:I') //Get year+month+day hour+minute gf_datetime_string(ls_datetime,'Y-M-D H:I:S') //Get year+month+day hour+minute+second |
13. gf_xml_get
(1), create a custom function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/* >==================================================== ==================== > Function: gf_xml_get >----------------------------------------------- ------------------- > Description: xml parsing >----------------------------------------------- ------------------- > Parameters: > string data > string label >----------------------------------------------- ------------------- > return value: string >==================================================== ==================== */ Long ll_start String ls_data, ls_data2, ls_data3 ls_data = Trim(Data) ll_start = Pos(ls_data,'<' + Label + '>') If ll_start = 0 Then Return '' Else ls_data2 = Mid(ls_data,ll_start + Len('<' + Label + '>')) ls_data3 = Mid(ls_data2,1,Pos(ls_data2,'<') - Len('<')) Return ls_data3 End If |
(2), call the function
1 2 3 4 5 6 7 8 9 |
String ls_xml ls_xml = '<?xml version="1.0" encoding="UTF-8"?>' + & '<code>' + & '<a>1</a>' + & '<b>2</b>' + & '<c>3</c>' + & '<d>4</d>' + & '</code>' gf_xml_get(ls_xml,'a') |
14. gf_dir
//Create structure st_dir
//Add parameters: string item_name, long item_type
//create function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* >==================================================== ==================== >Function: gf_dir >----------------------------------------------- ------------------- > >Description: Get all files and folders under the specified path >----------------------------------------------- ------------------- > >parameters: > string as_dir > listbox lb_filelist > > ref st_dir astr_itemlist[] >----------------------------------------------- ------------------- > >Return value: integer >==================================================== ==================== >*/ Long ll_i Long ll_Count Long ll_DirCount If Not DirectoryExists ( as_Dir ) Then Return -2 // directory does not exist End If If Right(as_Dir, 1) <> '\' Then as_Dir += '\' End If // process directory ll_DirCount = 0 lb_FileList.DirList( as_Dir + '*.*', 32784) ll_Count = lb_FileList.TotalItems() For ll_i = 1 To ll_Count If lb_FileList.Text(ll_i) = '[..]' Then Continue End If ll_DirCount++ astr_ItemList[ll_DirCount].Item_Name = lb_FileList.Text(ll_i) astr_ItemList[ll_DirCount].Item_Name = as_Dir + Mid(astr_ItemList[ll_DirCount].Item_Name, 2, Len(astr_ItemList[ll_DirCount].Item_Name) - 2) // Remove the external [] astr_ItemList[ll_DirCount].Item_Type = 2 Next // process the file lb_FileList.DirList( as_Dir + '*.*', 0) ll_Count = lb_FileList.TotalItems() For ll_i = 1 To ll_Count ll_DirCount++ astr_ItemList[ll_DirCount].Item_Name = as_Dir + lb_FileList.Text(ll_i) astr_ItemList[ll_DirCount].Item_Type = 1 Next Return ll_DirCount |
(2), call the function
1 2 |
st_dir astr_itemlist[] gf_dir('D:\',lb_1,astr_itemlist) |
15. gf_capital_amount
(1). Create a function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
/* >==================================================================== > Declare: gf_capital_amount >-------------------------------------------------------------------- > 描述: 将数值转换为大写金额 >-------------------------------------------------------------------- > 参数: > ac_money 要转换的金额 >-------------------------------------------------------------------- > 返回: 大写金额 >==================================================================== */ String ls_numstr[10] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"} String ls_monstr[20] = {"元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万","拾","佰","仟","亿", "拾","佰","仟"} Integer i Integer li_len String ls_temp String ls_char String ls_intstr String ls_decstr String ls_rtnintstr String ls_rtndecstr ls_temp = String(Abs(ac_money),"0.00") ls_intstr = Left (ls_temp,Len(ls_temp) - 3) ls_decstr = Right(ls_temp,2) li_len = Len(ls_intstr) For i = 1 To li_len ls_char = Mid(ls_intstr,li_len - i + 1,1) If ls_char = "0" Then Choose Case i Case 1,9,17 ls_rtnintstr = ls_monstr[i] + ls_rtnintstr Case 5,13 If li_len > i + 3 Then If Mid(ls_intstr,li_len - i - 2,3) <> "000" Then ls_rtnintstr = ls_monstr[i] + ls_rtnintstr End If Else ls_rtnintstr = ls_monstr[i] + ls_rtnintstr End If Case Else If Mid(ls_intstr,li_len - i + 2,1) <> "0" Then ls_rtnintstr = "零" + ls_rtnintstr End If End Choose Else ls_rtnintstr = ls_numstr[Integer(ls_char) + 1] + ls_monstr[i] + ls_rtnintstr End If Next If Long(ls_intstr) = 0 Then If Long(ls_decstr) > 0 Then ls_rtnintstr = "" Else ls_rtnintstr = "零元" End If End If If ls_decstr = "00" Then ls_rtndecstr = "整" ElseIf Mid(ls_decstr,1,1) = "0" Then ls_rtndecstr = ls_numstr[Integer(Mid(ls_decstr,2,1)) + 1] + "分" If Long(ls_intstr) > 0 Then ls_rtndecstr = "零" + ls_rtndecstr ElseIf Mid(ls_decstr,2,1) = "0" Then ls_rtndecstr = ls_numstr[Integer(Mid(ls_decstr,1,1)) + 1] + "角" Else ls_rtndecstr = ls_numstr[Integer(Mid(ls_decstr,1,1)) + 1] + "角" + & ls_numstr[Integer(Mid(ls_decstr,2,1)) + 1] + "分" End If If ac_money >= 0 Then Return ls_rtnintstr + ls_rtndecstr Else Return "负" + ls_rtnintstr + ls_rtndecstr End If |
(2). Call function
1 |
gf_capital_amount(11.22) |
16. gf_button_array
(1). Create a function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* >==================================================== ==================== > Function: gf_button_array >----------------------------------------------- ------------------- > Description: Add the button cycle to the button array >----------------------------------------------- ------------------- > Parameters: > commandbutton controls[] > ref commandbutton lbt_btn[] >==================================================== ==================== */ Long i For i = 1 To UpperBound(controls[]) If Left(ClassName(controls[i]),3) = "cb_" Then lbt_btn[UpperBound(lbt_btn) + 1] = controls[i] End If Next |
(2), call the function
1 2 3 4 |
commandbutton a[] a[1] = cb_1 a[2] = cb_2 gf_button_array(a) |
17. gf_dw_ddlb
(1). Create a function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/* >==================================================== ==================== > function: gf_dw_ddlb >----------------------------------------------- ------------------- > Description: Dynamically add the ddlb drop-down box under the data window field >----------------------------------------------- ------------------- > Parameters: > datawindow as_dw the datawindow to add drop-down box > string as_dw_column the column to add drop-down box > transaction as_affairs sql transaction to be queried > string as_table the table to query > string as_table_where where condition to query > string as_table_display The column to be displayed in the drop-down box > string as_table_data the data to be specified in the drop-down box >----------------------------------------------- ------------------- > return value: integer >==================================================== ==================== */ String dw_sql, dw_style String dw_syntax, dw_syntax_error, dw_create_error Boolean lb_flag = False String ls_data, ls_display String ls_value datastore dw_new dw_new = Create datastore as_dw.Modify(as_dw_column + ".ddlb.case='any'") as_dw.Modify(as_dw_column + ".ddlb.vscrollbar='yes'") If as_table_where = '' Or IsNull(as_table_where) Then dw_sql = "select distinct " + as_table_display + ',' + as_table_data + " From " + as_table Else dw_sql = "select distinct " + as_table_display + ',' + as_table_data + " From " + as_table + ' where ' + as_table_where End If dw_style = "style(type=grid)" dw_syntax = sqlca.SyntaxFromSQL(dw_sql, dw_style, dw_syntax_error) If Len(dw_syntax_error) > 0 Then MessageBox("Prompt", "Error constructing sql data source: " + dw_syntax_error) Return - 1 End If dw_new. Create(dw_syntax, dw_create_error) If Len(dw_create_error) > 0 Then MessageBox("Prompt", "Error creating data window: " + dw_create_error) Return - 1 End If dw_new.SetTransObject(sqlca) dw_new. Retrieve() Long i,ll_rowcount ll_rowcount = dw_new.RowCount() If ll_rowcount > 0 Then For i = 1 To ll_rowcount ls_display = String(dw_new.GetItemString(i,as_table_display)) ls_data = String(dw_new.GetItemString(i,as_table_data)) If Not lb_flag Then ls_value = as_dw_column + ".values='" lb_flag = True Else ls_value += "/" End If ls_value += Trim(ls_display) + "~t" + Trim(ls_data) Next ls_value += "'" If as_dw.Modify(ls_value) <> "" Then Return - 1 End If Else Return - 1 End If Return 0 |
(2), call the function
1 |
gf_dw_ddlb(dw_1,'name',sqlca,'user','name','id') |
Good Luck!