Solved

How to present user with a listbox

Posted on 2014-09-17
4
303 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 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
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
Comment Utility
My problem is now seen as a design problem.

Perhaps I will ask in that forum.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
simple redial program 3 84
I need a software suggestion 4 62
Best book to learn C++ 4 52
software license audit 6 41
A list of useful business intelligence software.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now