sábado, 2 de fevereiro de 2019

C++ Builder - preenchendo listbox

Criamos dois ListBox, sendo que o primeiro
já vem carregado pela matriz 
bidimensional de string char nomes [ ][ ];
que poderia ser de uma única dimensão,
mas preferi assim.
A função Sorteia ( ), com seus dois parâmetros,
inicializa um contador randômico,
mas elimina números repetidos,
isto é fundamental, para que os nomes não
se repitam dentro do listbox2, 
na hora da seleção aleatória,
que acontece pressionando o botão "OK",
da caixa de mensagem utilizada pelo usuário.




 



//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
#include <time.h>
#define tam 10

void Sorteia ( int *vet ) {
 int i, r, temp;
   srand ( time ( NULL ) );
    for ( i = 0; i < tam; i++ ) {
r = rand ( ) % tam;
temp = vet [ i ];
vet [ i ] = vet [ r ];
vet [ r ] = temp;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender ) {

 char nomes [ 10 ] [ 16 ] =
     { "Éder Costa",
   "Humberto Gomes",
   "Dijalma Lacerda",
   "Caroline Silva",
   "Igor Gonçalves",
   "Bruna Carla ",
   "Fábio Quadros",
   "Geany Barros",
   "Ana Célia",
   "Jaqueline Vega" };
 int vet [ tam ];
 int b = 9;
 int i, x = 0;
for ( i = 0; i < tam; i++ ) {
vet [ i ] = i;
}
 Sorteia ( vet );
 for ( i = 0; i < tam; i++ ) {
  ListBox1 -> Items -> Add ( nomes [ i ] );
 }
 for ( i = 0; i < tam; i++ ) {
  do {
  b = vet [ i ];
  i++;
  x++;
   MessageDlgPos ( L"Pressione no botão OK Para selecionar\n"
"Um nome aleatoriamente\n"
     "Para ser inserido no ListBox2",
     mtInformation, TMsgDlgButtons() << mbOK, 0, 290, 430 );
ListBox2 -> Items -> Add ( nomes [ b ] );
} while ( x < tam );
  i = i - 1;
  }
}
//---------------------------------------------------------------------------


#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TListBox *ListBox1;
TListBox *ListBox2;
void __fastcall OnPaint(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------

#endif


object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'C++ BUILDER - PREENCHENDO LISTBOX'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object ListBox1: TListBox
    Left = 24
    Top = 64
    Width = 217
    Height = 193
    Color = clGradientActiveCaption
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsItalic]
    ItemHeight = 19
    ParentFont = False
    TabOrder = 0
  end
  object ListBox2: TListBox
    Left = 352
    Top = 64
    Width = 217
    Height = 193
    Color = clSilver
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = [fsItalic]
    ItemHeight = 19
    ParentFont = False
    TabOrder = 1
  end

end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.