How can I add components at run time in C++ Builder XE7 - c++builder

I want to add some components at run time. I use C++ Builder XE 7 and vcl. How can I add components to the class Form in run time? Is that possible?

I found the solution. Thank you.
Here is an example:
__fastcall TForm2::TForm2(TComponent* Owner): TForm(Owner)
{
TButton* b = new TButton(this);
b->Parent = this;
b->Height = 100;
b->Width = 100;
b->Left = 0;
b->Top = 0;
b->Caption = "Testing";
b->Visible = true;
b->Enabled = true;
}

Related

Virtualtreeview for Borland C++ Builder 6

I downloaded the VirtualTreeView component for Borland C ++ Builder 6.0 and I'm trying to learn how to use it. Unfortunately I can not find any code for this compiler that serves as an example and I am based on a Delphi code that I have given.
I have created a project with a single form that contains only one TVirtualStringTree. I am using this code to try to understand how it works.
In the CPP:
void __fastcall TForm1 :: FormCreate (TObject * Sender)
{
AnsiString cLiteral;
PTrecBase pRecData;
PVirtualNode Node;
VST-> BeginUpdate ();
VST-> Clear ();
VST-> NodeDataSize = sizeof (TRecBase);
for (int nItem = 0; nItem <10; nItem ++)
{
cLiteral = "Node" + IntToStr (nItem);
Node = VST-> AddChild (NULL);
pRecData = (PTrecBase) VST-> GetNodeData (Node);
pRecData-> Literal = cLiteral;
for (int nSub = 0; nSub <5; nSub ++)
{
Node = getNodeDondeInsert (cLiteral);
Node = VST-> AddChild (Node);
pRecData = (PTrecBase) VST-> GetNodeData (Node);
pRecData-> Literal = cLiteral + "Sub" + IntToStr (nSub);
}
}
VST-> EndUpdate ();
}
In the H:
class TForm1: public TForm
{
__published: // IDE-managed Components
TVirtualStringTree * VST;
void __fastcall FormCreate (TObject * Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1 (TComponent * Owner);
};
struct TRecBase
{
AnsiString Literal;
};
typedef TRecBase * PTrecBase;
However I can not get the literal that I want to show. Only this is shown:
And I do not know what I'm doing wrong so that you do not see the literals that I'm defining. Does anyone have a code for C ++ Builder that can guide me? Thank you.
You are storing string data for each node, but you have no event handlers assigned to the TreeView, particularly OnGetText, to supply those strings to the TreeView when it is being rendered. This is a virtual control, you have to supply it with data when it asks you for it.
There is a C++Builder demo available on VirtualTreeView's GitHub repository
Thanks for your reply.
The code that you have indicated to me I have already tried it and I am not able to understand it because I can not find in which part of it it is necessary to indicate the literal of the node. The attached image is the result I get with that example code.
Form CBMininal
As you can see the result is practically the same as in the image that I went up yesterday. What I'm most sorry about is that even though I searched, I did not find any sample code for this component for Borland C ++.
What's more: If I run the example with the debugger, the same thing happens to me as with my code.
In my code when I execute the line pRecData->Literal = cLiteral; I can see in the debugger that it has the calculated values (Node 0, Node 1, etc.) and in the example, when this one is executed (Text = Data->Caption;) I can see that both Text and Data->Caption contain the value, for example, Level 0, Index 5 but this literal is not shown in the TVirtualStringTree.

How to know the Control is TLabel?

My Environment: C++ Builder XE4
how to copy all the TLabels parented with a TPanel on delphi to another TPanel?
I would like to implement above code in C++ Builder.
I do not know how to implement below in C++ Builder.
if ParentControl.Controls[i] is TLabel then
Are there any functions to get type as TLabel or some other?
You can use ClassType method as:
if(Controls[i]->ClassType() == __classid(TLabel))
{
...
}
Use dynamic_cast:
if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)
Here is a translation of that code:
void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
for(int i = 0; i < ParentControl->ControlCount; ++i)
{
if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)
{
TLabel *ALabel = new TLabel(DestControl);
ALabel->Parent = DestControl;
ALabel->Left = ParentControl->Controls[i]->Left;
ALabel->Top = ParentControl->Controls[i]->Top;
ALabel->Width = ParentControl->Controls[i]->Width;
ALabel->Height = ParentControl->Controls[i]->Height;
ALabel->Caption= static_cast<TLabel*>(ParentControl->Controls[i])->Caption;
//you can add manually more properties here like font or another
}
}
}
With that said, this would be slightly more efficient:
void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
int count = ParentControl->ControlCount;
for(int i = 0; i < count; ++i)
{
TLabel *SourceLabel = dynamic_cast<TLabel*>(ParentControl->Controls[i]);
if (SourceLabel != NULL)
{
TLabel *ALabel = new TLabel(DestControl);
ALabel->Parent = DestControl;
ALabel->SetBounds(SourceLabel->Left, SourceLabel->Top, SourceLabel->Width, SourceLabel->Height);
ALabel->Caption = SourceLabel->Caption;
//you can add manually more properties here like font or another
}
}
}
I found ClassName() method.
Following seems working.
static bool isTCheckBox(TControl *srcPtr)
{
if (srcPtr->ClassName() == L"TCheckBox") {
return true;
}
return false;
}

How to check if a component is TEdit type in c++builder?

How do I check through all the components of a form and verify that components are of type TEdit?
You can use the dynamic_cast operator.
Excuse me if I'm wrong, but won't embarcadero automatically add all form-component object pointers to the class definition ( in the header file )..
Such as:
class TFormSomeForm : public TForm
{
__published:
TEdit *SomeEditBox;
TEdit *AnotherEditBox;
...
}
Meaning that you can tell from the header which components are of type TEdit.
Or you can click on the components in the Design View and the Object Inspector will show the type.
My function sets Text property of all edits in a TWinControl and its children.
void __fastcall SetEditsText(TWinControl* winControl, UnicodeString editsText)
{
for (int c = 0; c < winControl->ControlCount; c++)
{
TControl* ctrl = winControl->Controls[c];
TWinControl* wc = dynamic_cast<TWinControl*>(ctrl);
// Check if it's grouping component
if (wc != NULL)
{
// Set edits of children
SetEditsText(wc, editsText);
}
else
{
if (ctrl->ClassType() == __classid(TEdit))
{
TEdit* ecomp = (TEdit*) ctrl;
ecomp->Text = editsText;
}
}
}
}
Using:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SetEditsText(form1, ""); // Clear all edits
}

Call global function from within Delphi class's method

Is it possible to call global methods from within a class where they are obscured by member functions of the same name?
I know in C++ you have the following syntax:
int var = 0;
void temp() {
int var = 2;
::var = var;
} //Global var is set to 2
Yes you can by using the name of the unit instead of ::
Like:
unit1.var := 2;
See for more details:
http://delphi.about.com/od/beginners/l/aa060899.htm
You can try
UnitName.VarName := 2

An activex control on this page might be unsafe

I created a com component in C#, which I registered by using Regasm. I am able now to use this in IE by using ActiveXObject(...). However this only works when I change my IE security settings and allow to run unsigned activex controls, in which case I get the message:
An ActiveX control on this page might be unsafe to interact with other parts of the page. Do you want to allow this interaction?
I always want IE to allow this interaction without the prompt. Does anybody know how this can be done?
Thanks
Your ActiveX control must implement the IObjectSafety interface in order for IE to stop showing the "unsafe?" prompt. I did this several years ago for a VB6 ActiveX control. In the 5th step of This page is shown how to do it in .Net.
I have already faced this problem.After long walk i have solved this problem.In your activeX class simply inherit IObjectSafety class.See the image bellow :
IObjectSafety Class given bellow:
[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid,[MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions,[MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid,[MarshalAs(UnmanagedType.U4)] int dwOptionSetMask,[MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
public class IObjectSafetyImpl : IObjectSafety
{
private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";
private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int _OK = 0;
private const int _FAIL = unchecked((int)0x80004005);
private const int _NOINTERFACE = unchecked((int)0x80004002);
private bool _fSafeForScripting = true;
private bool _fSafeForInitializing = true;
public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
{
int Result = _FAIL;
string strGUID = riid.ToString("B");
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
Result = _OK;
pdwEnabledOptions = 0;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
Result = _OK;
pdwEnabledOptions = 0;
if (_fSafeForInitializing == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
break;
default:
Result = _NOINTERFACE;
break;
}
return Result;
}
public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
int Result = _FAIL;
string strGUID = riid.ToString("B");
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) &&
(_fSafeForScripting == true))
Result = _OK;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) &&
(_fSafeForInitializing == true))
Result = _OK;
break;
default:
Result = _NOINTERFACE;
break;
}
return Result;
}
}
You can create a .reg file to modify the registry key like this:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0]
"1201"=dword:00000000
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0]
"1201"=dword:00000000
Start -> Run -> regedit
Go to
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVer
sion\Internet Settings\Zones\0
Doubleclick 1201 and change the value to 0 (it was
probably 1)
Close Registry Editor
I think you can just set the sites trust level to full.
Tools->Internet Options->Security->Trusted Sites->Sites button
As for signing the ActiveX see this article. However you will still have to allow the ActiveX (it'll just show you as the author). See Ryan's answer for how to allow the ActiveX for this site.

Resources