String Resource Component
for C++ Builder
Here is the source code for a string resource component you can use instead
of adding RC files with string tables to your project. Just compile and install
the component and you are ready to drop one on the form. The Component can be
used as a MRU list as well as a String Resource. There are only a few properties
and functions and they are:
| Published Properties |
| int |
MruMax |
Setting the max number of lines in the list if the component are used as an MRU list |
| bool |
Sorted |
Determines whether or not you want the strings in the list to be sorted |
| TStrings* |
Strings |
The actual container for your strings |
| Public Properties |
| int |
Count |
The number of lines in the list |
| String |
RcString[int i] |
Returns the line at int i position in the list the
index is 0 based |
| String |
Text |
Return all strings in the list and the lines are
separated by line breaks |
| String |
Value[String name] |
This one is used when the you are storing your string
in the Name=Value format |
| Public Functions (used with the MRU list) |
| int |
IndexOf(String str) |
Find a certain string in the list and return the index.
Return -1 if not found |
| void |
AddMru(String str) |
Insert a string at the front, remove any duplicates and
reduce list to MruMax |
| void |
SaveToFile(String file) |
Save the file to a UFT8 encoded file |
| void |
LoadFromFile(String file) |
Load the file and expect to receive a UFT8 encoded file |
When you use this component you can take advantage of
all the features that comes with the VCL TStringList, such at using
Name=Value pair in the list, just like it is done in an INI
file, that way you can easily give each string resource a name and fetch it by
property Value[String name].
You can use this source code as you see fit and at your own risk, as long as
you don't publish it anywhere and as long as you don't claim to have written it.
Links to this code page will be greatly appreciated.
The .h file:
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
| #ifndef AjStringRCH
#define AjStringRCH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
//---------------------------------------------------------------------------
class PACKAGE TAjStringRC : public TComponent
{
private:
protected:
int FMruMax;
TStringList* FStrings;
String __fastcall getValue( String name ){return FStrings->Values[name];}
void __fastcall setValue( String name, String Val );
String __fastcall getRcString( int i ){return FStrings->Strings[i];}
void __fastcall setRcString( int i, String Val );
String __fastcall getText(){return FStrings->Text;}
void __fastcall setText( String Val ){FStrings->Text = Val;}
int __fastcall getCount(){return FStrings->Count;}
bool __fastcall getSorted(){return FStrings->Sorted;}
void __fastcall setSorted( bool Val ){FStrings->Sorted = Val;};
void __fastcall setStrings( TStrings* Val ){FStrings->Assign( Val );};
TStrings* __fastcall getStrings(){return FStrings;}
public:
//=========================================
__fastcall TAjStringRC(TComponent* Owner);
__fastcall ~TAjStringRC();
//=========================================
int __fastcall IndexOf( String str ){ return FStrings->IndexOf( str ); }
void __fastcall AddMru( String str );
void __fastcall SaveToFile(String file);
void __fastcall LoadFromFile(String file);
__property int Count = {read=getCount};
__property String RcString[int i] = {read=getRcString, write=setRcString};
__property String Text = {read=getText, write=setText};
__property String Value[String n] = {read=getValue, write=seValue};
__published:
__property int MruMax = {read=FMruMax, write=FMruMax};
__property bool Sorted = {read=getSorted, write=setSorted};
__property TStrings* Strings = {read=getStrings, write=setStrings};
};
//---------------------------------------------------------------------------
#endif
|
The .Cpp file:
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
| #include <vcl.h>
#pragma hdrstop
#include "AjStringRC.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TAjStringRC *)
{
new TAjStringRC(NULL);
}
//---------------------------------------------------------------------------
namespace Ajstringrc
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TAjStringRC)};
RegisterComponents(L"AJ-Utils", classes, 0);
}
}
//---------------------------------------------------------------------------
__fastcall TAjStringRC::TAjStringRC(TComponent* Owner)
: TComponent(Owner)
{
FStrings = new TStringList();
}
//---------------------------------------------------------------------------
__fastcall TAjStringRC::~TAjStringRC()
{
delete FStrings;
}
//---------------------------------------------------------------------------
void __fastcall TAjStringRC::AddMru( String str )
{
int idx = IndexOf( str );
if( idx == 0 ) // string is in the reight place
return;
if( idx > 0 )
FStrings->Delete( idx );
FStrings->Insert( 0, str );
while( Count > FMruMax )
FStrings->Delete( Count - 1 );
}
//---------------------------------------------------------------------------
void __fastcall TAjStringRC::SaveToFile(String file)
{
FStrings->SaveToFile( file, TEncoding::UTF8 );
}
//---------------------------------------------------------------------------
void __fastcall TAjStringRC::LoadFromFile(String file)
{
FStrings->LoadFromFile( file, TEncoding::UTF8 );
}
//---------------------------------------------------------------------------
void __fastcall TAjStringRC::setValue( String name, String Val )
{
FStrings->Values[name] = Val;
}
//---------------------------------------------------------------------------
void __fastcall TAjStringRC::setRcString( int i, String Val )
{
FStrings->Strings[i] = Val;
}
//---------------------------------------------------------------------------
|
Last Updated: Aug-21-2026 ©
Copyright 2003-2026 Asger-P