multiple input data(ListBox): newbie MFC Programming question?
Hi Guys,
Say i have aprogram with my first edit box:
Edit Box 1asking for Number of People:
Once this is entered, I want to ask the following questions:
Age:
Name:
Height:
So say the user enters 20 people, I need to ask for the above details 20 times?
Because I don’t know how many people the user will enter I cannot create all the edit boxes for this before so what’s the best programming/practical way to do this?
I am sorry if this is such a basic question but I am just learning MFC and I’m not sure how this can be done. Because from what I know so far if I create an edit box I can only attach one variable (which threw my idea of using arrays out the window) to it…so does that mean I need 20 different edit boxes.
Please can anyone offer any advise or direct me in the right path!
Re: Program doesn't run!!!
Quote:
Originally posted by Ankoump
Hi There,
I tried the above example in my program just to get the hang of it but its giving errors that i really don't understand...
Please can anyone help!!!!
I think the problem is from the class CPerson, because i wasn't sure what to derive it from.....also i haven't deleted any of the objects.
Please see attached program!
post a compilable project.
Newbie: Visual C++ Programming question?
Hi guys,
Regarding the help given above, i have copied it below for creating a list box and populating it (my question is given below after the quotes):
Quote:
Originally posted by astanley
Well, this is where it gets a (little) bit clever.
Create a class to represent people, e.g., CPerson. The class should have properties for each bit of data about the person, e.g., name, age, height etc. Each person (row) in the list box is an object of this class. Use CListBox::SetItemDataPtr to associate the row in the list box with the person object for that row.
Something like this (CPersonDlg is your popup child dialog):
Code:
OnAdd:
CPersonDlg PersonDlg;
if (PersonDlg.DoModal() == IDOK)
{
// OKed from child dialog, so add new person to list box.
int nIndex = m_myListBox.AddString(PersonDlg.m_sPersonName);
if (nIndex != LB_ERR && nIndex != LB_ERRSPACE)
{
// New person successfully added to list box, so create a person object for them, using
// the member variables from the child dialog.
CPerson* pPerson = new CPerson(PersonDlg.m_sPersonName, PersonDlg.m_nSex, PersonDlg.m_DataOfBirth);
if (pPerson != NULL)
{
// Associate the new person object with the row in the list box.
m_myListBox.SetItemDataPtr(nIndex, pPerson);
}
}
}
OnEdit:
int nIndex = m_myListBox.GetCurSel();
if (nIndex != LB_ERR)
{
// Row is selected in list box, so get person object for this row, and pass to
// contructor for child dialog.
CPerson* pPerson = m_myListBox.GetItemDataPtr(nIndex);
if (pPerson != NULL)
{
CPersonDlg PersonDlg(pPerson);
if (PersonDlg.DoModal() == IDOK)
{
// Persons details have been edited, so update row in list box.
m_myListBox.DeleteString(nIndex);
// If sorting the list box, use AddString instead of InsertString.
nIndex = m_myListBox.InsertString(nIndex, pPerson->m_sPersonName);
// Associate row with existing person object.
if (nIndex != LB_ERR && nIndex != LB_ERRSPACE)
{
m_myListBox.SetItemDataPtr(nIndex, pPerson);
}
}
}
}
You will need to delete the pPerson objects in the destructor for the main dialog, e.g., iterate over the rows in the list box using GetItemDataPtr to get the person object for that row, and delete the object.
I just wanted to find out when learning C++ programming i thought that we could not assign a pointer to something else without deleting it first. But in this example we create a new CPerson on the freestore and then assign its address to the pointer pPerson. And then for the next person we do the same thing again.....My question is, is this dangerous as we are assigning a pointer that's pointer to a certain location in memory to another location without freeing the previous memory!Have i got these wrong???Please help
MASSIVE THANKS!That clears things up but i have another question?
Thanks a lot guys that really clears this things up for me as i find pointers very confusing, another question however is can i then use this pPerson in any function i create within my program to access the information associated in that row:
nIndex = 0;
CPerson* pPerson = m_myListBox.GetItemDataPtr(nIndex);
i.e pPerson->m_strName;
and secondly when you say delete these pointersin the main dialog i'm assuming this is the same as my view class destructor
and what happens if you don't delete it, should it not delete itself once the view closes i.e the program ends?
:confused: