How can I send a message to List Box instead of using a global boolean flag?

I need help in understanding messaging better in C++/MFC version 6.  I understand I can use SendMessage( ...) and TranslateMessage(....) commands, but don't fully comprehend how to utilize them.  I am not sure about WParam and LParam's parameters.  I've read the MSCN help files on it, but don't get enough examples or a simpler explanation than I can understand.  Really need a short primer on sending and reading messages. Is there a primer on the web someplace?  What I want to do is send a message to a List Box.  I have 3 List Boxes and for one of them I want to send a message(not a flag) not to use negative numbers.

Thanks,
JM
JPM2002Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You need to specify a message handler for your list control that handles your message, e.g.

class CMyListCtrl : public CListCtrl
{
//...

#define WM_LB_NONEGATIVENUMBERS (WM_USER + 1)
BEGIN_MESSAGE_MAP( CMyListCtrl , CListCtrl)
    //{{AFX_MSG_MAP( CMyWnd
    ON_MESSAGE( WM_LB_NONEGATIVENUMBERS, OnNoNegativeNumbers )
    // ... Possibly more entries to handle additional messages
    //}}AFX_MSG_MAP
END_MESSAGE_MAP( )

LRESULT OnNoNegativeNumbers(WPARAM,LPARAM);

private:

BOOL m_bNoNegativceNumbers;

};

LRESULT CMyListCtrl::OnNoNegativeNumbers(WPARAM,LPARAM)
{

    m_bNoNegativeNumbers = TRUE;
}

For more on this topic, see http://msdn2.microsoft.com/en-us/library/Aa270834.aspx ("Message Handling and Mapping Topics")
0
jkrCommented:
Ooops, forgot - that should be

LRESULT CMyListCtrl::OnNoNegativeNumbers(WPARAM,LPARAM)
{
    m_bNoNegativeNumbers = TRUE;

    return 0;
}
0
JPM2002Author Commented:
jkr,

Thanks but I already am using a modified ListBox for the 3 Listboxes I have inside a dialog box.  This list box modification can edit the text directly on the list box itself.  However I just want to send a message to
the one list box not to use negative numbers when I click on that single list box.  However I need to be able to separate that handler from the other 2 list boxes. I am wondering if I could use SendMessage(,,,) command and read it with something like PeekMessage(...).  As before I don't know and understand how to use the WParam and LParam values as I fully don't understand what they are about.

thanks,
JM
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jkrCommented:
>>I am wondering if I could use SendMessage(,,,)

Yes, that you then can do - after implementing the handler as above, i.e.

pListCtrl->SendMessage(WM_LB_NONEGATIVENUMBERS,0,0);

>>and read it with something like PeekMessage(...).

That does not make sense in a framework like MFC. MFC has its own internal message loop that dipatches the messages to their respective handlers. By defining such a handler as above, you can handle such a message.
0
JPM2002Author Commented:
jkr,

So what I am understanding is if I just send the message above after clicking on the list box that I don't want negatives I will not have to read it with the PeekMessage???
0
jkrCommented:
No, your handler function will be called instead automatically.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
jkr doesn't seem to have commented about WPARAM and LPARAM.
Simply said they are two variables that you can use to pass information.  The window that handles the message can then use those variables to determine further action.  The variables could be simple variables eg long or adresses of structures for example.

eg. You might want to pass the message - ignore negative numbers - and in WPARAM a value eg -1000 which tells the handler that it is to ignore all negative numbers less than -1000
0
itsmeandnobodyelseCommented:
>>>> I want to send a message(not a flag) not to use negative numbers.

You can call the handler directly in your dialog, you don't need to send messages:

void CMyDialog::OnInitDialog()
{
        ....
        // tell the 3rd listbox not to using negative numbers
        GetDlgItem(IDC_LISTBOX3)->OnNoNegativeNumbers(TRUE, FALSE);
        ....
        return TRUE;
}


Note, you can use the messaging system of Windows and MFC to pass that information by message. But actually the communication between a dialog and its controls more easily is made by directly calling the controls. Windows messaging is just an overkill for that task. Things were different if the listbox implementation was moved to an active-x dll (.ocx). Then messaging can be a good means to add new functionality without changing the interface.

Regards, Alex
0
itsmeandnobodyelseCommented:
>>>> I understand I can use SendMessage( ...) and TranslateMessage(....)
Hmmm. SendMessage is indeed a call you can use, but TranslateMessage was called by your dialog's message loop (see remarks from jkr regarding the MFC framework). In rare cases, e. g. if you have to process a lengthy evaluation in a handler function, you would setup a own message loop to not let the screen freeze:

void CMyDialog::OnLengthyEvaluation()
{
     for (int i = 0; i < 1000; ++i)
     {
           doPartofEvaluation(i, anyinfo);
           while (PeekMessage(...))
           {
                   GetMessage(...);
                   TranslateMessage(...);
                   DispatchMessage(...);
           }
     }
}  

That loop would process all messages made in the meantime like mouse messages ... You would need to use a filter in PeekMessage in order to not being called again and have a dead-lock cause the DispatchMessage was responsible for the call of CMyDialog::OnLengthyEvaluation() either.


>>>> if I just send the message above after clicking on the list box that I don't want negatives

I don't understand what you mean by "not to use negative numbers." How can a listbox 'decide' what to use or what to not use? Did you write a handler of your listbox control which allows 'editing' of the listbox entries?
0
JPM2002Author Commented:
jkr  --  I have to look into what you sent me and try it out.
Andy -- thanks for the commenting on the Wparam and Lparam.  
Alex -- good shorter technique too.

too all:
Is Sending messages limited to just controlling dialog boxes or other controls.  Is there a way I can send a message to in effect use one of the functions in my class using some type of messaging without getting
including .h file.
0
itsmeandnobodyelseCommented:
>>>> Is Sending messages limited to just controlling dialog boxes or other controls.
Sending and receiving messages is limited to 'windows', i. e. you always need a valid hwnd (window handle) to sent to. But there also can be 'pseudo handles' used, e. g. in a window-less thread which want to communicate by means of window messages with its main thread.  

>>>> Is there a way I can send a message to in effect use one of the functions in my class >>>> using some type of messaging without getting including .h file.

Windows messaging is a feature of the operation system. It provides by holding a list of all 'windows' (handles) and managing a 'queue' for each handle. With SendMessage you put a message at front of queue so that it was processed immediately (and the SendMessage waits for completion) and with PostMessage you put a message at end of queue. But that only works, if the owner of the handle (window) runs a message loop like the one I posted above, i. e. if it processes the queue. With MFC a window was associated with a CWnd class object so there you don't need to care for the handle. But actually the CWnd::SendMessage(....) simply calls ::SendMessage(m_hWnd, ...). And the MFC runs a seperate message loop for each dialog what actually is the engine of a MFC dialog-based application. Your part of 'customization' only is to add handlers for messages. You can handle mouse messages, timer messages, button-click messages, adn so on. And you can handle user-defined messages as jkr should above. If you handle a message you simply need to define the same message id at the receiver side but don't need to include the header file. But of course you need to 'know' what the message is for in order to correctly handling it. As told above, a dialog class normally wouldn't send user-defined messages to controls but simply get a CWnd pointer by GetDlgItem (or define a member for the control with the wizard) and then calls the needed function directly. It would be much more efforts to define a user-message, call SendMessage, define a handler for the target class, make a message map entry in order to get the handle to be triggered (from message loop with DispatchMessage) and finally call the function which you simply have been called directly. But, you are right, if calling SendMessage you don't need any header information of the target window but only the target window handle, And the receiver, doesn't need any information or header from the sender but only needs to user the same message id.

Regards, Alex
0
JPM2002Author Commented:
Thanks to all 3 experts who helped me.  I reward the highest amount to jkr
(50) and Alex and Andy 25 each.  I played with the code and mad some modifications to it to work properly in my app.  Many thanks.  JM
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.