ListBox with Columns
-
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
-
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
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.
-
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.
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
-
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
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?