需要帮助在mfc对话框类中获取变量

时间:2013-03-11 22:45:27

标签: class visual-c++ object mfc

拜托,我花了一整天时间试图解决这个问题,但不能。我有一个类(artist1)从一个带有一些编辑框的对话框创建。我想在编辑框中输入数据,并将它们保存到我在课堂上公开的变量中。但不知道为什么它不起作用。 PS是mfc编程的新手。谢谢 这是我的艺术家班级

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
}

这是类艺术家标题

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();
};

我从CView类cpp文件中调用该类

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 个答案:

答案 0 :(得分:0)

  1. 您应该在“编辑框”中创建变量(右击 - >在vs2010中添加变量)并检查其值。
    不要做“GetDlgItem”而不是GetWindowText。
  2. 您应该在UpdateData(TRUE)
  3. 的第一行添加OnBnClickedButton1
  4. 祝你好运!

答案 1 :(得分:0)

你的课很乱,但如果你只想从对话框中提取用户输入文字,那实际上很简单。

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);
}

答案 2 :(得分:0)

如果要为编辑控件输入值,则应调用updatedata(FALSE)。如果要将值存储到变量中,请调用updatedata(TRUE)。就是这样。

答案 3 :(得分:0)

我终于解决了这个问题。我声明了一些全局变量,使用GetDlgItemText在OnBnClickedButton1()中将编辑框中输入的值分配给它们,然后在OnBnClickedButton2()中将全局变量分配给我的类中的变量,即Name,Age等。