Jump to content
shadowrecon

Anyone good with Tlistbox's?

Recommended Posts

so ive been working the past todays on making a forum that has 2 list boxs, 3 text edit boxes a few buttons. When i add a player i start to store the player in an array, ive managed to do this, but i also i want the user to be able to select one and delete it which ive also managed to do, the problem comes in my arrays, of the user data, so like for instance when item at pos 3 was created i then stored the user info in array[3] but when its deleted this array is empty. So then i decided okay well ill just sort the arrays and removed empty user names so i wrote this function:

[scar]

// my method to correct data... best way i could thank of..

Procedure FastReplaceInfo;

Var

I,newL:Integer;

TempInfo: PInfoArray;

begin

SetLength(TempInfo,20);

SetLength(UserInfo,20);

For I := 0 to High(UserInfo) do

if UserInfo.Name <> '' then

begin

TempInfo[newL].Name := UserInfo.Name;

TempInfo[newL].Pass := UserInfo.Pass;

TempInfo[newL].Bank := UserInfo.Bank;

Inc(newL);

end;

newL := newL;

SetLength(UserInfo,newL);

For I := 0 to High(UserInfo) do

begin

UserInfo.Name := TempInfo.Name;

UserInfo.Pass := TempInfo.Pass;

UserInfo.Bank := TempInfo.Bank;

AddToList(ListBox1,UserInfo.Name);

end;

end;

[/scar]

 

This manages to do what i intended it to do but im getting array errors still can some one take a peak at my code and tell me what is wrong?

 

Here it is:

[scar]

 

type

PInfo = record

Name,Pass,Bank: String;

Active,World: Boolean;

Monster,Food,Potion: Integer;

end;

PInfoArray = Array of PInfo;

// Globals for user setting storeing.

Var

UserInfo: PInfoArray;

 

var

Form1_1: TForm;

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Label4: TLabel;

Label5: TLabel;

ListBox1: TListBox;

ListBox2: TListBox;

edtUN: TEdit;

edtPW: TEdit;

edtBP: TEdit;

btnAdd: TButton;

btnDelete: TButton;

btnAddPlayer: TButton;

btnDeletePlayer: TButton;

btnLoadSaved: TButton;

 

Procedure ShowErrorMessage;

begin

ShowMessage('Error, Please Check Input.');

end;

 

// Universal Add To List Function.

Function AddToList(Var LBox: TListBox; Const Addition: String): Integer;

Var

I: Integer;

begin

I := LBox.Items.IndexOf(Addition);

If I <> -1 then

begin

Result := I;

Exit;

end;

LBox.Items.Add(Addition);

Result := (LBox.Items.Count+1);

end;

 

// my method to correct data... best way i could thank of..

Procedure FastReplaceInfo;

Var

I,newL:Integer;

TempInfo: PInfoArray;

begin

SetLength(TempInfo,20);

SetLength(UserInfo,20);

For I := 0 to High(UserInfo) do

if UserInfo.Name <> '' then

begin

TempInfo[newL].Name := UserInfo.Name;

TempInfo[newL].Pass := UserInfo.Pass;

TempInfo[newL].Bank := UserInfo.Bank;

Inc(newL);

end;

newL := newL;

SetLength(UserInfo,newL);

For I := 0 to High(UserInfo) do

begin

UserInfo.Name := TempInfo.Name;

UserInfo.Pass := TempInfo.Pass;

UserInfo.Bank := TempInfo.Bank;

AddToList(ListBox1,UserInfo.Name);

end;

end;

 

// Universal Delete From List Function.

Procedure DeleteFromList(Var LBox: TListBox; Const Index:Integer);

begin

LBox.Items.Delete(Index);

UserInfo[index].Name := '';

UserInfo[index].Pass := '';

UserInfo[index].Bank := '';

edtUN.Text := UserInfo[index].Name;

edtPW.Text := UserInfo[index].Pass;

edtBP.Text := UserInfo[index].Bank;

FastReplaceInfo;

end;

 

// Moves from one list to the next

Procedure MoveToList(Var LBox1,LBox2: TListBox; Const Index:Integer);

Var

UN: String;

begin

UN := LBox1.Items.Strings[index];

DeleteFromList(LBox1,Index);

AddToList(LBox2, UN);

end;

 

//Stores Player Information While Forum Is Active Temp

Procedure StorePlayer(Var LBox: TListBox; Const Addition: String);

Var

curP,curL: Integer;

begin

SetLength(UserInfo,20);

curP := AddToList(ListBox1,edtUN.Text);

UserInfo[curP].Name := edtUN.Text;

UserInfo[curP].Pass := edtPW.Text;

UserInfo[curP].Bank := edtBP.Text;

FastReplaceInfo;

end;

 

//Saves Settings To File

Procedure SaveSettings;

Var

curL,I: Integer;

begin

curL := Length(UserInfo);

SaveSetting('UB_Fighter','UserLenght',IntToStr(curL));

For I := 0 to High(UserInfo) do

begin

SaveSetting('UB_Fighter','UserName'+IntToStr(I),UserInfo.Name);

SaveSetting('UB_Fighter','UserPass'+IntToStr(I),UserInfo.Pass);

SaveSetting('UB_Fighter','UserBank'+IntToStr(I),UserInfo.Bank);

end;

end;

 

// Loads From File

Procedure LoadSettings;

Var

curL,I: Integer;

begin

curL := StrToInt(LoadSetting('UB_Fighter','UserLenght'));

If curL = 0 then curL := 1;

ListBox1.items.Clear;

Setlength(UserInfo,curL);

For I := 0 to High(UserInfo) do

begin

UserInfo.Name := LoadSetting('UB_Fighter','UserName'+IntToStr(I));

UserInfo.Pass := LoadSetting('UB_Fighter','UserPass'+IntToStr(I));

UserInfo.Bank := LoadSetting('UB_Fighter','UserBank'+IntToStr(I));

AddToList(ListBox1,UserInfo.Name);

end;

end;

 

// On Click Add Player Adds New Player To Player User Lib.

Procedure AddPlayer(sender: TObject);

begin

If (edtUN.Text <> '') and (edtPW.Text <> '') and (edtBP.Text <> '') then

StorePlayer(ListBox1,edtUN.Text)

else

ShowErrorMessage;

end;

 

// Deletes Player From Player User Lib. when dubble clicked.

Procedure DeletePlayer(sender: TObject);

Var

curP,curL: Integer;

begin

curP := ListBox1.ItemIndex;

DeleteFromList(ListBox1,curP);

FastReplaceInfo;

SaveSettings;

end;

 

// Moves Play From Lib of Players To Active

Procedure MovePlayerActive(sender: TObject);

Var

curL: Integer;

begin

curL := (ListBox2.Items.Count);

MoveToList(ListBox1,Listbox2,curL);

end;

 

Procedure DisplayDetails(sender: TObject);

Var

curP: Integer;

begin

curP := ListBox1.ItemIndex;

if (curP = -1) or (curP > High(UserInfo)) then exit;

edtUN.Text := UserInfo[curP].Name;

edtPW.Text := UserInfo[curP].Pass;

edtBP.Text := UserInfo[curP].Bank;

end;

 

procedure Form1_1_Init;

begin

Form1_1 := CreateForm;

Label1 := TLabel.Create(Form1_1);

Label2 := TLabel.Create(Form1_1);

Label3 := TLabel.Create(Form1_1);

Label4 := TLabel.Create(Form1_1);

Label5 := TLabel.Create(Form1_1);

ListBox1 := TListBox.Create(Form1_1);

ListBox2 := TListBox.Create(Form1_1);

edtUN := TEdit.Create(Form1_1);

edtPW := TEdit.Create(Form1_1);

edtBP := TEdit.Create(Form1_1);

btnAdd := TButton.Create(Form1_1);

btnDelete := TButton.Create(Form1_1);

btnAddPlayer := TButton.Create(Form1_1);

btnDeletePlayer := TButton.Create(Form1_1);

btnLoadSaved := TButton.Create(Form1_1);

with Form1_1 do

begin

Left := 413;

Top := 240;

Caption := 'Form1';

ClientHeight := 409;

ClientWidth := 511;

Color := clBtnFace;

Font.Charset := DEFAULT_CHARSET;

Font.Color := clWindowText;

Font.Height := -11;

Font.Name := 'Tahoma';

Font.Style := [];

OldCreateOrder := False;

PixelsPerInch := 96;

end;

with Label1 do

begin

Parent := Form1_1;

Left := 110;

Top := 51;

Width := 71;

Height := 13;

Caption := 'User Database';

end;

with Label2 do

begin

Parent := Form1_1;

Left := 266;

Top := 51;

Width := 60;

Height := 13;

Caption := 'Active Users';

end;

with Label3 do

begin

Parent := Form1_1;

Left := 110;

Top := 267;

Width := 48;

Height := 13;

Caption := 'Username';

end;

with Label4 do

begin

Parent := Form1_1;

Left := 110;

Top := 294;

Width := 46;

Height := 13;

Caption := 'Password';

end;

with Label5 do

begin

Parent := Form1_1;

Left := 110;

Top := 319;

Width := 40;

Height := 13;

Caption := 'Bank Pin';

end;

with ListBox1 do

begin

Parent := Form1_1;

OnClick := @DisplayDetails;

ONDBLCLICK := @DeletePlayer;

Left := 110;

Top := 68;

Width := 121;

Height := 177;

ItemHeight := 13;

TabOrder := 0;

end;

with ListBox2 do

begin

Parent := Form1_1;

Left := 266;

Top := 68;

Width := 121;

Height := 177;

ItemHeight := 13;

TabOrder := 1;

end;

with edtUN do

begin

Parent := Form1_1;

Left := 167;

Top := 263;

Width := 121;

Height := 21;

TabOrder := 2;

end;

with edtPW do

begin

Parent := Form1_1;

Left := 167;

Top := 290;

Width := 121;

Height := 21;

TabOrder := 3;

end;

with edtBP do

begin

Parent := Form1_1;

Left := 167;

Top := 315;

Width := 121;

Height := 21;

TabOrder := 4;

end;

with btnAdd do

begin

Parent := Form1_1;

OnClick := @MovePlayerActive;

Left := 233;

Top := 115;

Width := 27;

Height := 25;

Caption := '>>';

TabOrder := 5;

end;

with btnAddPlayer do

begin

Parent := Form1_1;

OnClick := @AddPlayer;

Left := 312;

Top := 255;

Width := 75;

Height := 25;

Caption := 'Add Player';

TabOrder := 7;

end;

LoadSettings;

end;

 

procedure Form1_1_SafeInit;

var

v: TVariantArray;

begin

SetLength(v, 0);

ThreadSafeCall('Form1_1_Init', v);

end;

 

function Form1_1_ShowModal: Boolean;

begin

Result := Form1_1.ShowModal = mrOk;

end;

 

function Form1_1_SafeShowModal: Boolean;

var

v: TVariantArray;

begin

SetLength(v, 0);

Result := ThreadSafeCall('Form1_1_ShowModal', v);

end;

Var

I:Integer;

begin

Form1_1_SafeInit;

if Form1_1_SafeShowModal then

WriteLn('Form returned modalresult ok');

SaveSettings;

FreeForm(Form1_1);

For I := 0 to High(UserInfo) do

Writeln(UserInfo.name);

end.

[/scar]

 

---------- Post added at 03:29 AM ---------- Previous post was at 03:13 AM ----------

 

After messing around with this, i see a mjaor flaw in my correcting data.. its not changing the postions it really just doing nothing.. ugh.. how do i go about sorting and re arranging the array?

 

---------- Post added at 03:43 AM ---------- Previous post was at 03:29 AM ----------

 

Okay i think ive figured out a method for sorting the strings how does this look:

[scar]

program New;

 

Var

FakeData: TStringArray;

TempData: TStringArray;

 

Procedure SetFakeArrays;

Var

I: Integer;

begin

SetLength(FakeData,21);

For I := 0 to 20 do

begin

if (I = 5) or (I=8) or (I=6) then

FakeData := ''

else

FakeData := 'Pos: '+IntToStr(I);

end;

end;

 

 

 

Procedure FastReplaceInfo;

Var

I,II,newL:Integer;

begin

For I := 0 to High(FakeData) do

if FakeData <> '' then

begin

SetLength(TempData,Length(TempData)+1);

TempData[iI] := FakeData;

Inc(II);

end;

newL := (II);

SetLength(FakeData,newL);

For I := 0 to High(FakeData) do

FakeData := TempData;

end;

 

 

procedure MainLoop;

Begin

SetFakeArrays;

Writeln(Length(FakeData));

FastReplaceInfo;

Writeln(Length(FakeData));

end;

Begin

MainLoop;

end.

[/scar]

Edited by shadowrecon
Link to comment
Share on other sites

On first sight I'd say "AddToList" is wrong, should be:

 

[scar]Function AddToList(Var LBox: TListBox; Const Addition: String): Integer;

begin

Result := LBox.Items.IndexOf(Addition);

If Result <> -1 then

Exit;

Result := LBox.Items.Add(Addition);

end;[/scar]

 

Before you were not returning the index, you were returning the count + 1, which is 2 higher than the maximum index. I've also shortened it a bit.

 

EDIT: You really should not name that MainLoop, sends shivers down my spine... However, I have no idea what you're doing there with the trying to sort strings, but I suggest you take a look at this: http://www.java2s.com/Code/C/Data-Structure-Algorithm/AQuicksortforstrings.htm

Edited by Freddy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...