Thread: passing value from one dlg to another

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    // Search.cpp
    
    void ASearch::OnBnClickedButton1()  //ok button to search
    {		
            searchNumber();
    	if (resultCount > 0)
    	{
    		Search_result result; // YOU ARE CREATING A NEW OBJECT
    		result.DoModal();
    	}
    	else if (resultCount==0)
    	{
    		edittxt.SetWindowText(L"un ok");
    	}
    	UpdateData(TRUE);
    	// TODO: Add your control notification handler code here
    }
    
    void Search::searchNumber()
    {
    	busDB bus_db;
    	resultCount = 0;
    	int dbsize = bus_db.getDbSize();
    
    	CString editbxNo;
    	edittxt.GetWindowText(editbxNo); 
    
    	Bus AS_resultdb[817];
    	Search_result as_result; // AGAIN, YOU ARE CREATING A NEW, LOCAL OBJECT.
    
    	for (int i = 1; i <= 100; i++)
    	{
    		if (bus_db.getNumber(i).Find(editbxNo) >= 0)
    		{
    			details = bus_db.getRemark(i) + bus_db.getNumber(i) + bus_db.getTitle(i);
    			as_result.details = details;
    			UpdateData(TRUE);
    			++resultCount;
    		}
    	}
    }
    
    
    //Search_result.cpp:
    
    void ASearch_result::OnLbnSetfocusList5()   //listbox to start listing details
    {
    	// TODO: Add your control notification handler code here
    	this->AS_result_list.AddString(details);
    }
    Your indentation is so horrible, I had to fix it for you.
    Look where it's red - see? You're creating local objects! MFC works on the principle that all dialogs are objects - when they go our of scope, your dialog is destroyed.
    So what are you doing wrong? You're creating a dialog class, assigning data to it, it then gets destroyed, along with all its data. Then you're creating a NEW dialog class and expect the data to still be there.

    I suggest you read about classes, objects, references and pointers.
    http://www.cprogramming.com/tutorial/

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    Code:
    // Search.cpp
    
    void ASearch::OnBnClickedButton1()  //ok button to search
    {		
            searchNumber();
    	if (resultCount > 0)
    	{
    		Search_result result; // YOU ARE CREATING A NEW OBJECT
    		result.DoModal();
    	}
    	else if (resultCount==0)
    	{
    		edittxt.SetWindowText(L"un ok");
    	}
    	UpdateData(TRUE);
    	// TODO: Add your control notification handler code here
    }
    
    void Search::searchNumber()
    {
    	busDB bus_db;
    	resultCount = 0;
    	int dbsize = bus_db.getDbSize();
    
    	CString editbxNo;
    	edittxt.GetWindowText(editbxNo); 
    
    	Bus AS_resultdb[817];
    	Search_result as_result; // AGAIN, YOU ARE CREATING A NEW, LOCAL OBJECT.
    
    	for (int i = 1; i <= 100; i++)
    	{
    		if (bus_db.getNumber(i).Find(editbxNo) >= 0)
    		{
    			details = bus_db.getRemark(i) + bus_db.getNumber(i) + bus_db.getTitle(i);
    			as_result.details = details;
    			UpdateData(TRUE);
    			++resultCount;
    		}
    	}
    }
    
    
    //Search_result.cpp:
    
    void ASearch_result::OnLbnSetfocusList5()   //listbox to start listing details
    {
    	// TODO: Add your control notification handler code here
    	this->AS_result_list.AddString(details);
    }
    Your indentation is so horrible, I had to fix it for you.
    Look where it's red - see? You're creating local objects! MFC works on the principle that all dialogs are objects - when they go our of scope, your dialog is destroyed.
    So what are you doing wrong? You're creating a dialog class, assigning data to it, it then gets destroyed, along with all its data. Then you're creating a NEW dialog class and expect the data to still be there.

    I suggest you read about classes, objects, references and pointers.
    http://www.cprogramming.com/tutorial/


    do u mean when i create the new dialogue object the data will be destroyed as well? i had correct the code as u pointed but the result dialogue is still empty, where is the problem? should i use pointer? and how? could u suggest some code to me? thanks you so much!!
    Last edited by mr_empty; 11-25-2007 at 08:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM