Como passar itens de um ListBox para mais de um label ?

Delphi

26/08/2013

Pessoal,
Como passar itens de um ListBox para mais de um label , supondo que tenha no ListBox as seguintes disciplinas:

PORTUGUÊS
MATEMÁTICA
CIÊNCIAS
HISTÓRIA

No formulário coloquei os Label:

LABEL1
LABEL2
LABEL3
LABEL4

Se o usuário selecionou apenas as disciplinas: PORTUGUÊS E CIÊNCIA, então ficaria assim:

LABEL1 := PORTUGUÊS
LABEL2 := CIÊNCIAS
Osmar

Osmar

Curtidas 0

Respostas

Joel Rodrigues

Joel Rodrigues

26/08/2013

Antes de partirmos para uma possível solução, cabe uma pergunta: isso é realmente necessário? Considerando os vários controles visuais de que dispomos, acho que preencher labels dessa forma é um pouco 'desnecessário'. Claro que só você pode dizer se TEM que ser assim, mas eu buscaria outra solução mais prática e fácil de manter.
GOSTEI 0
Osmar

Osmar

26/08/2013

Joel,
Desculpe-me se não fui claro, o ListBox tem 12 disiciplinas, o multselect = True, preciso percorrer as 12 disciplinas para saber quais o usuário selecionou, lembro também que ele só pode selecionar no máximo 4 das 12 disciplinas, coloquei no formulário 4 ppLabel (report Builder), supondo que o usuário selecionou apenas a 6ª e a 10ª disciplinas, neste tem uma dica, mas está imcompleta, veja abaixo o código, então gostaria que ficasse assim:

ppLabel1 := 6ª Disciplina
ppLabel2 := 10ª Disciplina
ppLabel3.visible := false.
ppLabel4.visible := false.

procedure TFrmRelCompromisso.BtnImprimRequerMatriculaClick(Sender: TObject);
var
lbl: TLabel;
i: Integer;
begin

for i := 0 to ListBox1.Items.Count -1 do

begin

if not ListBox1.Selected[i] then
continue;

lbl := TLabel.Create(Self);
lbl.Parent := Self;
lbl.Caption := ListBox1.Items[i];
end;

ppReport1.Print;
close;
end;
GOSTEI 0
Osmar

Osmar

26/08/2013

Joel,
Desculpe-me se não fui claro, o ListBox tem 12 disiciplinas, o multselect = True, preciso percorrer as 12 disciplinas para saber quais o usuário selecionou, lembro também que ele só pode selecionar no máximo 4 das 12 disciplinas, coloquei no formulário 4 ppLabel (report Builder), supondo que o usuário selecionou apenas a 6ª e a 10ª disciplinas, neste site tem uma dica, mas está imcompleta, veja abaixo o código, então gostaria que ficasse assim:

ppLabel1 := 6ª Disciplina
ppLabel2 := 10ª Disciplina
ppLabel3.visible := false.
ppLabel4.visible := false.

procedure TFrmRelCompromisso.BtnImprimRequerMatriculaClick(Sender: TObject);
var
lbl: TLabel;
i: Integer;
begin

for i := 0 to ListBox1.Items.Count -1 do

begin

if not ListBox1.Selected[i] then
continue;

lbl := TLabel.Create(Self);
lbl.Parent := Self;
lbl.Caption := ListBox1.Items[i];
end;

ppReport1.Print;
close;
end;
GOSTEI 0
Osmar

Osmar

26/08/2013

Joel, alguma sugestão?
GOSTEI 0
Alan Souza

Alan Souza

26/08/2013

tenta assim:

var
  i, labels: Integer;
begin
  if ListBox.SelCount > 4 then
    Application.MessageBox('Somente 4 matérias podem ser selecionadas!', 'Selecionar', MB_ICONINFORMATION)
  else
  begin
    ppReport1.ppLabel1.Visible := False;
    ppReport1.ppLabel2.Visible := False;
    ppReport1.ppLabel3.Visible := False;
    ppReport1.ppLabel4.Visible := False;
    labels := 1;
    for i := 0 to Pred(ListBox.Count) do
    begin
      if ListBox.Selected[i] then
      begin
        TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).Caption := ListBox.Items[i];
        TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).Visible := True;
        Inc(labels);
      end;
    end;
    ppReport1.Print;
    Close;
  end;
end;



obs: não fiz as validações para ver se o componente ppLabel com o nome definido existe, se não estiver com o nome correto dará erro!
GOSTEI 0
Osmar

Osmar

26/08/2013

Alan,

O sistema não aceita NADA que comece com pp após o ppReport1., coloquei em vários eventos do ppReport1, mas não reconhece, então tirei o ppReport1 e o sistema rodou, mas na hora da impressão dá pau nessa linha:


TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];



retirando o ppReport1 dessa linha o sistema imprime, mas os labels não recebe as disciplinas, ao retirar o ppReport ficou assim:


if ListBox1.SelCount > 4 then
Application.MessageBox('Somente 4 matérias podem ser selecionadas!', 'Selecionar', MB_ICONINFORMATION)
else
begin
ppLabel3.Visible := False;
ppLabel4.Visible := False;
ppLabel5.Visible := False;
ppLabel6.Visible := False;
labels := 1;
for i := 0 to Pred(ListBox1.Count) do
begin
if ListBox1.Selected[i] then
begin
TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];
TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).Visible := True;
Inc(labels);
end;
end;
ppReport1.Print;
Close;
GOSTEI 0
Osmar

Osmar

26/08/2013

Alan,

O sistema não aceita NADA que comece com pp após o ppReport1., coloquei em vários eventos do ppReport1, mas não reconhece, então tirei o ppReport1 e o sistema rodou, mas na hora da impressão dá pau nessa linha:


TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];



retirando o ppReport1 dessa linha o sistema imprime, mas os labels não recebe as disciplinas, ao retirar o ppReport ficou assim:


if ListBox1.SelCount > 4 then
Application.MessageBox('Somente 4 matérias podem ser selecionadas!', 'Selecionar', MB_ICONINFORMATION)
else
begin
ppLabel3.Visible := False;
ppLabel4.Visible := False;
ppLabel5.Visible := False;
ppLabel6.Visible := False;
labels := 1;
for i := 0 to Pred(ListBox1.Count) do
begin
if ListBox1.Selected[i] then
begin
TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];
TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).Visible := True;
Inc(labels);
end;
end;
ppReport1.Print;
Close;
GOSTEI 0
Osmar

Osmar

26/08/2013

Alan, (estou repetindo porque está dando erro na página)

O sistema não aceita NADA que comece com pp após o ppReport1., coloquei em vários eventos do ppReport1, mas não reconhece, então tirei o ppReport1 e o sistema rodou, mas na hora da impressão dá pau nessa linha:

TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];

retirando o ppReport1 dessa linha o sistema imprime, mas os labels não recebe as disciplinas, ao retirar o ppReport ficou assim:

if ListBox1.SelCount > 4 then
Application.MessageBox('Somente 4 matérias podem ser selecionadas!', 'Selecionar', MB_ICONINFORMATION)
else
begin
ppLabel3.Visible := False;
ppLabel4.Visible := False;
ppLabel5.Visible := False;
ppLabel6.Visible := False;
labels := 1;
for i := 0 to Pred(ListBox1.Count) do
begin
if ListBox1.Selected[i] then
begin
TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];
TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).Visible := True;
Inc(labels);
end;
end;
ppReport1.Print;
Close;
GOSTEI 0
Alan Souza

Alan Souza

26/08/2013

eu supus que os seus labels fossem do tipo TppLabel, estariam corretamente nomeados e em outra unit, se não é o caso é só retirar o pReport1 da linha e mudar o typecast:

TppLabel(ppReport1.FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];


Ficando:

TppLabel(FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];
GOSTEI 0
Alan Souza

Alan Souza

26/08/2013

if ListBox1.SelCount > 4 then
Application.MessageBox('Somente 4 matérias podem ser selecionadas!', 'Selecionar', MB_ICONINFORMATION)
else
begin
ppLabel3.Visible := False;
ppLabel4.Visible := False;
ppLabel5.Visible := False;
ppLabel6.Visible := False;
labels := 3;
for i := 0 to Pred(ListBox1.Count) do
begin
if ListBox1.Selected[i] then
begin
TppLabel(FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];
TppLabel(FindComponent('ppLabel' + IntToStr(labels))).Visible := True;
Inc(labels);
end;
end;
ppReport1.Print;
Close;


se o tipo TppLabel não existir, verifique qual é o tipo e substitua o cast
GOSTEI 0
Osmar

Osmar

26/08/2013

Alan,
Agora funcionou beleza, agradeço a todos que ajudaram e principalmente a você, que Deus abençoe a todos, veja como ficou:

procedure TFrmRelCompromisso.BtnTermoCompromissoClick(Sender: TObject);
 var
  i, labels: Integer;
begin

  if ListBox1.SelCount < 1 then
   begin
     Application.MessageBox('Nenhuma disciplina foi selecionada!','Atenção',mb_ok + MB_ICONWARNING);
     exit;
   end;

 if ListBox1.SelCount > 4 then
    Application.MessageBox('Só podem ser selecionadas no máximo 4 disciplinas!','Atenção',mb_ok + MB_ICONWARNING)
 else
  begin
    ppLabel3.Visible := False;
    ppLabel4.Visible := False;
    ppLabel5.Visible := False;
    ppLabel6.Visible := False;
    labels := 3;
    for i := 0 to Pred(ListBox1.Count) do
  begin
   if ListBox1.Selected[i] then
    begin
     TppLabel(FindComponent('ppLabel' + IntToStr(labels))).caption := ListBox1.Items[i];
     TppLabel(FindComponent('ppLabel' + IntToStr(labels))).Visible := True;
     Inc(labels);
    end;
  end;
   ppReport1.Print;
   close;
  end;
end;
end.
GOSTEI 0
José

José

26/08/2013

Obrigado Osmar por compartilhar como ficou com a gente.
e já que a duvida foi sanada, estou dando o tópico por concluído.

att: Ricardo Teixeira.
GOSTEI 0
POSTAR