Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I buil a visual c++ form and i included a listbox for fisplaying message so the user knows where the program is. My problem is that the listbox only show the messages when the program ends.

How can i update/refresh/redraw my listbox?


My Code:

C#
if(cliente==NULL)
{
    listBox1->Items->Add( String::Format("Autopatch iniciado pela 1 vez...") );
    listBox1->Items->Add( String::Format("A criar configuracoes...") );
    listBox1->Items->Add( String::Format("Pode demorar vários minutos...") );
}
f2=fopen("caminho.txt","w");
lister(caminho_actual);
 fflush(f2);
fclose(f2);
f2=fopen("caminho.txt","r");
cliente=fopen("cliente.dat","w");
Posted
Comments
Maximilien 31-Aug-11 13:05pm    
The list will probably not be refreshed because the other functions calls after adding the items will block the list refresh.

One of the funny things about Windows is that the visible elements (GUI Controls, stuff like that) need to process messages in order to operate correctly.

Any Windows GUI based application needs to ensure that the message processing loop is run in a timely fashion to allow that to work. Some applications do that with a separate GUI thread (something you get for free with an MFC / Dialog based application.

The code you show looks like it's in the main line processing of your application, opening files, etc. If your code never enters an idle state or returns to the message loop until it's finished, you will see the behavior you report.

I cannot tell you how to structure your code without seeing lots more of it. However, you can force the message processing loop to run by calling
void ProcessWindowMessages()
{
	MSG msg;
	
	while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))	// let them see the message before we go into longer term wait
	{
		TranslateMessage(&msg);							// translate it
		DispatchMessage(&msg);							// and let windows dispatch it to WinProc
	}
}
at reasonable time, like after each time you put something in the listbox. Not that this is the best way to do things but it is often a good way depending on the design of your application.
 
Share this answer
 
After you add items to your listbox, you can call UpdateWindow on that box to force it to redraw. For example:
C++
listbox1->UpdateWindow();

Hope that helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900