String passing between functions in VC++ SDI application
I am doing an project in SDI. I have two functions name sendtext(CString str) and displaytext(CString inr) both in different class. I have a pointer name pView to send the string str to function "displaytext". The problem is after some operations i get a text in str and i send that text to display text in the output screen i get the text and wen the second text comes to "displaytext" the former text disappears and the latest string only present. Pleae help me like how can i display both the text in the output window on ClistCtrl class.
Code:
Void sendtext(CString str)
{
// do some operations
................
pView->displaytext(str);
}
void displaytext(CString inr)
{
CListCtrl &ctlsde = this->GetListCtrl();
ctlsde.InsertColumn(1, _T("First "), LVCFMT_LEFT, 80);
int nItem;
nItem = ctlsde.InsertItem(0, inr);
}
Re: String passing between functions in VC++ SDI application
Quote:
Originally Posted by
maxer
Code:
Void sendtext(CString str)
{
// do some operations
................
pView->displaytext(str);
}
void displaytext(CString inr)
{
CListCtrl &ctlsde = this->GetListCtrl();
ctlsde.InsertColumn(1, _T("First "), LVCFMT_LEFT, 80);
int nItem;
nItem = ctlsde.InsertItem(0, inr);
}
- Better way to pass CString into a function is by const reference, not by value! Like
Code:
void sendtext(const CString& str)
...
void displaytext(const CString& inr)
- What is pView?
- Is your displaytext a global function? Then what is this pointer within it?
Re: String passing between functions in VC++ SDI application
Thanks for ur reply :) ...
pView is the listview pointer used for sending the data from treeview to list view.
Nope display text is not a global function. It is a local function where we display the received text in the list view.
Re: String passing between functions in VC++ SDI application
Then please, post your real code rather than a pseudo one. Otherwise you woulg get a pseudo help instead of a real one!
Where exactly are you trying to display your text?
Why do you insert the same column "First " by every call of your displaytext function?
Re: String passing between functions in VC++ SDI application
Hi! this is the complete code which i hav done untill now
Code:
Void sendtext()
{
CTreeCtrl& trCtrl = GetTreeCtrl();
HTREEITEM hItem,hsc;
CExerciseDoc *pDoc = GetDocument();
CString pathname,strLine;
CString filename;
CFileDialog dlg(TRUE);
dlg.DoModal();
if(dlg.DoModal() == IDOK)
{
pathname=dlg.GetPathName(); // return full path and filename
}
CStdioFile File;
if(File.Open(pathname, CFile::modeRead)) // Open file to be read
{
while(File.ReadString(strLine)) // Read file
{
int Position = 0;
CString Token;
CAtlString str(strLine);
CAtlString resToken,resToken1;
resToken = str.Tokenize(_T("-:, "), Position); //A,B
pDoc->pSendview->displaytext(resToken);
if(resToken != (_T(""))) // Empty File Check
hItem = trCtrl.InsertItem(resToken , 0, 2 );
while(resToken!="")
{
resToken = str.Tokenize(_T("-:@, "), Position);
}
}
}
}
}
void CRightView::displaytext(CAtlString league)
{
CListCtrl &ctlRightView = this->GetListCtrl();
ResetLeagues();
CAtlString resToken;
ctlRightView.InsertColumn(1, _T("First "), LVCFMT_LEFT, 80);
ctlRightView.InsertColumn(1, _T("Second "), LVCFMT_LEFT, 80);
int nItem;
nItem = ctlRightView.InsertItem(0, league);
ctlRightView.SetItemText(nItem, 1, league);
}
Re: String passing between functions in VC++ SDI application
Quote:
Originally Posted by
maxer
I am doing an project in SDI. I have two functions name sendtext(CString str) and displaytext(CString inr) both in different class. I have a pointer name pView to send the string str to function "displaytext". The problem is after some operations i get a text in str and i send that text to display text in the output screen i get the text and wen the second text comes to "displaytext" the former text disappears and the latest string only present. Pleae help me like how can i display both the text in the output window on ClistCtrl class.
Quote:
Originally Posted by
maxer
Hi! this is the complete code which i hav done untill now
So where exactly do you have a situation "the second text comes to "displaytext" the former text disappears and the latest string only present"?
And what exactly "after some operations" mean?
Re: String passing between functions in VC++ SDI application
In displaytext, why are you inserting columns everytime? Why are you passing leauge by value rather than as a const reference? Also resToken is not used.
Quote:
the former text disappears and the latest string only present.
Correct! This is what your code does. It always inserts the item at index 0. If you want to display more than one item in the listview then you need to increment the index value used each time. Alternatively, you can use a very large index number in which case the item is inserted at the end of the list and nItem is then the index at which the value was inserted.
Code:
nItem = ctlRightView.InsertItem(0, league);
Try this
Code:
nItem = ctlRightView.InsertItem(9999, league);
Re: String passing between functions in VC++ SDI application
Let me explain you. In the sendtext() i open a text file which consists of some data with delimiters like man-woman so after opening i tokenize it and the tokenized output is resToken which is sent to displaytext() via
Code:
pDoc->pLeftView->displaytext(resToken);
and in the client window i am getting the text "man" and it goes for empty file check and after the while loop is broken it comes to the same tokenizing area and starts tokenizes the new line in the text file say brother-sister and tokenized output is sent to displaytext via the same piece of code mentioned above.
On the client window i am getting the thing as "brother" i dont see "man" .. i want both the "man and brother" to be displayed as
First(Column name)
brother
man
Re: String passing between functions in VC++ SDI application
2Kuad thanks for ur reply ... the resToken in the displaytext() i forgot to remove it let it b unused. And i hav stopped calling InsertItem function every time ratrher i have put that piece of code in OnInitialUpdate function but still i face the same prob i have incremented the nItem using this am i right here
Code:
int nItem;
nItem = ctlRightView.InsertItem(0, league);
nItem++;
Re: String passing between functions in VC++ SDI application
Quote:
Originally Posted by
maxer
2Kuad thanks for ur reply ... the resToken in the displaytext() i forgot to remove it let it b unused. And i hav stopped calling InsertItem function every time ratrher i have put that piece of code in OnInitialUpdate function but still i face the same prob i have incremented the nItem using this am i right here
Code:
int nItem;
nItem = ctlRightView.InsertItem(0, league);
nItem++;
How many time else it should be repeated until you uderstand that you just overwrite your previous text because you set your new text in the same place: in the first column of the first row of your listview!
If you just moved this code into OnInitialUpdate that it would executed only once and nothing could be updated!
Now eplain, please, what you mean by "i have incremented the nItem using this"? What for haveyou incremented the nItem if you then do NOT use it at all? :confused:
Re: String passing between functions in VC++ SDI application
Victor, thats where im stuck i dont know to proceed further pls help me. like rather than overwriting the former text i need to move the former text to second row first column and new text to first row first column
Re: String passing between functions in VC++ SDI application
Then pass in the 1 intead of 0 as the item index while calling InsertItem.
And for the third row pass 2... and so on
Re: String passing between functions in VC++ SDI application
yeah i am getting your point but the thing is the first text it is getting displayed in index 0
nItem = ctlRightView.InsertItem(0, league);
and while the second text is coming how do i increment the index value
Re: String passing between functions in VC++ SDI application
Very easy:
Code:
int nItem = ctlRightView.GetItemCount();
int index = ctlRightView.InsertItem(nItem, league);
// optional - check the result
if(ind == -1)
{
// error message
....
}
Re: String passing between functions in VC++ SDI application
this is not working Mr.Victor
Code:
void CRightView::Display(const CAtlString league)
{
CListCtrl &ctlRightView = this->GetListCtrl();
ResetLeagues();
ctlRightView.InsertColumn(0, _T("First"), LVCFMT_LEFT, 80);
int nItem = ctlRightView.GetItemCount();
int index = ctlRightView.InsertItem(nItem, league);
}