• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

How to present user with a listbox

The following code creates a list box, at an appropriate point in my program.

But I don't know how to present the user with an opportunity to select an item, so that the last part of my code will be useful.

Something seems to be missing at about line 25.

int name_the_suit(int type, int x, int y)
	int i, n;
   TListBox *Listing;
   AnsiString *s[20];

   Form1->Panel2->Left = x;
   Form1->Panel2->Top = y;

  Listing = new TListBox(Form1->Panel2);
  Listing->Parent = Form1->Panel2;
  Listing->Left = 15;
  Listing->Top = 15;
  Listing->Items->Add("No Trumps");
  Listing->Enabled = true;

   Form1->Panel2->Visible = true;
   Form1->Panel2->Enabled = true;

   for(int i = 0; i < Listing->Items->Count; ++i)
      //             s = Listing->Items->Strings[i];



Open in new window

1 Solution
Sinisa VukCommented:
Listing->SetFocus() will not select item in listbox
Must be something like:
Listing->Items->Item[0]->Selected = true;

Open in new window

... force selecting first...
windows is a reactive system. you can't populate a listbox in a function and handle the user's action in the same function.

instead, the system will send a LBN_SELCHANGE notification to the form window when the user selects a line in the listbox and you can provide a handler function in your form class which was called by the framework to handle the notification.

you would do that by adding an entry like the following to the reponse_table of your form:


Open in new window

see http://owlnext.sourceforge.net/help/html/d5/d5d/evchildnotify.html

trinitrotolueneDirector - Software EngineeringCommented:
I would recommend the following structure. Write functions to be as cohesive as possible

Note this is sample code only:

//initializes components
MyUI::MyUI(TComponent* tc)
    Listing = new TListBox(Form1);
    Listing->Parent = Form1;
    Listing->Left = 120;
    Listing->Top = 80;

//adds items
bool MyUI:: AddListItems()
  //logic to add list items to the listbox

//handle a user initiated selection
bool MyUI::handleSelectItem
 //logic to capture text from a selected item
// etc etc

//programmatically select an item without user intervention and capture its text
bool MyUI::ProcessList
  //logic to select an item
 //logic to capture text
// etc etc

Open in new window

VaughanMxAuthor Commented:
My problem is now seen as a design problem.

Perhaps I will ask in that forum.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now