Solved

How to present user with a listbox

Posted on 2014-09-17
4
317 Views
Last Modified: 2014-09-19
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
Comment
Question by:VaughanMx
4 Comments
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40327997
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
 
LVL 34

Expert Comment

by:sarabande
ID: 40328216
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
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40332096
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
 

Author Closing Comment

by:VaughanMx
ID: 40332103
My problem is now seen as a design problem.

Perhaps I will ask in that forum.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question