Results 1 to 5 of 5

Thread: VB 2008 Display Specified Value in the Centre (vertically) of a List Box on Opening

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2023
    Posts
    3

    VB 2008 Display Specified Value in the Centre (vertically) of a List Box on Opening

    Hi all,
    I pre-populate a list box with values using a Do Until loop that represent altitudes for an aircraft one thousand feet apart vertically - in this case from 100, 110,...400, 410, 420, 430 (43,000ft). When the list box opens, the top of the range (430) is displayed at the top of the list. However, I want the last selected value from the list (representing the level that the aircraft has currently been cleared to fly at) to appear in the middle of the range of values when the list box is opened by the user the next time. I have 9 values displayed in the list, so, for example, the last value selected was 330, so when the list box opens, I want 330 displayed in the middle of the range of values, with 370 at the top and 290 at the bottom. I have searched a number of forums but have not seen anything that covers this issue. Thanks for any help that can offered.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: VB 2008 Display Specified Value in the Centre (vertically) of a List Box on Openi

    Read up on the TopIndex property of ListBox. That lets you set the index that is visible at the top of the ListBox, which by extension allows you to control which item is in the "middle" of the ListBox. Of course you'll have to do some simple math to calculate which index to make the "top" if all you have is the index that you want in the middle.

    https://learn.microsoft.com/en-us/do...owsdesktop-7.0

    Good luck.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2023
    Posts
    3

    Re: VB 2008 Display Specified Value in the Centre (vertically) of a List Box on Openi

    Quote Originally Posted by OptionBase1 View Post
    Read up on the TopIndex property of ListBox. That lets you set the index that is visible at the top of the ListBox, which by extension allows you to control which item is in the "middle" of the ListBox. Of course you'll have to do some simple math to calculate which index to make the "top" if all you have is the index that you want in the middle.

    https://learn.microsoft.com/en-us/do...owsdesktop-7.0

    Good luck.
    Excellent. I will have a look at the material. Thanks very much!

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2023
    Posts
    3

    Re: VB 2008 Display Specified Value in the Centre (vertically) of a List Box on Openi

    Quote Originally Posted by OptionBase1 View Post
    Read up on the TopIndex property of ListBox. That lets you set the index that is visible at the top of the ListBox, which by extension allows you to control which item is in the "middle" of the ListBox. Of course you'll have to do some simple math to calculate which index to make the "top" if all you have is the index that you want in the middle.

    https://learn.microsoft.com/en-us/do...owsdesktop-7.0

    Good luck.
    Thanks OptionBase1. I didn't know that the TopIndex property existed, and after the mental gymnastics that I had been contemplating with index values, I am amazed that the solution was so simple. I set the initial displayed range with a hard-coded value and then it is just a case of TopIndex = SelectedIndex - 4. Brilliant!!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB 2008 Display Specified Value in the Centre (vertically) of a List Box on Openi

    Quote Originally Posted by cjr23 View Post
    I didn't know that the TopIndex property existed
    This is why you should ALWAYS read the documentation. Whenever you're using a new type or member, read the documentation first and you may just learn something new that you wouldn't have even known to look for. In the case of new types, you can see what members it has and, even if you don't need them now, you'll know they are there if you do need them in future. You can go straight to the topic for a type or member using context-sensitive Help (F1) or use the Help menu to go to the home page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width