Thread: passing value from one dlg to another

  1. #16
    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.

  2. #17
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Bubba View Post
    There are several options:

    1. Create the search object on the heap and retrieve the values from it later.
    2. Store the results in class members after the search dialog is done. Then when it goes out of scope you will have saved the important data you may need for later.
    thank you for your suggestion, but i dun know how to use heap, is it a must to use heap or is there any other way? thanks!

  3. #18
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well in C++, it'd be new and delete. Look them up.

    Eg, https://msdn2.microsoft.com/en-us/li...ba(VS.71).aspx

  4. #19
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I suggest you learn C++ programming before attempting to tackle anything in MFC.

  5. #20
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I agre with the suggestions above.....


    Easiest soulution....

    Store the DB data in the Search cpp (as a public member variable or as a property).

    From the code this looks like the 'Details' variable.

    In the 'results' dialog set/get the results by using (or similar)

    GetParent()->Details
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    actually i have got the result in the list box now, but i need to click on the list box once to display the result so that i think there's problem in my code before...

    however, now a problem arises, how to display the result without click on the list box??

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do it in your dialog's InitDialog procedeure.

  8. #23
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by mr_empty View Post
    actually i have got the result in the list box now, but i need to click on the list box once to display the result so that i think there's problem in my code before...

    however, now a problem arises, how to display the result without click on the list box??
    Sounds like a drawing issue (data is in LV but not asked to be drawn to the screen until you click).

    Add a call to redraw the dlg

    InvalidateRect
    UpdateWindow
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #24
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    Do it in your dialog's InitDialog procedeure.
    my result dialog is not called by initDialog cpp file...

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you actually know anything of MFC?
    Override the class's InitDialog function. There's where you put init code.
    Also, from what I can see, the list is updated once it recieves the focus. I don't know if a child window will recieve focus when a list is created.

  11. #26
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    Do you actually know anything of MFC?
    Override the class's InitDialog function. There's where you put init code.
    Also, from what I can see, the list is updated once it recieves the focus. I don't know if a child window will recieve focus when a list is created.
    thanks for reply, i'm a beginner of MFC and i'm still kearnning when i'm working on this program..
    u said do it in the init code, but actually how's the code? just call the dialogue ? thnaks~

  12. #27
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    o ic, just add string into the list in the oninitdialog can do this, thanks

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it can, and it's the best place to do it.

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