dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38

Thread: Array of Variables?

  1. #1
    Join Date
    Dec 2011
    Posts
    21

    Array of Variables?

    Hi, I'm somewhat new to MFC and C++. I have Java experience, but that's about it.

    Here is a short breakdown what I want to do and any help would be appreciated.

    I'm making a dialog-based MFC Application that essentially countdowns different timers for their respective variables.

    I created an Edit Control box and assigned a variable name to it. (Call it CString m_var1)
    I need to duplicate this Edit Control box about 20 different times.
    I have a CArray of objects that contain specific data to each variable.

    I want to cycle through the objects and update their respective variables on the control box.

    Something like this....

    for (int i = 0; i < Array.GetCount(); i++) {

    // .... Do something with the first object in the array
    // .... Update "var1" with contents of that specific object
    }

    Thanks for your help!!

    -Jason Simon

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Array of Variables?

    m_var1 = new_value;
    UpdateData(FALSE);

    It is not clear how you want to show the whole array in single editbox. Result of your loop shows only last value.

  3. #3
    Join Date
    Dec 2011
    Posts
    21

    Re: Array of Variables?

    There will be about 20 different edit boxes.

    Edit box 1 - var1
    Edit box 2 - var2
    .
    .
    .
    etc

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Array of Variables?

    If you know at compile time how many edit boxes you want, you can use a static array. Otherwise, you need to create a vector of pointers to CEditBox and create each one individually. This is because CObject is not copyable.
    Code:
    std::vector<CEditBox*> editBoxes;
    //...
    editBoxes.resize(20, NULL);
    for (size_t i = 0; i < 20; ++i) {
        editBoxes[i] = new CEditBox();
        // create
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,551

    Re: Array of Variables?

    Create a control variable for each control.
    Add the address of each control to an array.
    Iterate the array.

    Code:
    CObArray m_Edits;
    m_Edits.Add(&m_Edit1);
    m_Edits.Add(&m_Edit2);
    ...

  6. #6
    Join Date
    Dec 2011
    Posts
    21

    Re: Array of Variables?

    It will probably be easier if I show you my code...

    Object.h

    Code:
    #pragma once
    
    // Object command target
    
    class Object : public CObject
    {
    public:
    	Object(int,int);
    	virtual ~Object();
    	void setHours(int);
    	void setMinutes(int);
    	int getHours();
    	int getMinutes();
    private:
    	int hours;
    	int minutes;
    };
    Object.cpp

    Code:
    // Object.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "QATest.h"
    #include "Object.h"
    
    
    // Object
    
    Object::Object(int a, int b)
    {
    	hours = a;
    	minutes = b;
    }
    
    Object::~Object()
    {
    }
    
    
    // Object member functions
    
    void Object::setHours(int h) {hours = h;}
    void Object::setMinutes(int m) {minutes = m;}
    int Object::getHours() {return hours;}
    int Object::getMinutes() {return minutes;}
    QATestDlg.h

    Code:
    // QATestDlg.h : header file
    //
    
    #pragma once
    
    
    // CQATestDlg dialog
    class CQATestDlg : public CDialog
    {
    // Construction
    public:
    	CQATestDlg(CWnd* pParent = NULL);	// standard constructor
    
    // Dialog Data
    	enum { IDD = IDD_QATEST_DIALOG };
    
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
    
    
    // Implementation
    protected:
    	HICON m_hIcon;
    
    	// Generated message map functions
    	virtual BOOL OnInitDialog();
    	afx_msg void OnPaint();
    	afx_msg HCURSOR OnQueryDragIcon();
    	DECLARE_MESSAGE_MAP()
    
    	CArray<Object*, Object*> m_Object;
    public:
    	CString m_Hours1;
    	CString m_Hours2;
    	CString m_Minutes1;
    	CString m_Minutes2;
    	afx_msg void OnTimer(UINT_PTR nIDEvent);
    };
    QATestDlg.cpp

    Code:
    // QATestDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "QATest.h"
    #include "QATestDlg.h"
    #include "Object.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // CQATestDlg dialog
    
    
    
    
    CQATestDlg::CQATestDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CQATestDlg::IDD, pParent)
    	, m_Hours1(_T("24"))
    	, m_Hours2(_T("24"))
    	, m_Minutes1(_T("00"))
    	, m_Minutes2(_T("00"))
    {
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CQATestDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	DDX_Text(pDX, IDC_HOURS1, m_Hours1);
    	DDX_Text(pDX, IDC_HOURS2, m_Hours2);
    	DDX_Text(pDX, IDC_MINUTES1, m_Minutes1);
    	DDX_Text(pDX, IDC_MINUTES2, m_Minutes2);
    }
    
    BEGIN_MESSAGE_MAP(CQATestDlg, CDialog)
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	//}}AFX_MSG_MAP
    	ON_WM_TIMER()
    END_MESSAGE_MAP()
    
    
    // CQATestDlg message handlers
    
    BOOL CQATestDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// 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
    	SetTimer(1, 1000, 0);
    	m_Object.Add(new Object(24,60));
    	m_Object.Add(new Object(24,60));
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    // 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 CQATestDlg::OnPaint()
    {
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
    
    		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CQATestDlg::OnQueryDragIcon()
    {
    	return static_cast<HCURSOR>(m_hIcon);
    }
    
    
    void CQATestDlg::OnTimer(UINT_PTR nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
    
    	for (int i =0; i < m_Object.GetCount(); i++)
    	{
    		int help1 = m_Object[i]->getHours();
    		int help2 = m_Object[i]->getMinutes();
    	
    		char strHours[6];
    		char strMins[6];
    		itoa(help1, strHours, 10);
    		itoa(help2, strMins, 10);
    
    		/* This is where I need the help... Object[1] contains the
    			data that I want to populate to m_Hours1 and m_Minutes1
    			on so on.
    
    		this->m_Hours1 = CString(strHours);
    		this->m_Minutes1 = CString(strMins);
    		this->m_Hours2 = CString(strHours);
    		this->m_Minutes2 = CString(strMins);*/
    
    
    		help2--;
    		m_Object[i]->setMinutes(help2);
    	}
    
    	UpdateData(FALSE);
    	CDialog::OnTimer(nIDEvent);
    }
    The OnTimer event is what I'm trying to figure out.

    Thanks!

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,551

    Re: Array of Variables?

    Why not must make m_Hours and m_Minutes a CStringArray?

    I'm pretty sure this is valid code.


    DDX_Text(pDX, IDC_HOURS1, m_Hours[1]);

  8. #8
    Join Date
    Dec 2011
    Posts
    21

    Re: Array of Variables?

    Right now, I have 4 text boxes on my dialog screen. They correspond to m_Hours1, m_Hours2, m_Minutes1, m_Minutes2.

    I tried fiddling around with using a CStringArray, but I could never get the values to update on the screen.

  9. #9
    Join Date
    Dec 2011
    Posts
    21

    Re: Array of Variables?

    Quote Originally Posted by GCDEF View Post
    Why not must make m_Hours and m_Minutes a CStringArray?

    I'm pretty sure this is valid code.


    DDX_Text(pDX, IDC_HOURS1, m_Hours[1]);

    So, I could add this statement into my for loop?

    this->m_Hours[i] = CString(strHours);
    this->m_Minutes[i] = Cstring(strHours);

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: Array of Variables?

    An aside question: why don't you use one single listbox control instead of 20 edit boxes?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,551

    Re: Array of Variables?

    Quote Originally Posted by Jsimon926 View Post
    So, I could add this statement into my for loop?

    this->m_Hours[i] = CString(strHours);
    this->m_Minutes[i] = Cstring(strHours);
    I think so. There's no need to construct the temporary CString though.

    m_Hours[i] = strHours;

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Array of Variables?

    You should name your class something more descriptive than "Object".

  13. #13
    Join Date
    Dec 2011
    Posts
    21

    Re: Array of Variables?

    Quote Originally Posted by ovidiucucu View Post
    An aside question: why don't you use one single listbox control instead of 20 edit boxes?
    Because each edit box has to be seen at all times and has buttons that correspond to it.

    Quote Originally Posted by GCDEF View Post
    I think so. There's no need to construct the temporary CString though.

    m_Hours[i] = strHours;
    Thanks, I'll give it a try.

    Quote Originally Posted by Lindley View Post
    You should name your class something more descriptive than "Object".
    It does have a more descriptive name. The code I upload was a rough skeleton of where my problem was, but thanks for your input.

  14. #14
    Join Date
    Dec 2011
    Posts
    21

    Re: Array of Variables?

    GCDEF,

    When I change my code to:

    Code:
     void CQATestDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	DDX_Text(pDX, IDC_HOURS1, m_Hours[1]);
    	DDX_Text(pDX, IDC_HOURS2, m_Hours[2]);
    	DDX_Text(pDX, IDC_MINUTES1, m_Minutes[1]);
    	DDX_Text(pDX, IDC_MINUTES2, m_Minutes[2]);
    }
    It throws a post compile error... Maybe I'm not initializing the CStringArray correctly.

    I simply added:

    CStringArray m_Hours;
    CStringArray m_Minutes;

    to my header file. I'm not exactly sure where to instantiate it.

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,551

    Re: Array of Variables?

    What's a "post compile" error and what does it say?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)