| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| Author: | TX Text Control Support Department |
| Language: | Visual Basic, Visual C++ |
| Version: | 1.1 |
| Released: | March 20, 2001 |
| Last modified: | January 11, 2008 |
| Requirements: | TX Text Control ActiveX with Delphi/Visual C++ |
This weeks tip is not really a tip - we are just revisiting the example of the last week. It showed how to load data from a database into the TX without using it as a bound control. As I am sure that you remember, the trick was to use the LoadFromMemory and SaveToMemory methods to load a byte array that contains formatted data (or unformatted such as a simple string) into TX or save the contents to such a byte array.
VB provides a simple method to do the conversion from a string to a byte array and vice versa, but Delphi users or VC++ users have to deal with the Variant data type and most users don't know how to do that. So, let's take a look at how you so this:
procedure TForm1.Button1Click(Sender: TObject); var vData: Variant; pPtr: Pointer; strData: AnsiString; nLen: Integer; begin strData := 'Test text'; nLen := Length(strData); // copy text into byte array vData := VarArrayCreate([0, nLen], varByte); pPtr := VarArrayLock(vData); StrCopy(pPtr, PChar(strData)); VarArrayUnlock(vData); TXTextControl1.LoadFromMemory(vData, 1, False); // release data VarClear(vData); end;
void loadString() CString csTemp = "Test text"; VARIANT var1, var2, var3; // create array SAFEARRAYBOUND bound; bound.lLbound = 0; bound.cElements = csTemp.GetLength() + 1; // inclusive '\0' VariantInit(&var1); var1.parray = SafeArrayCreate(VT_UI1, 1, &bound); var1.vt = VT_UI1 | VT_ARRAY; // copy data into array LPVOID lpData; SafeArrayAccessData(var1.parray, &lpData); CopyMemory(lpData, (LPCTSTR) csTemp, bound.cElements); SafeArrayUnaccessData(var1.parray); var2.intVal = 5; // RTF format var2.vt = VT_I4; var3.boolVal = true; // replace selection var3.vt = VT_BOOL; m_TxTranslate.LoadFromMemory(var1, var2, var3); VariantClear(&var1); }
The examples above load the string "Test text" directly into the TX. We could equally use a text format that supports formatting here, such as HTML and RTF. The only thing you need to change is the format parameter of the LoadFromMemory method.
In Delphi you can use the VarArrayCreate method to create the Variant. Then the Variant needs to be locked. This returns a pointer to the internal byte array and then StrCopy can be used to copy the string to the array. The second and third parameter of the LoadFromMemory are also prototyped as Variants, but you can simply treat them as normal variables and Delphi performs the conversion for you.
In VC++ the procedure is similar. First of all, the Variant has to be initialized. Then the array is created with SafeArrayCreate. Afterwards, the Variant is locked with SafeArrayAccessData and the string is copied to the array with CopyMemory. VC++ expects the second and third parameter to be Variants as well. This means you must not forget to set up two more Variants.
After you are done with the Variants, you need to free the allocated memory. This is done with VarClear(Delphi) resp. VariantClear(VC++).
Now it should be fairly easy to use the LoadFromMemory method with a database in your preferred languaged language.