Einzelnen Beitrag anzeigen

mcinternet

Registriert seit: 22. Apr 2010
Ort: Odenwald
193 Beiträge
 
Delphi 10.3 Rio
 
#1

Dynamisch erzeugte TListbox zeigt nicht die Vorauswahl an

  Alt 3. Aug 2019, 15:09
Hallo die Gemeinde,

ich habe eine Komponente gebastelt, welche eine TListBox enthält. Klappt auch alles soweit und gut.
Nur eines stört noch: Die TListBox soll bitteschön die Zeile highlighten, welche über .selected, bzw. .itemindex vorausgewählt ist, OHNE dass man was anklicken muss.
Das tut sie leider nicht.

Bild 1 = IST - Zustand
Bild 2 = Soll - Zustand (nach Klick)

Wer hat da eine Idee?

// funktioniert leider nicht!
fLBDruckerAuswahl.ItemIndex := fDruckerNummer;
fLBDruckerAuswahl.Selected[fDruckerNummer];



Die komplette Unit mal anbei



Code:
unit uDruckerPanel;

interface

  uses System.Classes, Vcl.ExtCtrls, Vcl.StdCtrls, AboutOwn;



  Type


    TDruckerPanel = Class(TPanel)
    private

      flblUeberschrift : TLabel;
      flblAnzeigen    : TLabel;
      flblAuswahl     : TLabel;
      flblAuswahlnr   : Tlabel;
      fDrucker : String;
      fDruckerNummer : Integer;
      fDatum        : TDate;
      fVersion      : String;
      fEntwickler   : String;
      fPanTop       : TPanel;
      fPanMid       : TPanel;
      fPanBottom    : TPanel;
      fLBDruckerAusWahl : TListbox;
      FOnChange     : TNotifyEvent;

      Procedure Init;
      Procedure SetScrollWidth(sWidth : Integer);
      function GetScrollWidth : Integer;
      Procedure fLBDruckerAuswahlDoppelKlick(Sender: TObject);
    public
      procedure fLBDruckerAuswahlClick(Sender: TObject);
      Constructor Create(AOwner : TComponent); override;
      Destructor Destroy; override;
      procedure CreateWnd; override;
    published
      property Datum : TDate read fDatum;
      property Version : String read fVersion;
      property Entwickler : String read fEntwickler;
      //----------
      property Drucker : String read fDrucker; // write WriteDruckerName;
      property DruckerNummer : Integer read fDruckerNummer; // write WriteDruckerNummer;
      property ScrollWidth  : Integer read GetScrollWidth write SetScrollWidth;
      property ListBox : TListBox read fLBDruckerAusWahl;
      property OnChange : TNotifyEvent Read FOnChange write FOnChange;
      property PanelTop : TPanel read fPanTop;
      property PanelBottom : TPanel read fPanBottom;
  End;



implementation

//uses Printers;
uses System.SysUtils, VCL.Forms, System.IniFiles, VCL.Printers, VCL.Controls, Vcl.Graphics, VCL.Dialogs;

Procedure TDruckerPanel.SetScrollWidth(sWidth : Integer);
begin
  fLBDruckerAusWahl.ScrollWidth := sWidth;
end;

function TDruckerPanel.GetScrollWidth : Integer;
begin
  result := fLBDruckerAusWahl.ScrollWidth;
end;

Procedure TDruckerPanel.Init;
begin
   fVersion   := sVersion;
   fEntwickler := sEntwickler;
   fDatum     := strtodate(sDatum);
   height     := 400;
   width      := 400;
end;

procedure TDruckerPanel.CreateWnd;
begin
  inherited;
  if (csDesigning in ComponentState) then
  begin
    fLBDruckerAuswahl.Items     := Printer.Printers;
    fDruckerNummer              := Printer.PrinterIndex;
    fDrucker                    := Printer.Printers[Printer.PrinterIndex];
    flblAuswahlnr.Caption       := IntToStr(Printer.PrinterIndex);
    flblAuswahl.Caption         := fDrucker;
    Application.ProcessMessages;
  end;
end;

Constructor TDruckerPanel.Create(AOwner : TComponent);
begin
  inherited create(AOwner);
  Init;

  ParentFont := false;
  ParentColor := false;
  color := clMoneyGreen;

  fPanTop := TPanel.Create(self);
  fPanTop.Parent := self;
  fpanTop.Height := 25;
  fPanTop.Align := alTop;
  fPanTop.ParentColor := false;
  fPanTop.color := clMoneyGreen;

  fPanBottom := TPanel.Create(self);
  fPanBottom.Parent := self;
  fPanBottom.Height := 25;
  fPanBottom.Align := alBottom;

  fPanMid := TPanel.Create(self);
  fPanMid.Parent := self;
  fPanMid.Align := alClient;

  flblUeberschrift := TLabel.Create(self);
  flblUeberschrift.Parent := fPanTop;
  flblUeberschrift.Left := 20;
  flblUeberschrift.Top := 5;
  flblUeberschrift.Caption := 'Druckerauswahl - Doppelkick = Einstellungen';
  flblUeberschrift.ParentFont := false;
  flblUeberschrift.Font.Size := 10;

  fLBDruckerAusWahl := TListbox.Create(self);
  fLBDruckerAusWahl.SetSubComponent(True);
  fLBDruckerAusWahl.Parent := fPanMid;
  fLBDruckerAusWahl.Align := alClient;
  fLBDruckerAusWahl.ParentFont := false;
  fLBDruckerAusWahl.Font.Size := 8;

  flblAnzeigen := TLabel.Create(self);
  flblAnzeigen.Parent := fPanBottom;
  flblAnzeigen.Top := 5;
  flblAnzeigen.Left := 14;
  flblAnzeigen.Caption := 'Auswahl:';
  flblAnzeigen.ParentFont := false;
  flblAnzeigen.Font.Size := 10;


  flblAuswahlnr := TLabel.Create(self);
  flblAuswahlnr.Parent := fPanBottom;
  flblAuswahlnr.Top := flblAnzeigen.Top;
  flblAuswahlnr.Left := 73;
  flblAuswahlnr.ParentFont := false;
  flblAuswahlnr.Font.Size := 10;

  flblAuswahl := TLabel.Create(self);
  flblAuswahl.Parent := fPanBottom;
  flblAuswahl.Top := flblAnzeigen.Top;
  flblAuswahl.Left := 83;
  flblAuswahl.ParentFont := false;
  flblAuswahl.Font.Size := 10;



  if not (CsDesigning in ComponentState) then
  begin
    fLBDruckerAuswahl.Items     := Printer.Printers;
    fDruckerNummer              := Printer.PrinterIndex;
    fDrucker                    := fLBDruckerAuswahl.Items[fDruckerNummer];
    flblAuswahlnr.Caption       := IntToStr(fDruckerNummer);
    flblAuswahl.Caption         := fDrucker;

    // funktioniert leider nicht!
    fLBDruckerAuswahl.ItemIndex := fDruckerNummer;
    fLBDruckerAuswahl.Selected[fDruckerNummer];

    Application.ProcessMessages;
    fLBDruckerAuswahl.OnClick   := fLBDruckerAuswahlClick;
    fLBDruckerAuswahl.OnDblClick := fLBDruckerAuswahlDoppelKlick;
  end;
end;

Destructor TDruckerPanel.Destroy;
begin
  freeandnil(flblAuswahl);
  freeandnil(flblAuswahlnr);
  freeandnil(flblAnzeigen);
  freeandnil(fLBDruckerAusWahl);
  freeandnil(flblUeberschrift);
  FreeAndNil(fPanMid);
  FreeAndNil(fPanBottom);
  FreeAndNil(fPanTop);
  inherited;
end;

procedure TDruckerPanel.fLBDruckerAuswahlClick(Sender: TObject);
begin
  fDrucker              := fLBDruckerAuswahl.Items[fLBDruckerAuswahl.ItemIndex]; //
  flblAuswahl.Caption   := fDrucker;
  fDruckerNummer        := fLBDruckerAuswahl.ItemIndex;
  flblAuswahlnr.Caption := IntToStr(fDruckerNummer);
  printer.PrinterIndex  := fDruckerNummer;
  if Assigned(fOnChange) then fOnChange(Self);
end;

Procedure TDruckerPanel.fLBDruckerAuswahlDoppelKlick(Sender: TObject);
  var printDialog : tprintdialog;
begin
  fLBDruckerAuswahlClick(Sender);
  printDialog := tprintdialog.Create(self);
  printDialog.Execute;
  fLBDruckerAuswahl.ItemIndex := printer.PrinterIndex;
  fLBDruckerAuswahlClick(Sender);
  FreeAndNil(printDialog);
end;



end.

Beste Grüße

mcinternet
Miniaturansicht angehängter Grafiken
billddp1.png   bilddp2.png  
Jörg

Geändert von mcinternet ( 3. Aug 2019 um 15:17 Uhr)
  Mit Zitat antworten Zitat