Using Created() CListBox
-
I have a rather basic MFC question. It is clear to me how to use the Create() method of a CListBox to create one. It is not clear to me what to do with it after it is created. As far as I know, it does not have a DoModal() method as a CDialog object does or something analogous to the TrackPopupMenu() method of a CMenu object. Does anyone have an example Creating and displaying a CListBox? I want the CListBox to "return" when the user makes a selection, and then to inquire of the CListBox which selection was made. Thanks
-
I have a rather basic MFC question. It is clear to me how to use the Create() method of a CListBox to create one. It is not clear to me what to do with it after it is created. As far as I know, it does not have a DoModal() method as a CDialog object does or something analogous to the TrackPopupMenu() method of a CMenu object. Does anyone have an example Creating and displaying a CListBox? I want the CListBox to "return" when the user makes a selection, and then to inquire of the CListBox which selection was made. Thanks
My first question would be why are you creating it dynamically instead of making it part of the dialog template? gokings wrote: It is not clear to me what to do with it after it is created. Add items to it, or perhaps I don't quite understand your question. gokings wrote: As far as I know, it does not have a DoModal() method as a CDialog object does or something analogous to the TrackPopupMenu() method of a CMenu object. Why are you comparing it to a
CDialog
orCMenu
object? A listbox is a control that is placed on a dialog and enables the user to choose one option from a list of possibilities. gokings wrote: Does anyone have an example Creating and displaying a CListBox? It's such a common control that examples are everywhere. Search here at CP for starters, and then Google for even more. gokings wrote: I want the CListBox to "return" when the user makes a selection, and then to inquire of the CListBox which selection was made. The analogy of "returning" when the user makes a selection makes no sense. When a selection is made from a listbox, the dialog will receive aLBN_SELCHANGE
message. If you are using MFC, the dialog will need aON_LBN_SELCHANGE
handler instead. At that point, you can get the text and/or index of the currently selected item.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
My first question would be why are you creating it dynamically instead of making it part of the dialog template? gokings wrote: It is not clear to me what to do with it after it is created. Add items to it, or perhaps I don't quite understand your question. gokings wrote: As far as I know, it does not have a DoModal() method as a CDialog object does or something analogous to the TrackPopupMenu() method of a CMenu object. Why are you comparing it to a
CDialog
orCMenu
object? A listbox is a control that is placed on a dialog and enables the user to choose one option from a list of possibilities. gokings wrote: Does anyone have an example Creating and displaying a CListBox? It's such a common control that examples are everywhere. Search here at CP for starters, and then Google for even more. gokings wrote: I want the CListBox to "return" when the user makes a selection, and then to inquire of the CListBox which selection was made. The analogy of "returning" when the user makes a selection makes no sense. When a selection is made from a listbox, the dialog will receive aLBN_SELCHANGE
message. If you are using MFC, the dialog will need aON_LBN_SELCHANGE
handler instead. At that point, you can get the text and/or index of the currently selected item.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
The crux of the matter is that I want to display a popup menu, but the CMenu class has limitations making it unsuitable for my needs. Someone suggested that I use a Listbox instead of a Menu. From this I assumed that a Listbox could exist independently of a dialog box. Realizing that this is not the case makes everything fall into place. As for not knowing what to do with a Listbox once it was created, in the context of my question I meant how to cause it to interact with the user. It seems to me that it could be attached at run time to any window, such as a FrameWnd, but perhaps it must be part of a dialog as you indicate. As far as examples being everywhere, if what you say is true, there are no examples of doing what I wanted to do, as it can't be done. I don't need an example of creating a Listbox on a dialog, a monkey could figure that out without an example. Yes, I am aware of handling ON_LBN_SELCHANGE, but I was hoping that there was a "modal" alternative. Regards
-
The crux of the matter is that I want to display a popup menu, but the CMenu class has limitations making it unsuitable for my needs. Someone suggested that I use a Listbox instead of a Menu. From this I assumed that a Listbox could exist independently of a dialog box. Realizing that this is not the case makes everything fall into place. As for not knowing what to do with a Listbox once it was created, in the context of my question I meant how to cause it to interact with the user. It seems to me that it could be attached at run time to any window, such as a FrameWnd, but perhaps it must be part of a dialog as you indicate. As far as examples being everywhere, if what you say is true, there are no examples of doing what I wanted to do, as it can't be done. I don't need an example of creating a Listbox on a dialog, a monkey could figure that out without an example. Yes, I am aware of handling ON_LBN_SELCHANGE, but I was hoping that there was a "modal" alternative. Regards
gokings wrote: ...but the CMenu class has limitations making it unsuitable for my needs. What is it that you are needing that a popup menu cannot provide? I'm just curious if there are some other options that can also be explored. gokings wrote: It seems to me that it could be attached at run time to any window, such as a FrameWnd, but perhaps it must be part of a dialog as you indicate. I don't think a dialog is a requirement, but it is the most common.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
I have a rather basic MFC question. It is clear to me how to use the Create() method of a CListBox to create one. It is not clear to me what to do with it after it is created. As far as I know, it does not have a DoModal() method as a CDialog object does or something analogous to the TrackPopupMenu() method of a CMenu object. Does anyone have an example Creating and displaying a CListBox? I want the CListBox to "return" when the user makes a selection, and then to inquire of the CListBox which selection was made. Thanks
In context with your previous question and my reply ... Probably the easiest way for you would be to derive a new control from CListBox that handles the various selection (or return, escape, ...) messages and have them set a variable to the selected value then destroy or hide the window. Hiding/showing the window may be preferable if the list has many items, but requires a little more code to set up. ...cmk Save the whales - collect the whole set
-
gokings wrote: ...but the CMenu class has limitations making it unsuitable for my needs. What is it that you are needing that a popup menu cannot provide? I'm just curious if there are some other options that can also be explored. gokings wrote: It seems to me that it could be attached at run time to any window, such as a FrameWnd, but perhaps it must be part of a dialog as you indicate. I don't think a dialog is a requirement, but it is the most common.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
I was able to make a CMenu work, thank you. The issue was that the entries to be placed in the menu were dynamically determined at run time. Having a method for each possible message didn't seem clean, and would have limited me to a maximum number of entries in the menu. cmk suggested that I use a method which handled a command range. Being an MFC neophyte, I was unaware of this possibility (and specifically that the command id was a parm to the method). This solved the problem. Thanks.