listbox visible items

when i resize my listbox the amount of items that i can display changes.  I wrote a small function that returns how many items are visible at any given time.  For some reason my function doesnt work all of the time.

int get_ListBoxVisibleCount()< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

//check to see IF items displayed are less than total displayable

          //divide height of listbox with the height of an item in the listbox for the total items displayed

if(listView1->get_Visible())

          {

                    if(this->WindowState == FormWindowState::Maximized)

                             return (this->listView1->get_ClientSize().get_Height()+7) / this->listBox1->get_ItemHeight();

                   else

                             return (this->listView1->get_ClientSize().get_Height()+14) / this->listBox1->get_ItemHeight();

          }

          else //listbox visible

                    return this->listBox1->get_ClientSize().get_Height() / this->listBox1->get_ItemHeight(); //return VisiblePageCount

}



Answer this question

listbox visible items

  • Bart Elia

    i realized the reason why it doesnt work all of the time. If there are not enough items to display in the listbox that causes the problem. The above code assumes you will always have more items to display than can currently be displayed, oops! That just goes to show why you should have proper error checking!


  • listbox visible items