Thread: passing value from one dlg to another

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    74

    passing value from one dlg to another

    Dear all,


    i'm a new guy in c++, i'm making a program using MFC dialogue, i need to pass an array value from one dialogue to another, but the other dialogue doesn't recognise the variable even i've include the previous dlg header file, so, how to do that? thanks very much!!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Will one dialog call the other dialog or will both be open and active simultanously ?

    If one dialog openes the other one, like showing details, you can pass your variable(s) in the constructor of the second dialog.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    my application is click on the button of the dialogue will show the other dialogue...acutally i dun understand how to use constructor to pass the value in this way, is there any other method? thanks~

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Moved to Windows board.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your first post is a bit vague. Why not post the code so we can see what you're doing wrong?

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    As long as you call the base constructor CDialog with the params it requires you can then add params to your CDialog derived class constructor as you see fit.

    So don't feel as if you cannot add your own parameters to the constructor just because you have to init the base class constructor.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    i use these code:
    i declare details in both dialogue file, and

    Code:
    resultDlg result;
    result.details=details;to pass the value to the result dialogue from the input dialogue where result is the result dialogue object.
    but the result is the dailogue appear said that the debug assertion fail.

    what should i do to pass the value between dialgue? thanks!!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That can't be called code and it doesn't help at all.
    Post a snippet of your class declaration and the code that creates the variable you want to assign and the code where you DO assign it to the class. That, at the very least, should help.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That snippet does not help since it won't even work.

    If you are subclassing a dialog in MFC then you can pass data from the dialog back to the caller in several ways.

    1. In the designer set a variable to be associated with the control. In code provide a means to get the value of this variable. When the dialog user presses OK call CDialog::UpdateData(TRUE). This will update the variables associated with controls to the value that is in the controls at the time the user pressed OK. Retrieve these variables from another class or object via pointers to the class or some other method.

    2. Set the variables manually by scanning the controls, retrieving their state (text, value, or other), storing that in class members, and retrieving later via pointers to the class or some other method.

    In both instances this requires that the CDialog derived object exists even after the user pressed OK and the dialog disappears or becomes hidden. If the CDialog object does not exist then the variables do not exist and you will have a serious problem if you try to access the object since it does not exist.

    Novacain also showed me this at one time and it has been a huge help. You can get a pointer to your main window by doing this:

    Code:
    CMainFrame *ptrFrame = (CMainFrame *)AfxGetMainWnd();
    You can use this pointer to access variables in your CFrameWnd derived class. Very handy.
    Last edited by VirtualAce; 11-24-2007 at 10:52 AM.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    this is my code:


    Code:
    Search.cpp
    
    void ASearch::OnBnClickedButton1()  //ok button to search
    {		
    	        searchNumber();
    	
    	if (resultCount>0){
    
        Search_result result;
    		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;
    
    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);
    
    
    }
    the result dialogue show nothing, where my input exist in the database, any thing need to be changed in tmy code? thanks a lot!
    Last edited by mr_empty; 11-25-2007 at 09:18 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Your identation is horrible.
    2) Don't just use generic names such as button1. NAME your controls.
    3) You're expecting a local object to keep its data when you construct another, NEWER object of the same type later? Come on. Once your dialog goes out of scope, everything it holds will be destructed as well.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    1) Your identation is horrible.
    2) Don't just use generic names such as button1. NAME your controls.
    3) You're expecting a local object to keep its data when you construct another, NEWER object of the same type later? Come on. Once your dialog goes out of scope, everything it holds will be destructed as well.
    i declare the public variable in both header files, and use this:
    as_result.details=details;
    and retrieve the data in result dialogue,
    can't it work? then what should i do? thank you

  13. #13
    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/

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention there's one dialog and (possibly) more than one result, resulting in overwriting the last find.

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