Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
Syndicate this content

Using databases with TX Text Control - Part II

This source code snippet requires TX Text Control ActiveX
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:

Delphi

  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3. vData: Variant;
  4. pPtr: Pointer;
  5. strData: AnsiString;
  6. nLen: Integer;
  7. begin
  8. strData := 'Test text';
  9. nLen := Length(strData);
  10.  
  11. // copy text into byte array
  12. vData := VarArrayCreate([0, nLen], varByte);
  13. pPtr := VarArrayLock(vData);
  14. StrCopy(pPtr, PChar(strData));
  15. VarArrayUnlock(vData);
  16.  
  17. TXTextControl1.LoadFromMemory(vData, 1, False);
  18.  
  19. // release data
  20. VarClear(vData);
  21. end;

Visual C++

  1. void loadString()
  2. CString csTemp = "Test text";
  3. VARIANT var1, var2, var3;
  4.  
  5. // create array
  6. SAFEARRAYBOUND bound;
  7. bound.lLbound = 0;
  8. bound.cElements = csTemp.GetLength() + 1; // inclusive '\0'
  9.  
  10. VariantInit(&var1);
  11. var1.parray = SafeArrayCreate(VT_UI1, 1, &bound);
  12. var1.vt = VT_UI1 | VT_ARRAY;
  13.  
  14. // copy data into array
  15. LPVOID lpData;
  16.  
  17. SafeArrayAccessData(var1.parray, &lpData);
  18. CopyMemory(lpData, (LPCTSTR) csTemp, bound.cElements);
  19. SafeArrayUnaccessData(var1.parray);
  20.  
  21. var2.intVal = 5; // RTF format
  22. var2.vt = VT_I4;
  23.  
  24. var3.boolVal = true; // replace selection
  25. var3.vt = VT_BOOL;
  26. m_TxTranslate.LoadFromMemory(var1, var2, var3);
  27.  
  28. VariantClear(&var1);
  29. }

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.

External verification page for ISO 9000:2000 certificate
ISO 9001:2000
certified