Hi everyone I'm building a program that manipulates with Singly Linked List which has a one data member ( int ), and there are many functions like: add first, delete, ... The problem is: How can I draw the list ? ( It is okay if each node represented by a square and inside it there is the int data ). These are some pics of my program : The main window: Resource View: Files View: Class View: ( I created a class for each dialog, is that ok ? ) This the source code for "Singly Linked List intDlg": PHP: // Singly Linked List intDlg.cpp : implementation file // #include "stdafx.h" #include "Singly Linked List int.h" #include "Singly Linked List intDlg.h" //$$$$$$$$$$$$$$$$$$$$ #include "AddFirst.h" #include "Delete.h" #include "Size.h" #include "Print.h" #include "About.h" //$$$$$$$$$$$$$$$$$$$$ #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// //$$$$$$$$$$$$$$$$$$$$$$$ Node Class $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// //$$$$$$$$$$$$$$$$$$$ AddFirst addFirstDLG; Delete deleteDLG; Size sizeDLG; Print printDLG; About aboutDLG; //$$$$$$$$$$$$$$$$$$$ class Node { public: Node( int ); ~Node(); int DATA; Node* next; }; Node::Node( int nd ) { DATA = nd; next = NULL; } Node::~Node() { next = NULL; } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// //$$$$$$$$$$$$$$$$$$$$$$$ Node Class $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// /********************* **********************/ Node* firstPtr = NULL; int sizeOfList; /********************** *********************/ class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSinglyLinkedListintDlg dialog CSinglyLinkedListintDlg::CSinglyLinkedListintDlg(CWnd* pParent /*=NULL*/) : CDialog(CSinglyLinkedListintDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSinglyLinkedListintDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSinglyLinkedListintDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSinglyLinkedListintDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSinglyLinkedListintDlg, CDialog) //{{AFX_MSG_MAP(CSinglyLinkedListintDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_EXIT, OnExit) ON_BN_CLICKED(IDC_DELETE, OnDelete) ON_BN_CLICKED(IDC_ADD_AFTER, OnAddAfter) ON_BN_CLICKED(IDC_ADD_BEFORE, OnAddBefore) ON_BN_CLICKED(IDC_SIZE, OnSize) ON_BN_CLICKED(IDC_IS_EMPTY, OnIsEmpty) ON_BN_CLICKED(IDC_SWAP, OnSwap) ON_BN_CLICKED(IDC_IN_LIST, OnInList) ON_BN_CLICKED(IDC_GET_INDEX, OnGetIndex) ON_BN_CLICKED(IDC_PRINT, OnPrint) ON_BN_CLICKED(IDC_ADD_FIRST, OnAddFirst) ON_BN_CLICKED(IDC_ABOUT, OnAbout) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSinglyLinkedListintDlg message handlers BOOL CSinglyLinkedListintDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CSinglyLinkedListintDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CSinglyLinkedListintDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSinglyLinkedListintDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSinglyLinkedListintDlg::OnExit() { // TODO: Add your control notification handler code here MessageBox("Thank you for using our program","Singly Linked List", MB_ICONINFORMATION ); exit(1); } void CSinglyLinkedListintDlg::OnAddFirst() { // TODO: Add your control notification handler code here addFirstDLG.DoModal(); int newData = addFirstDLG.m_data; Node* newNode = new Node( newData ); if ( firstPtr != NULL ) { newNode->next=firstPtr; firstPtr=newNode; } else firstPtr = newNode; } void CSinglyLinkedListintDlg::OnDelete() { // TODO: Add your control notification handler code here Node* currentPtr = firstPtr; Node* prevPtr = NULL; bool deleted = false; deleteDLG.DoModal(); int sd = deleteDLG.m_toDelete; while( currentPtr != NULL ) { if ( currentPtr->DATA == sd ) { if ( currentPtr == firstPtr ) { firstPtr = firstPtr->next; delete currentPtr; MessageBox("The node is deleted", "Singly Linked List", MB_ICONINFORMATION); deleted = true; break; } if ( currentPtr->next == NULL ) { prevPtr->next = NULL; delete currentPtr; MessageBox("The node is deleted", "Singly Linked List", MB_ICONINFORMATION); deleted = true; break; } prevPtr->next = currentPtr->next; delete currentPtr; MessageBox("The node is deleted", "Singly Linked List", MB_ICONINFORMATION); deleted = true; break; } prevPtr = currentPtr; currentPtr = currentPtr ->next; } if ( deleted == false ) MessageBox("The node does not exist or the list is empty", "Singly Linked List", MB_ICONEXCLAMATION ); } void CSinglyLinkedListintDlg::OnAddAfter() { // TODO: Add your control notification handler code here } void CSinglyLinkedListintDlg::OnAddBefore() { // TODO: Add your control notification handler code here } void CSinglyLinkedListintDlg::OnSize() { // TODO: Add your control notification handler code here Node* currentPtr = firstPtr; int count=0; while( currentPtr != NULL ) { count++; currentPtr = currentPtr -> next; } sizeDLG.m_size = count; sizeOfList = count; sizeDLG.DoModal(); } void CSinglyLinkedListintDlg::OnIsEmpty() { // TODO: Add your control notification handler code here if ( firstPtr == NULL ) MessageBox("The list is empty"); else if ( firstPtr != NULL ) MessageBox("The list is not empty"); } void CSinglyLinkedListintDlg::OnSwap() { // TODO: Add your control notification handler code here } void CSinglyLinkedListintDlg::OnInList() { // TODO: Add your control notification handler code here } void CSinglyLinkedListintDlg::OnGetIndex() { // TODO: Add your control notification handler code here } void CSinglyLinkedListintDlg::OnPrint() { // TODO: Add your control notification handler code here Node* currentPtr = firstPtr; printDLG.DoModal(); while( currentPtr != NULL ) { /* . . . */ //cout << currentPtr->DATA << endl; currentPtr = currentPtr->next; } } void CSinglyLinkedListintDlg::OnAbout() { // TODO: Add your control notification handler code here aboutDLG.DoModal(); } So, what I want is: How can I create the print function so the user can see the list ?
Have a text box where you output everything. Also I would suggest you don't give the screen shots of the resource editor or the dialog as that does not help or at least to me.
First of all, thank you Mr.Shabbir for being with us and for the quick reply secondly, I know how to connect an integer data member ( say: int m_data ) with the test box, but as you know there are many nodes in the list. I'm just saying that if you could please give me an example? and can I connect an array with one text box ? &
Just for being clear, if I click on the print button, then a dialog is appears with one big text box and the nodes are listed in it like this: 10 -> 2 -> 3 -> 95 So, how can I do that ?
I didn't understand any word from your reply Mr.shabbir because I'm new at VC++ Could you please type the code that do this: Have the string concatenate and then do the SetWindowText
Its nothing related to VC++. It something that you do in C or C++. You will output all the thing using printf and instead of doing that have them concatenate into a string then set that text to the text box text using the API SetWindowText
In C++ also you can append the strings. Just append the complete string you wish to show to the user when print button is clicked and then display that string in the text box. Something like Code: string szTemp; szTemp = First Node Content; szTemp += Second Node Content; TextBox.SetWindowText(szTemp);
I wrote something like your code : PHP: Node* currentPtr = firstPtr; CString contents(""); if ( currentPtr != NULL ) { while( currentPtr -> next != NULL ) { CString temp(""); temp.Format("%i", currentPtr->DATA ); contents += temp; contents += " -> "; currentPtr = currentPtr->next; } contents =+ currentPtr -> DATA; printDLG.m_content = contents; UpdateData(TRUE); printDLG.DoModal(); } But the final result ( when I execute the program ) like this: ـ or ؟؟
ًWoOoOoOoW !! I want to dance now !!! the program now is 100% complete, I knew the problem with the previous code: PHP: // TODO: Add your control notification handler code here Node* currentPtr = firstPtr; CString contents(""); if ( currentPtr != NULL ) { while( currentPtr -> next != NULL ) { CString temp(""); temp.Format("%i", currentPtr->DATA ); contents += temp; contents += " -> "; currentPtr = currentPtr->next; } CString temp(""); temp.Format("%i", currentPtr -> DATA ); contents += temp; printDLG.m_content = contents; UpdateData(TRUE); printDLG.DoModal(); } else MessageBox("The list is empty" );