Solved

How to present user with a listbox

Posted on 2014-09-17
4
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
This article was originally published on Monitis Blog, you can check it here . If you have responsibility for software in production, I bet you’d like to know more about it. I don’t mean that you’d like an extra peek into the bowels of the sourc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

739 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