-
April 8th, 2013, 04:27 AM
#1
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);
}
-
April 8th, 2013, 04:46 AM
#2
Re: String passing between functions in VC++ SDI application
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?
Last edited by VictorN; April 8th, 2013 at 05:11 AM.
Victor Nijegorodov
-
April 8th, 2013, 05:04 AM
#3
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.
Last edited by maxer; April 8th, 2013 at 06:01 AM.
-
April 8th, 2013, 05:15 AM
#4
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?
Victor Nijegorodov
-
April 8th, 2013, 05:50 AM
#5
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);
}
Last edited by maxer; April 8th, 2013 at 06:25 AM.
-
April 8th, 2013, 06:15 AM
#6
Re: String passing between functions in VC++ SDI application
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.
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?
Victor Nijegorodov
-
April 8th, 2013, 07:09 AM
#7
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.
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);
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 8th, 2013, 11:49 PM
#8
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
Last edited by maxer; April 9th, 2013 at 01:59 AM.
-
April 9th, 2013, 01:51 AM
#9
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++;
Last edited by maxer; April 9th, 2013 at 01:54 AM.
-
April 9th, 2013, 03:01 AM
#10
Re: String passing between functions in VC++ SDI application
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?
Victor Nijegorodov
-
April 9th, 2013, 03:46 AM
#11
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
-
April 9th, 2013, 03:56 AM
#12
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
Victor Nijegorodov
-
April 9th, 2013, 04:13 AM
#13
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
-
April 9th, 2013, 04:19 AM
#14
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
....
}
Victor Nijegorodov
-
April 17th, 2013, 01:10 AM
#15
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);
}
Last edited by maxer; April 17th, 2013 at 01:20 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|