String passing between functions in VC++ SDI application
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: String passing between functions in VC++ SDI application

  1. #1
    Join Date
    Mar 2013
    Posts
    9

    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);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    Re: String passing between functions in VC++ SDI application

    Quote Originally Posted by maxer View Post
    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);
    }
    1. 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)
    2. What is pView?
    3. 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

  3. #3
    Join Date
    Mar 2013
    Posts
    9

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    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

  5. #5
    Join Date
    Mar 2013
    Posts
    9

    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.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    Re: String passing between functions in VC++ SDI application

    Quote Originally Posted by maxer View Post
    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 View Post
    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

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,366

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Mar 2013
    Posts
    9

    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.

  9. #9
    Join Date
    Mar 2013
    Posts
    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.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    Re: String passing between functions in VC++ SDI application

    Quote Originally Posted by maxer View Post
    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

  11. #11
    Join Date
    Mar 2013
    Posts
    9

    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

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    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

  13. #13
    Join Date
    Mar 2013
    Posts
    9

    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

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    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

  15. #15
    Join Date
    Mar 2013
    Posts
    9

    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.

Page 1 of 2 12 LastLast

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center