Board index » cppbuilder » can't add or delete listbox item on another form

can't add or delete listbox item on another form

i have a listbox on a form that holds a list of servers. i have two other
forms, one to add a new server and the other to edit server properties.

in the add server forms' ok button click i have..

Settings_Form->Servers_ListBox->AddItem(Address_Edit->Text, NULL);

and in the edit server forms' ok button click i have....

Settings_Form->Servers_ListBox->Items->Delete(Settings_Form->Servers_ListBox
->ItemIndex);
Settings_Form->Servers_ListBox->AddItem(Address_Edit->Text, NULL);

the problem is that in both cases the items don't get added to the listbox
and for editing the item doesn't get deleted from the listbox. this is
similar to another problem i posted about where i couldn't get the listbox
to clear.

from settings.h.....

class TSettings_Form : public TForm
{
__published: // IDE-managed Components
        TListBox *Servers_ListBox;

TIA

 

Re:can't add or delete listbox item on another form


[C++ Error] Unit2.cpp(21): E2316 'AddItem' is not a member of 'TListBox'

Did you not receive an error when compiling?

use: Settings_Form->Servers_ListBox->Items->Add( Address_Edit->Text );

Quote
"mostuff" <do...@emailback.com> wrote in message

news:3ec1842f@newsgroups.borland.com...
Quote
> i have a listbox on a form that holds a list of servers. i have two other
> forms, one to add a new server and the other to edit server properties.

> in the add server forms' ok button click i have..

> Settings_Form->Servers_ListBox->AddItem(Address_Edit->Text, NULL);

> and in the edit server forms' ok button click i have....

> Settings_Form->Servers_ListBox->Items->Delete(Settings_Form->Servers_ListBox
> ->ItemIndex);
> Settings_Form->Servers_ListBox->AddItem(Address_Edit->Text, NULL);

> the problem is that in both cases the items don't get added to the listbox
> and for editing the item doesn't get deleted from the listbox. this is
> similar to another problem i posted about where i couldn't get the listbox
> to clear.

> from settings.h.....

> class TSettings_Form : public TForm
> {
> __published: // IDE-managed Components
>         TListBox *Servers_ListBox;

> TIA

Re:can't add or delete listbox item on another form


Quote
"mostuff" <do...@emailback.com> wrote in message

news:3ec1842f@newsgroups.borland.com...

Quote
> the problem is that in both cases the items don't get added
> to the listbox and for editing the item doesn't get deleted
> from the listbox. this is similar to another problem i posted
> about where i couldn't get the listbox to clear.

With all the problems you're having with this lately, it makes me think you
are misusing the ListBox incorrectly in the first place.  Everything you
have described so far should be working fine under normal conditions.  What
are your EXACT settings for the ListBox itself?  Are you able to create a
new project with absolute minimal code that demonstrates the same problems?
If so, then please post that project to the .attachments group for us to
have something to actually look at.

Gambit

Re:can't add or delete listbox item on another form


Quote
"Todd Brylski" <tbryl...@new.rr.com> wrote in message

news:3ec19833$1@newsgroups.borland.com...

Quote
> Did you not receive an error when compiling?

AddItem() exists in BCB6.

However, I do agree that Add() should typically be called directly instead
of using AddItem():

    Settings_Form->Servers_ListBox->Items->Add(Address_Edit->Text);

Gambit

Re:can't add or delete listbox item on another form


"Remy Lebeau (TeamB)" <gambi...@yahoo.com> wrote in message
news:3ec199a8$1@newsgroups.borland.com...

Quote

> With all the problems you're having with this lately, it makes me think
you
> are misusing the ListBox incorrectly in the first place.  Everything you
> have described so far should be working fine under normal conditions.
What
> are your EXACT settings for the ListBox itself?  Are you able to create a
> new project with absolute minimal code that demonstrates the same
problems?
> If so, then please post that project to the .attachments group for us to
> have something to actually look at.

> Gambit

i don't doubt i'm doing something wrong. i tried deleting the listbox and
placing a new one on the settings form but it made no difference, which
means i am doing something wrong.

here's all the code i think is relevant

void __fastcall TSettings_Form::Add_ButtonClick(TObject *Sender)
{
 TAddServer_Form *AddServer = new TAddServer_Form(Application);
 AddServer->ShowModal();
 delete AddServer;

Quote
}

void __fastcall TSettings_Form::Edit_ButtonClick(TObject *Sender)
{
 if (Servers_ListBox->ItemIndex > -1) {
    TEditServer_Form *EditServer = new TEditServer_Form(Application);

    TLocateOptions SearchOptions;
    if (Data->Servers_DataSource->DataSet->Locate("ServerName",
Servers_ListBox->Items->Strings[Servers_ListBox->ItemIndex], SearchOptions))
{

       for (int i=0;i<EditServer->Type_ComboBox->Items->Count;i++) {
           if (EditServer->Type_ComboBox->Items->Strings[i] ==
Data->Servers_DataSource->DataSet->FieldByName("Type")->AsString) {
              EditServer->Type_ComboBox->ItemIndex = i;
           }
       }

       EditServer->Address_Edit->Text =
Data->Servers_DataSource->DataSet->FieldByName("ServerAddress")->Value;
       EditServer->Username_Edit->Text =
Data->Servers_DataSource->DataSet->FieldByName("Username")->Value;
       EditServer->Password_Edit->Text =
Data->Servers_DataSource->DataSet->FieldByName("Password")->Value;

       EditServer->ShowModal();

       delete EditServer;
    }
 }

Quote
}

void __fastcall TAddServer_Form::OK_ButtonClick(TObject *Sender)
{
 Data->Servers_DataSource->DataSet->Insert();

 Data->Servers_DataSource->DataSet->FieldByName("ServerName")->Value =
Address_Edit->Text;
 Data->Servers_DataSource->DataSet->FieldByName("ServerAddress")->Value =
Address_Edit->Text;
 Data->Servers_DataSource->DataSet->FieldByName("Type")->Value =
Type_ComboBox->Text;
 Data->Servers_DataSource->DataSet->FieldByName("Username")->Value =
Username_Edit->Text;
 Data->Servers_DataSource->DataSet->FieldByName("Password")->Value =
Password_Edit->Text;

 Data->Servers_DataSource->DataSet->Post();

 Settings_Form->Servers_ListBox->AddItem(Address_Edit->Text, NULL);

 Close();

Quote
}

void __fastcall TEditServer_Form::OK_ButtonClick(TObject *Sender)
{
 Data->Servers_DataSource->DataSet->Edit();

 Data->Servers_DataSource->DataSet->FieldByName("ServerName")->Value =
Address_Edit->Text;
 Data->Servers_DataSource->DataSet->FieldByName("ServerAddress")->Value =
Address_Edit->Text;
 Data->Servers_DataSource->DataSet->FieldByName("Type")->Value =
Type_ComboBox->Text;
 Data->Servers_DataSource->DataSet->FieldByName("Username")->Value =
Username_Edit->Text;
 Data->Servers_DataSource->DataSet->FieldByName("Password")->Value =
Password_Edit->Text;

 Data->Servers_DataSource->DataSet->Post();

Settings_Form->Servers_ListBox->Items->Delete(Settings_Form->Servers_ListBox
->ItemIndex);
 Settings_Form->Servers_ListBox->AddItem(Address_Edit->Text, NULL);

 Close();

Quote
}

 i also tried a new application doing the same basic thing and that worked
ok and it i put a tedit in the settings form and just have the add button
add whatever's in the edit into listbox, that works ok too. and there's no
Add() for a tlistbox, only AddItem().

mostuff

Re:can't add or delete listbox item on another form


Quote
"mostuff" <do...@emailback.com> wrote in message

news:3ec1c9bb$1@newsgroups.borland.com...

Quote
> there's no Add() for a tlistbox, only AddItem().

Look closer at the earlier samples posted.  Add() is a method of the Items
subproperty, not the ListBox itself:

    ListBox->Items->Add(TextToAdd);

Gambit

Re:can't add or delete listbox item on another form


"Remy Lebeau (TeamB)" <gambi...@yahoo.com> wrote in message
news:3ec1dd5e$1@newsgroups.borland.com...

Quote

> Look closer at the earlier samples posted.  Add() is a method of the Items
> subproperty, not the ListBox itself:

>     ListBox->Items->Add(TextToAdd);

doesn't work either

Re:can't add or delete listbox item on another form


Quote
"mostuff" <do...@emailback.com> wrote in message

news:3ec1e96d$1@newsgroups.borland.com...

Quote
> doesn't work either

Yes, it does.  If it's not working for you, then again I emphasize the
possibility that your ListBox usage is messed up in general, and as such you
really need to actually show us how you're really using it.  Please create a
small compilable sample project that demonstrates the smae problem and then
post it to the .attachments group for review.

Gambit

Re:can't add or delete listbox item on another form


Hello,
first of all, I'm not a great expert in BCB and I only have the german
version, so sometimes I really don't know the english names of the
components, but regardless of that let me try to help you.
Quote

>  i also tried a new application doing the same basic thing and that worked
> ok and it i put a tedit in the settings form and just have the add button
> add whatever's in the edit into listbox, that works ok too. and there's no
> Add() for a tlistbox, only AddItem().

So it seems, that you know what you are doing and you do it right, cause
your test programm works. Did you ever set a breakpoint on the line where
you try to add an item in the list box? If no, just do it and test the
content of Address_Edit->Text, because I think that the returned value is
NULL or an empty AnsiString.
The next thing I want to ask you is, does the code for adding a new entry in
the listbox stands in the same form as the edit field. If not I think you
have to add the form name (e.g. frmTest->Address_Edit->Text).
Maybe that helps you a little bit.

Greetings Oliver Tonn

Other Threads