Loop doesn't update listboxes until it is done iterating

丶灬走出姿态 提交于 2019-12-25 01:26:46

问题


I have a loop that takes file name from a listbox, performs a system() call, then moves that filename to another listbox. Problem is, it doesn't move the filenames over one at a time, but waits until the entire loop is finished and moves them all at once. What would I do to get it to perform how I want it to?

the loop:

for each( String^% file in filename )
{
    int x = convert( file );
    lbComplete->Items->Add( lbFiles->Items[0] );    // place the completed file
    lbFiles->Items->Remove( lbFiles->Items[0] );    // in the other listbox
}

The function convert() that contains the system call:

int convert( String^ file )
{
    std::stringstream ss;
    std::string dir, fileAddress, fileName, outputDir;
    ...
    return system( ss.str().c_str() );          
}

回答1:


You would need to call a refresh function at the end of your loop forcing it to redraw your listboxes otherwise it will wait until after your loop to do so.

for each( String^% file in filename )
{
    int x = convert( file );
    lbComplete->Items->Add( lbFiles->Items[0] );    // place the completed file
    lbFiles->Items->Remove( lbFiles->Items[0] );    // in the other listbox
    // INSERT REFRESH FUNCTION HERE
}



回答2:


It's very common in GUI programming that methods called from the "event thread" or "event loop" need to finish before the changes they make are seen on the screen. That's regardless of the language or the environment. The usual way to avoid that problem on the things I'm most familiar with (like Java Swing) is to do the updates outside of the event loop, but use notifications (swing events) to tell the GUI to update the display occassionally.




回答3:


Tell both listboxes to refresh themselves after moving an item.



来源:https://stackoverflow.com/questions/2958494/loop-doesnt-update-listboxes-until-it-is-done-iterating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!