10
I have a component that has the function of connecting to a specific hardware. It connects through the network or serial port. Something like the code below:
TConexao = (conRede, conSerial);
THardware = class(TComponent)
private
FAtivo: Boolean;
FPortaSerial: string;
FTipoConexao: TConexao;
FPorta: Integer;
FIP: string;
procedure SetAtivo(const Value: Boolean);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Ativar;
procedure Desativar;
published
property Ativo: Boolean read FAtivo write SetAtivo;
property TipoConexao: TConexao read FTipoConexao write FTipoConexao;
property IP: string read FIP write FIP;
property Porta: Integer read FPorta write FPorta;
property PortaSerial: string read FPortaSerial write FPortaSerial;
end;
To connect through the network I use the IP and Port properties and to connect through the serial I use only serial port. In Object Inspector when setting Network Link I would like you to show only the IP and Port properties, if it were changed to Serial show only the Portaserial property. It is possible?
Hello Patrick, welcome to [en.so]. Are you willing to change which properties will be visible according to the type of connection? If that is it, you can [Dit] the question by adding this information to make it clear so that we can help you.
– Caputo
Oops! Amended question @Pagenotfound
– Patrick Alves
It is not possible. You could disable filling out information through Get and Set. Ex: Getip, Setip and implement control within these methods. Another way is to create 2 components for configuration, and use them by composition within your class. This way, you control the instantiation of one or the other depending on the case
– EProgrammerNotFound
Thanks @Caputo That’s right. I’ve changed the question.
– Patrick Alves
@Pagenotfount in the case I would define in the Typo Set the destruction and creation of these components?
– Patrick Alves
If you choose this way, yes.
– EProgrammerNotFound
Dear @Pagenotfound I added the two components as suggested and when setting Tipoconexao set the creation of the associated component and works satisfactorily thanks for the tip. The only thing is that the property is still visible and cannot be changed. You have some other alternative?
– Patrick Alves
What’s wrong with being shown?
– EProgrammerNotFound
The most correct way would be to create a custom property editor, inheriting from Tpropertyeditor. Read this article. In this custom editor you can do whatever you want, including a Form with a selector with its options and configure panels to be shown at each option. When choosing and setting options, apply them to the component at runtime. Search more using the term Propertyeditor
– EProgrammerNotFound
Remember to make sure your settings are being saved when you close Delphi and open it again.
– EProgrammerNotFound
Okay, thank you!!!
– Patrick Alves
How about doing it this way? This looks like a typical interface situation. http://www.devmedia.com.br/desmistificando-as-interfaces/347
– Ricardo da Rocha Vitor