15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


ListBox Select

Danie Viljoen -- A72820@kepnfs02.eskom.co.za
Wednesday, November 27, 1996

Environment : VC++1.5 , Win3.1

Hi there, I'm am just a hobbiest, but I really need your help?

After struggeling to get data into a Listbox, in a Dialogbox, I 
cannot get the selected item out of it

PLEASE HELP

My code follows

void CMainWnd::CMEdit()
{  
  CString Data;
  
        
  FNameList.RemoveAll();
  //make list to display in listbox
  for(int tel=1; tel <= counter; tel++){ 
    Data=MainData[tel].FSurname;
    Data+=", ";
    Data+=MainData[tel].FName;
    FNameList.AddTail(Data);	
  }

     CApp2Dialog Dlg(this);
     Dlg.DNameList=&FNameList;  
      if (Dlg.DoModal()==IDOK)
        {
           CString selectString;

            // for debugging
            char TestString[10];

            CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
            int selectno=MyListBox->GetCurSel(); 

           // for debugging, to see the index of selected item, I 
           //always get a 0 back and I dont no way???
            sprintf(TestString,"%i",selectno);
            MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);

            // Another problem, even when I change the selectno to a 
            //constant, for example 2 I still gets a empty string back???
            MyListBox->GetText(selectno, selectString);
            MessageBox((const char*) selectString, "info", MB_OK|MB_ICONINFORMATION);		
         }
}

PLEASE HELP ME ?

Danie 



LeRoy Baxter -- lbaxter@transport.com
Saturday, November 30, 1996

By the time you exit the DoModal(), any Windows (as opposed to MFC)
objects have been destroyed -- including your Listbox.  You cannot use
GetDialogItem() from the Parent (as you have done here).  Your
Dialog class will have to extract the relevant data an put it in member
variables in the CApp2Dialog class. 

to rephrase;  the MFC class has persistence within your app - the
actual Windows object (the dialog) is created and destroyed within
the DoModal().

On Tuesday, November 26, 1996 11:37 PM, Danie Viljoen[SMTP:A72820@kepnfs02.eskom.co.za] wrote:
>Environment : VC++1.5 , Win3.1
>
>Hi there, I'm am just a hobbiest, but I really need your help?
>
>After struggeling to get data into a Listbox, in a Dialogbox, I 
>cannot get the selected item out of it
>
>PLEASE HELP
>
>My code follows
>
>void CMainWnd::CMEdit()
>{  
>  CString Data;
>  
>        
>  FNameList.RemoveAll();
>  //make list to display in listbox
>  for(int tel=1; tel <= counter; tel++){ 
>    Data=MainData[tel].FSurname;
>    Data+=", ";
>    Data+=MainData[tel].FName;
>    FNameList.AddTail(Data);	
>  }
>
>     CApp2Dialog Dlg(this);
>     Dlg.DNameList=&FNameList;  
>      if (Dlg.DoModal()==IDOK)
>        {
>           CString selectString;
>
>            // for debugging
>            char TestString[10];
>
>            CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
>            int selectno=MyListBox->GetCurSel(); 
>
>           // for debugging, to see the index of selected item, I 
>           //always get a 0 back and I dont no way???
>            sprintf(TestString,"%i",selectno);
>            MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);
>
>            // Another problem, even when I change the selectno to a 
>            //constant, for example 2 I still gets a empty string back???
>            MyListBox->GetText(selectno, selectString);
>            MessageBox((const char*) selectString, "info", MB_OK|MB_ICONINFORMATION);		
>         }
>}
>
>PLEASE HELP ME ?
>
>Danie 
>





Luke Stephens -- luker@tfs.net
Sunday, December 01, 1996

After the DoModal() ends, the dialog is dead and the controls on the dialog
are also dead.  You must retrieve values from within the dialog class.

Luke Stephens
luker@tfs.net

----------
> From: Danie Viljoen 
> To: mfc-l@netcom.com
> Subject: ListBox Select
> Date: Wednesday, November 27, 1996 1:37 AM
> 
> Environment : VC++1.5 , Win3.1
> 
> Hi there, I'm am just a hobbiest, but I really need your help?
> 
> After struggeling to get data into a Listbox, in a Dialogbox, I 
> cannot get the selected item out of it
> 
> PLEASE HELP
> 
> My code follows
> 
> void CMainWnd::CMEdit()
> {  
>   CString Data;
>   
>         
>   FNameList.RemoveAll();
>   //make list to display in listbox
>   for(int tel=1; tel <= counter; tel++){ 
>     Data=MainData[tel].FSurname;
>     Data+=", ";
>     Data+=MainData[tel].FName;
>     FNameList.AddTail(Data);	
>   }
> 
>      CApp2Dialog Dlg(this);
>      Dlg.DNameList=&FNameList;  
>       if (Dlg.DoModal()==IDOK)
>         {
>            CString selectString;
> 
>             // for debugging
>             char TestString[10];
> 
>             CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
>             int selectno=MyListBox->GetCurSel(); 
> 
>            // for debugging, to see the index of selected item, I 
>            //always get a 0 back and I dont no way???
>             sprintf(TestString,"%i",selectno);
>             MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);
> 
>             // Another problem, even when I change the selectno to a 
>             //constant, for example 2 I still gets a empty string back???
>             MyListBox->GetText(selectno, selectString);
>             MessageBox((const char*) selectString, "info",
MB_OK|MB_ICONINFORMATION);		
>          }
> }
> 
> PLEASE HELP ME ?
> 
> Danie 



Paul Todd. -- paul@pault.demon.co.uk
Sunday, December 01, 1996

Hi Danie,

Have you looked at the DoDataExchange method in a dialog box

When the DoModal loop in a dialog ends, all controls in the dialog are
destroyed.

So accessing the listbox outside of the Modal loop is not possible.

You need to create a public member variable such as m_strSelectedString in
the class and then in the DoDataExchange use DDX_LBString(pDX, IDC_LIST1,
m_strSelectedString);

You can then call TRACE1("The string was %s", dlg->m_strSelectedString)

If you need to send me all the original code and I will comment where you
are going wrong

Paul Todd.

----------
> From: Danie Viljoen 
> To: mfc-l@netcom.com
> Subject: ListBox Select
> Date: 27 November 1996 07:37
> 
> Environment : VC++1.5 , Win3.1
> 
> Hi there, I'm am just a hobbiest, but I really need your help?
> 
> After struggeling to get data into a Listbox, in a Dialogbox, I 
> cannot get the selected item out of it
> 
> PLEASE HELP
> 
> My code follows
> 
> void CMainWnd::CMEdit()
> {  
>   CString Data;
>   
>         
>   FNameList.RemoveAll();
>   //make list to display in listbox
>   for(int tel=1; tel <= counter; tel++){ 
>     Data=MainData[tel].FSurname;
>     Data+=", ";
>     Data+=MainData[tel].FName;
>     FNameList.AddTail(Data);	
>   }
> 
>      CApp2Dialog Dlg(this);
>      Dlg.DNameList=&FNameList;  
>       if (Dlg.DoModal()==IDOK)
>         {
>            CString selectString;
> 
>             // for debugging
>             char TestString[10];
> 
>             CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
>             int selectno=MyListBox->GetCurSel(); 
> 
>            // for debugging, to see the index of selected item, I 
>            //always get a 0 back and I dont no way???
>             sprintf(TestString,"%i",selectno);
>             MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);
> 
>             // Another problem, even when I change the selectno to a 
>             //constant, for example 2 I still gets a empty string back???
>             MyListBox->GetText(selectno, selectString);
>             MessageBox((const char*) selectString, "info",
MB_OK|MB_ICONINFORMATION);		
>          }
> }
> 
> PLEASE HELP ME ?
> 
> Danie 



Jim Lawson Williams -- jimlw@mail.ccur.com.au
Monday, December 02, 1996

At 07:37 AM 27-11-96 GMT+2, you wrote:
>Environment : VC++1.5 , Win3.1
>
>Hi there, I'm am just a hobbiest, but I really need your help?
>
>After struggeling to get data into a Listbox, in a Dialogbox, I 
>cannot get the selected item out of it

G'day Danie!

I guess I could give a fairly grumpy response of "Go away and find a
Beginner's list!"  However, from your question I infer that, while you
have programming experience, you've missed the links between external
events, the operating system, and application programs.

As the code is written, you load the list with data, display it, and
immediately test for a selection.

Think about it.  Even on an 8086 there's no way the User is going to be
able to make a selection unless the program allows an appropriate
interval between displaying the dialog and looking for a selection.
You are getting a "no selection made" result.  One logical explanation
would have to be "there's no 'wait' occurring".  So, somehow or other
you are going to have to allow for the User to read the list-items
(which may involve scrolling up and down), think about the choices,
move the cursor to the chosen item, then "click".  You are going to
have to yield control to the operating system and wait for it to wake
you up when the appropriate event has occurred.

In older-style systems this sequence might have been handled by
displaying a "prompt" message, followed by a simple "read-and-wait"
request, transferring control to the OS to do the physical device
control  --  and whatever other tasks needed scheduling. With IO
complete, execution would eventually resume at the next sequential
instruction in the application program.  It appears as if you are
trying to follow this model, which is entirely inappropriate:  here the
User is not limited to one single action,and particularly not to the
data currently on display if the list-size is greater than the
display-area.

In the system you are using there are a much richer set of features,
though the User-input devices are (in the majority) limited to a
keyboard and a mouse.  Windows and the Application Interface code are
interpreting every key-press, every mouse-event, and relating each to
the mix of tasks.  In effect, a properly-functioning application is
hanging an implicit "wake-me-when-it-happens" read on every active
menu-item, every active control-bar button, and more beside.

Windows and MFC (as your chosen application platform) take the
responsibility for recognizing that a particular user-action has
occurred, and that the action has some meaning in the context of your
program.  "Mouse-move" may be meaningful to some programs, require no
handling by others. Unless an event is defined as "meaningful", it
obviously has to be considered "meaningless" and discarded.  You have
to define the events that interest you.  You have to define how those
interesting events are to be handled.

In Windows the general mechanism for notifying programs of events is
through "messages".  In VC++, the "message map" defines what messages
are to be accepted, and the handler-routine corresponding to each
message.  In your particular case you want to catch the
"user-has-made-a-selection" message, which the Wizard will generate as

	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)

in 4.1 (something similar in 1.5?) and insert a skeleton code-block
into your .CPP file.

I most strongly recommend you buy at least one book on MFC
programming.  You will get a proper overview of how things work.

Good luck!  Jim LW >From the BBC's "Barchester Chronicles":

    "I know that ultimately we are not supposed to understand.  But I
    also know that we must try."

       -- the Reverend Septimus Harding, C++ programmer



Greg D. Tighe -- gdt@eng.aisinc.com
Monday, December 02, 1996

> From:          "Danie Viljoen" 
> Environment : VC++1.5 , Win3.1
> 
> Hi there, I'm am just a hobbiest, but I really need your help?
> 
> After struggeling to get data into a Listbox, in a Dialogbox, I 
> cannot get the selected item out of it
> 
> PLEASE HELP
> 
Danie,
	Use ClassWizard to create a member variable in your dialog class 
which is of type CString and is associated with your dialog's 
listbox.  When the DoModal() call returns the member variable will be 
set to the selected item's text.

	The reason your code did not work was that you were calling 
CMainWnd::GetDlgItem() by default instead of explicitly calling 
Dlg.GetDlgItem().  This probably would not have worked anyway after 
the dialog window (and all control windows along with it) was 
destroyed before returning from DoModal().


	-Greg Tighe
	Applied Intelligent Systems, Inc.
	Ann Arbor, MI
	gdt@aisinc.com



Nitin Bhide -- nitinb@gsslco.co.in
Tuesday, December 03, 1996


Environment : VC++1.5 , Win3.1

Hi there, I'm am just a hobbiest, but I really need your help?

After struggeling to get data into a Listbox, in a Dialogbox, I 
cannot get the selected item out of it

PLEASE HELP

My code follows

void CMainWnd::CMEdit()
{  
  CString Data;
  
        
  FNameList.RemoveAll();
  //make list to display in listbox
  for(int tel=1; tel <= counter; tel++){ 
    Data=MainData[tel].FSurname;
    Data+=", ";
    Data+=MainData[tel].FName;
    FNameList.AddTail(Data);	
  }

     CApp2Dialog Dlg(this);
     Dlg.DNameList=&FNameList;  
      if (Dlg.DoModal()==IDOK)
        {
           CString selectString;

            // for debugging
            char TestString[10];

            CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
            int selectno=MyListBox->GetCurSel(); 

           // for debugging, to see the index of selected item, I 
           //always get a 0 back and I dont no way???
            sprintf(TestString,"%i",selectno);
            MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);

            // Another problem, even when I change the selectno to a 
            //constant, for example 2 I still gets a empty string back???
            MyListBox->GetText(selectno, selectString);
            MessageBox((const char*) selectString, "info", MB_OK|MB_ICONINFORMATION);		
         }
}

PLEASE HELP ME ?

Danie 
----------------------------------------------------------------------------------------------------------------

When DoModal function is finished Dialog window and its child windows are destroyed. 

Extract the selected item index and text of list box in your dialogs OnOK function. Store it in a 
member variable of your dialog class and access those member variables after DoModal
function to get the selected item in the list.

Add a member variable CString m_selectString to your CApp2Dialog class.

CApp2Dialog::OnOK()
{
            // for debugging
            char TestString[10];

            CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
            int selectno=MyListBox->GetCurSel(); 
            sprintf(TestString,"%i",selectno);
            MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);

            MyListBox->GetText(selectno, m_selectString);
            MessageBox((const char*) selectString, "info", 
}
     
     Now in you can access m_selectString variable of the CApp2Dialog class to get the
    selected string after DoModal function.



Wolfgang Loch -- Wolfgang.Loch@RZ.TU-Ilmenau.DE
Tuesday, December 03, 1996

Danie Viljoen wrote:
> 
> Environment : VC++1.5 , Win3.1
> 
> Hi there, I'm am just a hobbiest, but I really need your help?
> 
> After struggeling to get data into a Listbox, in a Dialogbox, I
> cannot get the selected item out of it
> 
> PLEASE HELP
> 
> My code follows
> 
> void CMainWnd::CMEdit()
> {
>   CString Data;
> 
> 
>   FNameList.RemoveAll();
>   //make list to display in listbox
>   for(int tel=1; tel <= counter; tel++){
>     Data=MainData[tel].FSurname;
>     Data+=", ";
>     Data+=MainData[tel].FName;
>     FNameList.AddTail(Data);
>   }
> 
>      CApp2Dialog Dlg(this);
>      Dlg.DNameList=&FNameList;
>       if (Dlg.DoModal()==IDOK)
>         {
>            CString selectString;
> 
>             // for debugging
>             char TestString[10];
> 
>             CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
>             int selectno=MyListBox->GetCurSel();
> 
>            // for debugging, to see the index of selected item, I
>            //always get a 0 back and I dont no way???
>             sprintf(TestString,"%i",selectno);
>             MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);
> 
>             // Another problem, even when I change the selectno to a
>             //constant, for example 2 I still gets a empty string back???
>             MyListBox->GetText(selectno, selectString);
>             MessageBox((const char*) selectString, "info", MB_OK|MB_ICONINFORMATION);
>          }
> }
> 
> PLEASE HELP ME ?
> 
> Danie

You should ask for the selected item before the dialog has been closed,
since after DoModal has returned, the dialog and the listbox are already
destroyed.
Override CApp2Dialog.OnOk() and store the item in a CString member of
the CApp2Dialog.

Wolfgang
-- 
/-------------------------------------------------\
| Wolgang Loch  (Technical University of Ilmenau) |
|   e-mail: Wolfgang.Loch@rz.TU-Ilmenenau.DE      |
|   www   : http://www.rz.tu-ilmenau.de/~wolo     |
\-------------------------------------------------/



Michael Boyken -- mboyken@pens.com
Tuesday, December 03, 1996

Danie,
	There is a sample of a DDX routine on Compuserve (in the MFC section)
that provides a means of transferring a CStringList into and out of a
dialog box. It was intended for multiple selection list boxes but the
concept is the same for single selection.

Mike Boyken
mboyken@pens.com

>----------
>From: 	Luke Stephens[SMTP:luker@tfs.net]
>Sent: 	Saturday, November 30, 1996 10:42 PM
>To: 	mfc-l@netcom.com
>Subject: 	Re: ListBox Select
>
>After the DoModal() ends, the dialog is dead and the controls on the dialog
>are also dead.  You must retrieve values from within the dialog class.
>
>Luke Stephens
>luker@tfs.net
>
>----------
>> From: Danie Viljoen 
>> To: mfc-l@netcom.com
>> Subject: ListBox Select
>> Date: Wednesday, November 27, 1996 1:37 AM
>> 
>> Environment : VC++1.5 , Win3.1
>> 
>> Hi there, I'm am just a hobbiest, but I really need your help?
>> 
>> After struggeling to get data into a Listbox, in a Dialogbox, I 
>> cannot get the selected item out of it
>> 
>> PLEASE HELP
>> 
>> My code follows
>> 
>> void CMainWnd::CMEdit()
>> {  
>>   CString Data;
>>   
>>         
>>   FNameList.RemoveAll();
>>   //make list to display in listbox
>>   for(int tel=1; tel <= counter; tel++){ 
>>     Data=MainData[tel].FSurname;
>>     Data+=", ";
>>     Data+=MainData[tel].FName;
>>     FNameList.AddTail(Data);	
>>   }
>> 
>>      CApp2Dialog Dlg(this);
>>      Dlg.DNameList=&FNameList;  
>>       if (Dlg.DoModal()==IDOK)
>>         {
>>            CString selectString;
>> 
>>             // for debugging
>>             char TestString[10];
>> 
>>             CListBox* MyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
>>             int selectno=MyListBox->GetCurSel(); 
>> 
>>            // for debugging, to see the index of selected item, I 
>>            //always get a 0 back and I dont no way???
>>             sprintf(TestString,"%i",selectno);
>>             MessageBox(TestString,"info",MB_OK|MB_ICONINFORMATION);
>> 
>>             // Another problem, even when I change the selectno to a 
>>             //constant, for example 2 I still gets a empty string back???
>>             MyListBox->GetText(selectno, selectString);
>>             MessageBox((const char*) selectString, "info",
>MB_OK|MB_ICONINFORMATION);		
>>          }
>> }
>> 
>> PLEASE HELP ME ?
>> 
>> Danie 
>




| Вернуться в корень Архива |