nasa
nasa

Reputation: 13

insert config in the combobox list item

create a new project. I insert one ComboBox into the project and 1 editbox into the project. then I menambhkan string item in combobox list of items

server 1 au
server 2 uk
server 3 my

after making a string in the combobox then formed a list in combobox. all I want is to add a config in the string. like if i choose server 1 au in the editbox combobox displays the server address as ns.au.server.com

ns.au.server.com server address -> string to server 1 au

combobox1-> select string item-> Server 1 au

editbox1 then displays the address ns.au.server.com

Upvotes: 0

Views: 1075

Answers (2)

Lars
Lars

Reputation: 1438

The functionality you ask for is available using TStrings Name-Value pairs.

type
  TFormMain = class(TForm)
    ComboBox1: TComboBox;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private

  public
    FServerConfig : TStrings;
  end;

var
  FormMain: TFormMain;

implementation
{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
var i : integer;
begin
  FServerConfig := TStringList.Create;

  //Add Name = Value pairs
  FServerConfig.Append('Server 1 AU=ns.au.server.com');
  FServerConfig.Append('Server 2 UK=ns.uk.server.com');
  FServerConfig.Append('Server 3 MY=ns.my.server.com');

  for i := 0 to FServerConfig.Count - 1 do
    ComboBox1.Items.Append(FServerConfig. Names[i]);
end;

procedure TFormMain.ComboBox1Change(Sender: TObject);
begin
  Edit1.Text := FServerConfig.Values[ComboBox1.Text];
end;

Upvotes: 0

Andy_D
Andy_D

Reputation: 2350

One approach is to define a class that holds a name and configuration string for your servers like this :-

Type
  TServer = Class
  Private
    FName : String;
    FConfig : String;
  Public
    Property Name : String Read FName Write FName;
    Property Config : String Read FConfig Write FConfig;
  End;

You can then create instances of your objects and use the Item's AddObject method to add them to your combo box like this :-

Var
  lItem : TServer;
Begin
  lItem := TServer.Create;
  lItem.Name := 'Server 1 AU';
  lItem.Config := 'ns.au.server.com';
  ComboBox1.Items.AddObject(lItem.Name, lItem);
// Add more as required.
End;

Then in your Combobox's OnChange event you can write :-

Var
  lIndex : Integer;
Begin
  lIndex := ComboBox1.ItemIndex;
  If (lIndex <> -1) Then
    Edit1.Text := TServer(ComboBox1.Items.Objects[lIndex]).Config;
End;

Upvotes: 1

Related Questions