Tema: listbox
Ver Mensaje Individual
  #2  
Antiguo 06-09-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Vamos a ver si encontramos la solución, para empezar el código que pones daría error al llegar al ultimo elemento, una versión corregida seria:

Código Delphi [-]
{$WRITEABLECONST ON}
procedure TForm1.SpeedButton2Click(Sender: TObject);
const
  i: integer = 0;
begin
   with ListBox4 do
      begin
       if i < Count then
        if Items[i] = 'S' then // Aqui estaba el error al poner i+1
         begin
           Self.Height := 739;
           Self.Width := 1024;
           Self.Left := 0;
           Self.Top := 0;
         end;
        if Items[i] = 'N' then
         begin
           Self.Height := 300;
           Self.Width := 550;
           Self.Left := 400;
           Self.Top := 400;
         end;
       i:= (i + 1) mod Count;
      end;
end;
{$WRITEABLECONST OFF}

El codigo anterior recorre el listbox pero sin mover el elemento seleccionado, si lo que queremos es mover el elemento seleccionado el codigo todavia es mas simple:

Código Delphi [-]
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
   with ListBox4 do
      begin
       ItemIndex:= (ItemIndex + 1) mod Count;
       if ItemIndex < Count then
        if Items[ItemIndex] = 'S' then
         begin
           Self.Height := 739;
           Self.Width := 1024;
           Self.Left := 0;
           Self.Top := 0;
         end;
        if Items[ItemIndex] = 'N' then
         begin
           Self.Height := 300;
           Self.Width := 550;
           Self.Left := 400;
           Self.Top := 400;
         end;
      end;
end;

Con el codigo anterior solo tenemos que seleccionar el elemento de partida y con cada clic boton se seleccionara el siguiente. Ahora bien, si quieres indicar en que elemento debe comenzar, eso lo puedes hacer manualmente, o en el evento OnCreate del formulario, colocar la propiedad ItemIdex del Listbox en el elemento adecuado. Pero si lo que quieres es que nuestro programa recuerde en que posicion se quedo en la ejecucion anterior no te queda mas remedio que guardarlo, bien sea en un archivo o en el registro:

Por ejemplo en un archivo:
Código Delphi [-]
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  with TStringList.Create do
  try
    Values['POS']:= IntToStr(ListBox4.ItemIndex);
    SaveToFile(ChangeFileExt(ParamStr(0),'.ini'));
  finally
    Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if FileExists(ChangeFileExt(ParamStr(0),'.ini')) then
    with TStringList.Create do
    try
      LoadFromFile(ChangeFileExt(ParamStr(0),'.ini'));
      ListBox4.ItemIndex:= StrToIntDef(Values['POS'],-1);
    finally
      Free;
    end;
end;
Responder Con Cita