Hi,

The above code generates a window and it has a few buttons on it.That is working fine.
But however the part of the code where class "FileDialog" is defined and the function
"myButton5_Click" is used, it does not seem to work.

The output should be such that - When the browse(myButton5) is clicked, a dialog should be opened.

It would be great if anyone can let me know what is wrong with that part of the code as the code was working before I added that class.

Thanks in advance

#include <afxwin.h>
#include <afxdlgs.h>

#include "resource.h"
#using <System.dll>
#using <System.Windows.Forms.dll>


using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Windows::Forms;




class MFC_Window :public CFrameWnd
{
		 CMenu menu1;
		 CButton myButton1;
		 CButton myButton2;
		 CButton myButton3;
		 CButton myButton4;
		 CButton myButton5;
		 CScrollBar Scrollbar;
		 
		 
		 
public:
    MFC_Window()
    {
	
         Create(NULL,"RTBI Billing");
		 menu1.LoadMenu(IDR_MENU1);
		 SetMenu(&menu1);
		 myButton1.Create("BridgeIP",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(0,10,100,60),this,1);
		 myButton2.Create("PortNumber",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(0,70,100,120),this,2);
		 myButton3.Create("RTBI Version",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(0,130,100,180),this,3);
		 myButton4.Create("Transmit",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(0,190,100,240),this,4);
		 myButton5.Create("Browse",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(0,250,100,300),this,5);
		 Scrollbar.Create( SBS_HORZ | SBS_SIZEBOXTOPLEFTALIGN | WS_CHILD | WS_VISIBLE,CRect(0,350,800,350),this,100);
		
		 
         
    }
	void OnFileNew();
	void OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI);
	void myButton5_Click( Object^ sender , System::EventArgs^ e );
	DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP( MFC_Window, CFrameWnd)
	ON_COMMAND(IDM_FILE_NEW,OnFileNew)
	ON_WM_GETMINMAXINFO()
	 
END_MESSAGE_MAP()


void MFC_Window::OnFileNew()
{
	//MessageBox("Clicked File->New");
}

void MFC_Window::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
lpMMI->ptMinTrackSize.x = 500;  //width of the window
lpMMI->ptMaxTrackSize.x = 800;
lpMMI->ptMinTrackSize.y = 500; //height of the window
lpMMI->ptMaxTrackSize.y = 800;

CFrameWnd::OnGetMinMaxInfo(lpMMI);

}


public ref class FileDialog abstract : public CommonDialog
{
private:
   void myButton5_Click( Object^ sender , System::EventArgs^ e )
   {
      Stream^ myStream;
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

      openFileDialog1->InitialDirectory = "c:\\";
      openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      openFileDialog1->FilterIndex = 2;
      openFileDialog1->RestoreDirectory = true;

	  if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
      {
         if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
         {
            // Insert code to read the stream here.
            myStream->Close();
         }
      }
   }
};



class MyApp :public CWinApp
{
	MFC_Window *wnd;
public:
   BOOL InitInstance()
   {
        wnd = new MFC_Window();
        m_pMainWnd = wnd;
        m_pMainWnd->ShowWindow(1);
        return 1;
		
     }
};

MyApp theApp;

You have no ON_BN_CLICKED in your Message map. Add an ON_BN_CLICKED(your button ID, function to call)

Hi,

After adding the "ON_BN_CLICKED(IDC_BUTTON1,&MFC_Window::Button1_click)", I get the error:

error C2440: 'static_cast' : cannot convert from 'void (__clrcall MFC_Window::* )(System::Object ^,System::EventArgs ^)' to 'AFX_PMSG'Cast from base to derived requires dynamic_cast or static_cast

#include <afxwin.h>
#include <afxdlgs.h>
#include <windows.h>
#include "resource.h"

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Web.dll>
#using <mscorlib.dll>

using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Windows::Forms;
using namespace System::Web::UI::WebControls;
using namespace Microsoft::Win32;


const int IDC_BUTTON1 = 101;
afx_msg void Button1_click(Object^ sender , System::EventArgs^ e);


class MFC_Window :public CFrameWnd
{
		 CMenu menu1;
		 CButton Button1;
		
		 
				 
public:
    MFC_Window()
    {
	
         Create(NULL,"RTBI Billing");
		 menu1.LoadMenu(IDR_MENU1);
		 SetMenu(&menu1);
		 Button1.Create("BridgeIP",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(0,10,100,60),this,IDC_BUTTON1);	
		 //this->Button1.click += gcnew EventHandler(this, &MFC_Window::Button1_click);

	}
 
	void OnFileNew();
	void OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI);
	void Button1_click( Object^ sender , System::EventArgs^ e);
	DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP( MFC_Window, CFrameWnd)
	ON_COMMAND(IDM_FILE_NEW,OnFileNew)
	ON_WM_GETMINMAXINFO()
	ON_BN_CLICKED(IDC_BUTTON1,&MFC_Window::Button1_click)
	 
END_MESSAGE_MAP()


void MFC_Window::OnFileNew()
{	
}

void MFC_Window::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
lpMMI->ptMinTrackSize.x = 500;  //width of the window
lpMMI->ptMaxTrackSize.x = 800;
lpMMI->ptMinTrackSize.y = 500; //height of the window
lpMMI->ptMaxTrackSize.y = 800;

CFrameWnd::OnGetMinMaxInfo(lpMMI);

}


void MFC_Window::Button1_click( System::Object^ sender , System::EventArgs^ e)
   {
	  
      Stream^ myStream;
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

      openFileDialog1->InitialDirectory = "c:\\";
      openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      openFileDialog1->FilterIndex = 2;
 
	  if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
      {
         
		  if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
         {
            // Insert code to read the stream here.
            myStream->Close();
         }
		 
      }

   }


class MyApp :public CWinApp
{
	MFC_Window *wnd;
public:
   BOOL InitInstance()
   {
        wnd = new MFC_Window();
        m_pMainWnd = wnd;
        m_pMainWnd->ShowWindow(1);
        return 1;
		
     }
};

MyApp theApp;

Not sure how to solve the type cast error.It would be great if you could help me in using the static_cast.

FYI

I am using Visual studio 2008 with .Net Framework Version 3.5 SP1

Levy,

Could you please have a look at the next post i posted for MFC and VC++ problem.
Your feedback/suggestion would be of great help to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.