Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use a membervariable of CForm1 in CForm2

Status
Not open for further replies.

massur

Programmer
Aug 22, 2003
9
DE
HI!
I have got a SDI application with 2 CFormViews, the first is CForm1 and the second is CForm2. On CForm1 i have a combolist with the membervariable m_combo on CForm2 i have a Button, when i click on this Button i want m_combo to add an item with AddString, but i dont know how to use m_combo, also when i include CForm1.h m_combo is always not declared CForm1::m_combo doesnt work, too!

Please help me

Kevin
 
You can use the CDocument class of your SDI application to
pass the data from CForm2 to CForm1. After pressing the button, you set a CDocument variable with the string you want to write in the CForm1's combo-box, and then call CDocument::UpdateAllViews. You must rewrite the CForm1's OnUpdate.
For further informations see MSDN: CView::OnUpdate. There is a thorough explanation of how this mechanism works and how it must be used to efficiently work.

To get the pointer to CDocument, when you press the button:

Code:
CDocument *pDoc = GetDocument();
...now you can use it...

You can make a cast to your class that was derived from CDocument.

Hope it helps,
Liviu
 
If you do not want to modify your document, you can just
use the CDocument's functions: GetFirstViewPosition and
GetNextView to search for the CForm1 view pointer, and then
use it to add the string to the combo-box. If you have just
two views, the search is easy. (all this when you press the
button).

To see an example using the GetFirstViewPosition and
GetNextView also look in MSDN
CDocument::GetFirstViewPosition

Hope it helps,
Liviu.
 
I do not understand how your first methode should work, i tried your second which doesnt work too:
CDocument *pDoc = GetDocument();
POSITION pos = pDoc->GetFirstViewPosition();
CView* pFirstView = pDoc->GetNextView( pos );
pFirstView->m_combo.AddString("blubb");

m_combo is not declared :((((((
 
Hello massur,

I've created a small test SDI application that does what
you want. First I've created the SDI application with SplitWindow selected at step 4 from VC++ 6, button Advance.

After that I've made two Dialogs, one contaning a combo-box,
the other one button.

I've selected the style "child" for each of them (the
dialogs), and Border none ( these from the properties panels
of the dialogs).

I've derived two classes from CFormView, using the dialogs
described above, with ClassWizard : CForm1 and CForm2 classes.

With ClassWizard I've added the variable m_combo to CForm1
class which is bind with the combo-box from the correspondely dialog.

In the class CMainFrame, in function OnCreateClient, I've wrote these:
Code:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{

	if( !m_wndSplitter.CreateStatic(this, 1, 2) ) return FALSE;

	if( !m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CForm1), 
		     CSize(150,300), pContext) ||  
        !m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CForm2), 
		CSize(100,100), pContext))
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}

	return TRUE;

}
[\code] 

My button from Form2 is named Button1, so I've created a function which executes when the button is pressed:

[code]
void CForm2::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CFormsSDI_0Doc *pDoc = (CFormsSDI_0Doc*)GetDocument();

	POSITION pos =  pDoc->GetFirstViewPosition();
	CView *pView;
	
	while((pView = pDoc->GetNextView(pos)) != NULL)
	{
		if( pView->IsKindOf( RUNTIME_CLASS(CForm1) ) )
			break;
	}

	CForm1 *pForm1 = (CForm1*)pView;

	pForm1->m_combo.AddString("AAAA");

	
	
}
[\code]

In the file MainFrm.cpp I've put 
[code]
#include "Form1.h"
#include "Form2.h"
[\code]

In the file Form2.cpp I've included:
[code]
#include "Form1.h"
#include "formsSDI_0Doc.h"
[\code]

formsSDI_0Doc is the name of my document derived class.

In the file Form1.h, before 
[code]
class CForm1 : public CFormView
[\code]
I've put 
[code]
class CFormsSDI_0Doc;
[\code]

That is all. It works fine here.

Hope this helps you,
Liviu.
 
HI!

when i use this code:
void CArtikel::OnEditartikel()
{
CEtikettDoc *pDoc = (CEtikettDoc*)GetDocument();

POSITION pos = pDoc->GetFirstViewPosition();
CView *pView;

while((pView = pDoc->GetNextView(pos)) != NULL)
{
if( pView->IsKindOf( RUNTIME_CLASS(CKunden) ) )
break;
}

CKunden *pKunden = (CKunden*)pView;

pKunden->m_kundenliste.AddString("AAAA");

// TODO: Code für die Behandlungsroutine der Steuerelement-Benachrichtigung hier einfügen

}

my programm crashes at pKunden->m_kundenliste.AddString("AAAA");

why? :(

Massur
 
I did not have any problem with that line.
What does it say when it crashes ?

Liviu.
 
it says - i translate it into english :)

The order in "0x5f481ab6" reprimands on the memory in "0x00000020". The Action "read" couldnt be executed on the memory.

:(((((((((

Kevin
 
Hello massur,

OK. I got what you ment. You used a CComboBoxEx, and I used a CComboBox control. The CComboBoxEx does not support AddString. This is what I've found in C:\ProgramFiles\..\VC98\MFC\Include\AFXCMN2.INL:
Code:
// While CComboBoxEx derives from CComboBox, there are some
// CB_messages the underlying ComboBoxEx control doesn't support.

_AFXCMN_INLINE int CComboBoxEx::Dir(UINT attr, LPCTSTR lpszWildCard)
	{ UNUSED_ALWAYS(attr); UNUSED_ALWAYS(lpszWildCard);
		ASSERT(FALSE); return CB_ERR; }
_AFXCMN_INLINE int CComboBoxEx::FindString(int nIndexStart, LPCTSTR lpszFind) const
	{ UNUSED_ALWAYS(nIndexStart); UNUSED_ALWAYS(lpszFind);
		ASSERT(FALSE); return CB_ERR; }
_AFXCMN_INLINE int CComboBoxEx::AddString(LPCTSTR lpszString)
	{ UNUSED_ALWAYS(lpszString); ASSERT(FALSE); return CB_ERR;}
_AFXCMN_INLINE BOOL CComboBoxEx::SetEditSel(int nStartChar, int nEndChar)
	{ UNUSED_ALWAYS(nStartChar); UNUSED_ALWAYS(nEndChar);
		ASSERT(FALSE); return FALSE; }
_AFXCMN_INLINE int CComboBoxEx::InsertString(int nIndex, LPCTSTR lpszString)
	{ UNUSED_ALWAYS(nIndex); UNUSED_ALWAYS(lpszString);
		ASSERT(FALSE); return CB_ERR; }
[\code]

So, use CComboBox.

Hope it helps,
Liviu.
 
No thats wrong, i use CComboBox

perhaps my application crashes because its extrapolates from CListView :////
 
I do not have any idea why it crashes. If you want you can dowload the program I've made. It works fine on my
computer. When I use CComboBox it works, but when I use
CComboBoxEx it does not.

The site is:
web.ss.pub.ro/~tinta


Liviu.
 
Hello massur,

Please make it a zip archive, this rar archive seems to be corrupted. I will look at it.

Liviu.
 
Hello massur,

I've looked at your code.
In function
Code:
CUsefulSplitterWnd::ReplaceView[\code] you destroy the View that you want replaced. But you do not remove it from your document class. So, when you iterate through the document's views you got the one you just destroyed. This is the source of your error.

Now, to remedy the situation I see two possibilities:

1) the first, and the most easy one, is to memorize all the data in the view before you destroy it, in the document class. When you recreate the view, you just take the informations from the document class and remake them. This is the solution that I've also implemented for you. It has a small problem, because you are using a static splitter. When you recreate the view, the controls that are on the dialog correspondent to it are not created (if you are curious just look at the [code]m_hWnd[\code] member of the ComboBox to see that it is NULL). So you must construct them dynamically in code using the [code]Create[\code] member (you have there an example).

See the MSDN documentation for [code]CSplitterWnd::CreateView[\code].
It says at Remarks:
"Call this member function to create the panes for a static splitter window. All panes of a static splitter window must be created before the framework displays the splitter."

In your case the splitter is already displayed when you call it. That's why it behaves strangely, I think.


2) the second one is to rethink the design of the application and its GUI(you may use a tab control, I do not know)

You can FTP the code from:

earth.prohosting.com
user: tintaliv
password: pinequ01

The file is massur.zip

Hope it helps,
Liviu.

P.S. I do not use the account, I've just created it because the server where I've put the code last time it's down :-)
 
Hi thanks, this is very complex so i will just include this when its really necesarry (or how its written ;)) :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top