Listbox
I've never been able to use the listbox with any success and I believe that I need to do it here... I'm hoping some of you might help walk me through the learning curve... What I have is a dialog box and once you hit the "Add" button, the values drop to the bottom. The thought was that one could keep adding to this list and when all was complete, you hit the save button and I create a series of files for each line item... Right now I have it to the point of one complete line but the contents are into a editbox :eek: ... I know the first step is to replace the editbox with the listbox...Then I'm clueless...
Thanks,
Ray Schmidt
# 1 Re: Listbox
Well, I always have problems with listboxes too. That's the reason why I always use CListCtrl.
I can't find a way to delete all items with one function in a listbox, but with a CListCtrl, I can.
So my advice, use a CListCtrl. It also supports columns and all that stuff.
# 2 Re: Listbox
I can't find a way to delete all items with one function in a listbox, but with a CListCtrl, I can.
Now you can ;)
CListBox::ResetContent (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcemfc/htm/clistbox_32.asp)
To OP:
Well, I think, you want to replace the edit box with the list box. When you press add button, use CListBox::AddString (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_clistbox.3a3a.addstring.asp) to add the entries and when you want to save, you can use CListBox::GetCount (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_clistbox.3a3a.getcount.asp) to loop through all the entries, use CListBox::GetText (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistbox.3a3a.gettext.asp) to get the text one by one and save them in the file.
Ejaz at 2007-11-11 0:31:10 >

# 3 Re: Listbox
Thank you...
It was much easier just adding:
CListBox* pmyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
CString str;
str.Format(_T("%s %s %s %s %s %s %s %s %s"),truss);
pmyListBox->AddString(str);
vs.
m_displaybox = truss.Name;
m_displaybox += " ";
m_displaybox += truss.Qty;
m_displaybox += " ";
m_displaybox += truss.Span;
m_displaybox += " ";
m_displaybox += truss.TCP;
m_displaybox += " ";
m_displaybox += truss.BCP;
m_displaybox += " ";
m_displaybox += truss.LeftOH;
m_displaybox += " ";
m_displaybox += truss.LOHType;
m_displaybox += " ";
m_displaybox += truss.RightOH;
m_displaybox += " ";
m_displaybox += truss.ROHType;
How do I go about saving this information into CFileDialog FileDialog? What should I be looking at for doing this?
Thanks,
Ray Schmdit
# 4 Re: Listbox
CListBox* pmyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
CString str;
str.Format(_T("%s %s %s %s %s %s %s %s %s"),truss);
pmyListBox->AddString(str);
Note though, that listboxes do support tabstops as well which might be an option here...take a look at 'SetTabStops()' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_clistbox.3a3a.settabstops.asp)...
How do I go about saving this information into CFileDialog FileDialog? What should I be looking at for doing this?
Well...I don't quite understand...what would you like to add to 'CFileDialog'? The file dialog is ues to open or save files and not to display any other output? :confused:
# 5 Re: Listbox
hmmm... so to add at tab every variable I should add something like CListBox* pmyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
CString str;
pmyListBox->SetTabStops(5); // ??
str.Format(_T("%s%s%s%s%s%s%s%s%s"),truss);
pmyListBox->AddString(str);
As far as "'CFileDialog'", I don't know how to complete the saving process using this. Normally, I would just save w/o using the CFileDialog... Since CFileDialog allows one to specify a location in a typical format, I would like to take advantage of it...
# 6 Re: Listbox
hmmm... so to add at tab every variable I should add something like[/code]
Well...you would do...
// In 'OnInitDialog'
// Set tabstops in pixel values
int tabs[] = { 10, 30, 60, 74, 80, 100, 115, 130 };
CListBox* pmyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
pmyListBox->SetTabStops(sizeof(tabs) / sizeof(tabs[0]), tabs);
// Adding data
CListBox* pmyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
CString str;
str.Format(_T("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"), "A", "B", "C", "D", "E", "F", "G", "H", "I");
pmyListBox->AddString(str);
[QUOTE=Ray Schmidt]As far as "'CFileDialog'", I don't know how to complete the saving process using this. Normally, I would just save w/o using the CFileDialog... Since CFileDialog allows one to specify a location in a typical format, I would like to take advantage of it...
I am still not sure whether I understood the above...'CFileDialog' simply lets you choose the location a file can be saved. It does not do the actual saving though...
CFileDialog save_dlg(FALSE, ".txt", 0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Text files (*.txt)|*.txt|All Files (*.*)|*.*|" , 0);
if(save_dlg.DoModal() == IDOK)
{
CString t;
t = save_dlg.GetPathName();
// Actually save
}
# 7 Re: Listbox
First, thank you for your help!!!
The following code was added to mine and no matter what I do in changing it, I get no tabs...
void CDFModifierDlg::OnAdd()
{
// In 'OnInitDialog'
// Set tabstops in pixel values
int tabs[] = { 10, 30, 60, 74, 80, 100, 115, 130 };
CListBox* pmyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
pmyListBox->SetTabStops(sizeof(tabs) / sizeof(tabs[0]), tabs);
CString str;
struct New
{
CString Name;
CString Qty;
CString Span;
CString TCP;
CString BCP;
CString LeftOH;
CString LOHType;
CString RightOH;
CString ROHType;
} truss;
UpdateData(TRUE);
truss.Name = m_name;
truss.Qty.Format("%ld",m_qty); // int to CString on the Qty
truss.Span.Format("%0.8f",m_span); // double to CString
truss.TCP.Format("%0.8f",m_tc); // double to CString
truss.BCP.Format("%0.8f",m_bc); // double to CString
truss.LeftOH.Format("%0.8f",m_loh); // double to CString
truss.LOHType = m_loht ; // m_loht
truss.RightOH.Format("%0.8f",m_roh); // double to CString
truss.ROHType = m_roht; // m_roht
str.Format(_T("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"), "A", "B", "C", "D", "E", "F", "G", "H", "I");
//str.Format(_T("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"),truss.Name,truss.Qty,truss.TCP,truss.BCP,truss.LeftOH,truss.LOHType,truss.RightOH,truss.ROHType);
//str.Format(_T("%s %s %s %s %s %s %s %s %s"),truss);
pmyListBox->AddString(str);
UpdateData(FALSE);
}
As far as the other issue, well I just was'nt sure how to handle the opening and reading of the file. This is what I have so far and I think I heading in the right direction...
void CDFModifierDlg::OnOpen()
{
CFileDialog FileDialog(TRUE,
"*.*",
NULL,
OFN_HIDEREADONLY,
"TrusWal Truss Files: (*.df)|*.df||");
if(FileDialog.DoModal() == IDOK)
{
CListBox* pmyListBox = (CListBox*)(GetDlgItem(IDC_LIST1));
CString str;
CFile file;
UpdateData(TRUE);
CString PathName = FileDialog.GetPathName();
file.Open(PathName,CFile::modeRead);
char ch[100];
int FileLength=file.GetLength();
file.Read(ch, FileLength);
str.Format(_T("%s"),ch);
pmyListBox->AddString(str);
m_displaybox += PathName;
m_displaybox += "\r\n";
file.Close();
UpdateData(FALSE);
}
# 8 Re: Listbox
The following code was added to mine and no matter what I do in changing it, I get no tabs...
In order to respond to this function, the listbox has to be created with the 'LBS_USETABSTOPS' style (either through code or the checkbox in the resource editor). It looks like you didn't...
As far as the other issue, well I just was'nt sure how to handle the opening and reading of the file. This is what I have so far and I think I heading in the right direction...
Well...are you trying to read in the file line-by-line and display each line in the listbox? Or are you trying to parse the file and put the separated strings into the listbox?
# 9 Re: Listbox
Presto on the whole tabs deal!!!
The information being read in from the file should go to my edit boxes for each variable. Now that I have the file opening up, what would be the best approach to reading in this information. Some kind of getline function or read the whole file into a variable and sort it out? Example of file data (ASCI txt):
4 COMMENT LINE
6 T1
7 24
8 5
9 2.5
22 2 2 2 2
Thanks,
Ray Schmidt
# 10 Re: Listbox
Well...kind of depends on the layout...is the shown layout one set of data that should go into one row of the listbox? If so...reading in the file and parsing line-by-line should be one way...
# 11 Re: Listbox
All the information would be like this:
(File)
4 COMMENT LINE
6 T1
7 24
8 5
9 2.5
22 2 2 2 2
(ListBox)
4 COMMENT LINE 6 T1 7 24 8 5 9 2.5 22 2 2 2 2
And I figured that I would put all of this in a single line of the listbox... I've been trying CFile & CStdioFile to open and read the file. How do I eliminate the "|" at the end of each line (see pic below)...
Thanks,
Ray Schmidt