0
votes

please, I've spent all day trying to figure this out but can't. I have a class (artist1) created from a dialog box with some edit boxes. I want to get the data typed in the edit boxes and save them to variables i made public in the class. But don't know why it doesn't work. PS am new to mfc programming. Thanks here is my artist class

void artist1::OnBnClickedButton1()
{
    //artist1 AA=*art1; 

    CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT1); 
    pEdit1->GetWindowText(Name1);
    nn=new CString;
    *nn=Name1;
    CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT2); 
    pEdit2->GetWindowText(Age1);
    n2=new CString;
    *n2=Age1;
    CEdit* pEdit3 = (CEdit*)GetDlgItem(IDC_EDIT3); 
    pEdit3->GetWindowText(Nationality1);
    n3=new CString;
    *n3=Nationality1;
    CEdit* pEdit4 = (CEdit*)GetDlgItem(IDC_EDIT4); 
    pEdit4->GetWindowText(Group1);
    n4=new CString;
    *n4=Group1;
    CEdit* pEdit5 = (CEdit*)GetDlgItem(IDC_EDIT5); 
    pEdit5->GetWindowText(num_of_albums1);
    n5=new CString;
    *n5=num_of_albums1;
    SH(Name1,Age1,Nationality1,Group1,num_of_albums1);
    art1=this;
//  memcpy(art1,this,sizeof(this));

    //Name_box.SetWindowText(g);
    //AfxMessageBox( Age );
    //AfxMessageBox( Nationality );
    // TODO: Add your control notification handler code here

}

/*bool artist1::SH()
{
    if(NoShow==false)return true;
    else return false;
}*/
void artist1::OnBnClickedButton2()
{
    //Cooplab1View vm;
    NoShow=false;
    nvalidateRect(NULL,NULL);
    EndDialog(IDD_FORMVIEW);

    // TODO: Add your control notification handler code here
}

and here is the class artist header

class artist1 : public CDialogEx
{
    //DECLARE_DYNAMIC(artist1)

public:
    artist1(CWnd* pParent = NULL);   // standard constructor
    virtual ~artist1();
    bool NoShow;
    bool *address;

    CString Albums[5];

    void OnInsertArtist(artist1 &at);
// Dialog Data
    enum { IDD = IDD_FORMVIEW };
private:
    CString Nm;
    CString Ag;
    CString Nation;
    CString group;
    CString No_of_A;
    CString *nnn;

public:
    // artist1* GetTreeObj();
    //virtual CString ShowDetails(CDC* pDC);
    void SH(CString a,CString b,CString c,CString d,CString e)
{
    Name=a;
    Age=b;
    Nationality=c;
    Group=d;
    num_of_albums=e;
}

protected:
    CString Name,Age,Nationality,Group,num_of_albums;
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    CString Name1,Age1,Nationality1,Group1,num_of_albums1;
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnBnClickedButton1();
    virtual CString ShowDetails(CDC* pDC,artist1 & at1);

    afx_msg void OnBnClickedButton2();
};

and i call the class from the CView class cpp file

void Cooplab1View::OnDraw(CDC* pDC)
{
    artist1 art;
    artist1 A1;
    Cooplab1Doc* pDoc = GetDocument();
    //ASSERT_VALID(pDoc);
    //if (!pDoc)
    //  return;



    if (noShow)
    {
        art.OnInsertArtist(art);
        //art.OnBnClickedButton1();
        //art=&obj;
    //  art.GetTreeObj();
        art.ShowDetails(pDC,art);
    }

    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
    // TODO: add draw code for native data here
}


void Cooplab1View::OnInsertArtist1()
{
    noShow=true;
    InvalidateRect(NULL,NULL);
    //UpdateWindow();
}
4

4 Answers

0
votes
  1. You should create variables (right click->Add variables in vs2010) matching your "edit boxes" and check their values.
    Don't do "GetDlgItem" and not GetWindowText.
  2. You should addUpdateData(TRUE) at the first line of OnBnClickedButton1
  3. Good Luck !
0
votes

Your class is quite messy, but if you just want to extract user entry text from the dialog items, it is actually quite simple.

void artist1::OnBnClickedButton1()
{
    // Since variables Name,Age,Nationality,Group,num_of_albums are all CString items
    // declare as member variables in the class, 
    // you can just retrieve user entry from the dialog directly into them
    // without calling the "SH(Name1,Age1,Nationality1,Group1,num_of_albums1);"
    GetDlgItemText(IDC_EDIT1, Name);
    GetDlgItemText(IDC_EDIT2, Age);
    GetDlgItemText(IDC_EDIT3, Nationality);
    GetDlgItemText(IDC_EDIT4, Group);
    GetDlgItemText(IDC_EDIT5, num_of_albums);
}
0
votes

If you are entering a value to the edit control, you should call updatedata( FALSE ). If you want to store the value into a variable then call updatedata(TRUE). That's it.

0
votes

I finally solved the problem. I declared some global variables, assigned the values inputted in the edit boxes to them in OnBnClickedButton1() using GetDlgItemText then in OnBnClickedButton2() i assigned the global variables to the variables in my class ie Name, Age etc.