Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • 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("Spades");
  Listing->Items->Add("Hearts");
  Listing->Items->Add("Diamonds");
  Listing->Items->Add("Clubs");
  Listing->Items->Add("No Trumps");
  Listing->Enabled = true;

   Form1->Panel2->Visible = true;
   Form1->Panel2->Enabled = true;
   Listing->SetFocus();
   Form1->Repaint();

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


   return(1);

}

Open in new window

0
VaughanMx
Asked:
VaughanMx
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...
0
 
sarabandeCommented:
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:

EV_NOTIFY_CHILD(ID_LISTBOX, LBN_SELCHANGE, OnSelChanged),

Open in new window


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

Sara
0
 
trinitrotolueneCommented:
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)
:TForm(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

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

Perhaps I will ask in that forum.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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