Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ListBox with Columns

ListBox with Columns

Scheduled Pinned Locked Moved C / C++ / MFC
game-devhelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hosam Aly Mahmoud
    wrote on last edited by
    #1

    Hello everyone. I have a dialog to show the scores of a game, and I put a list box in that dialog. I want to show the names and the scores in a table. I just can't find a reference to tell me anything about how to make columns in a list box. I tried something else to temporarily solve the problem, but something is going wrong. I wrote the following piece of code:

    CString Score, Name, Final;
    CRect ScoreRect (0,0,0,0), MyListRect;
    CListBox *MyList = static_cast < CListBox* > ( GetDlgItem ( IDC_MYLIST ) );
    CDC* dc = MyList->GetDC();

    MyScoreFile.ReadString ( Name );
    MyScoreFile.ReadString ( Score );

    MyList->GetWindowRect ( MyListRect );

    while ( ScoreRect.Width() < MyListRect.Width() )
    {
    Name += " ";
    Final.Format ( "%s%s" , Name , Score );
    dc->DrawText ( Final , ScoreRect , DT_NOCLIP | DT_CALCRECT );
    }

    MyList->AddString ( Final );

    The problem is that the text does not appear taking the whole width of the list box, but rather a small region, about two thirds of the width of the list box. I also tried to use MyList->GetClientRect ( MyListRect ) with the same results. Thank you very much.

    Hosam Aly Mahmoud

    D 1 Reply Last reply
    0
    • H Hosam Aly Mahmoud

      Hello everyone. I have a dialog to show the scores of a game, and I put a list box in that dialog. I want to show the names and the scores in a table. I just can't find a reference to tell me anything about how to make columns in a list box. I tried something else to temporarily solve the problem, but something is going wrong. I wrote the following piece of code:

      CString Score, Name, Final;
      CRect ScoreRect (0,0,0,0), MyListRect;
      CListBox *MyList = static_cast < CListBox* > ( GetDlgItem ( IDC_MYLIST ) );
      CDC* dc = MyList->GetDC();

      MyScoreFile.ReadString ( Name );
      MyScoreFile.ReadString ( Score );

      MyList->GetWindowRect ( MyListRect );

      while ( ScoreRect.Width() < MyListRect.Width() )
      {
      Name += " ";
      Final.Format ( "%s%s" , Name , Score );
      dc->DrawText ( Final , ScoreRect , DT_NOCLIP | DT_CALCRECT );
      }

      MyList->AddString ( Final );

      The problem is that the text does not appear taking the whole width of the list box, but rather a small region, about two thirds of the width of the list box. I also tried to use MyList->GetClientRect ( MyListRect ) with the same results. Thank you very much.

      Hosam Aly Mahmoud

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      This seems like a very convoluted way to be using a listbox. If you are restricted to using a listbox, a simpler approach would be to turn on the listbox's LBS_USETABSTOPS style. Then populate it with something like: while (! bDone) { MyScoreFile.ReadString ( Name ); MyScoreFile.ReadString ( Score ); Final.Format ( "%s\t%s" , Name , Score ); MyList->AddString ( Final ); } No doubt you'll need to adjust the "width" of the columns (as 32 DLUs are probably not going to be enough for a name-type column) by calling SetTabStops(). That said, I would use a list control (in report mode) instead.

      H 1 Reply Last reply
      0
      • D David Crow

        This seems like a very convoluted way to be using a listbox. If you are restricted to using a listbox, a simpler approach would be to turn on the listbox's LBS_USETABSTOPS style. Then populate it with something like: while (! bDone) { MyScoreFile.ReadString ( Name ); MyScoreFile.ReadString ( Score ); Final.Format ( "%s\t%s" , Name , Score ); MyList->AddString ( Final ); } No doubt you'll need to adjust the "width" of the columns (as 32 DLUs are probably not going to be enough for a name-type column) by calling SetTabStops(). That said, I would use a list control (in report mode) instead.

        H Offline
        H Offline
        Hosam Aly Mahmoud
        wrote on last edited by
        #3

        Thank you very much for your reply. DavidCrow wrote: I would use a list control (in report mode) instead. I really thank you for this advice. Until the time I posted my question, I did not even know that there is something called a list control that is different from a list box. Meanwhile, I now know it exists. I tried to use it on my own (with the help of MSDN), but I could not get the results I wanted (yet). I would be very thankful if you could tell me of an article that explains the subject in details. Thanky you again.

        Hosam Aly Mahmoud

        D 1 Reply Last reply
        0
        • H Hosam Aly Mahmoud

          Thank you very much for your reply. DavidCrow wrote: I would use a list control (in report mode) instead. I really thank you for this advice. Until the time I posted my question, I did not even know that there is something called a list control that is different from a list box. Meanwhile, I now know it exists. I tried to use it on my own (with the help of MSDN), but I could not get the results I wanted (yet). I would be very thankful if you could tell me of an article that explains the subject in details. Thanky you again.

          Hosam Aly Mahmoud

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Hosam Aly Mahmoud wrote: I tried to use it on my own (with the help of MSDN), but I could not get the results I wanted (yet). I would be very thankful if you could tell me of an article that explains the subject in details. Examples are bountiful, both on MSDN and the Internet (use Google). What exactly is not working for you?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups