为什么我的单选按钮不起作用?

发布于 2024-07-22 07:39:35 字数 7009 浏览 3 评论 0原文

我正在 Visual C++ 2005 中开发 MFC 对话框应用程序。我的单选按钮是 m_Small、m_Medium 和 m_Large。 它们都没有在我的 m_Summary 编辑框中显示应有的内容。 可能出什么问题了?

这是我的代码。

    // Pizza_ProgramDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Pizza_Program.h"
#include "Pizza_ProgramDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    enum { IDD = IDD_ABOUTBOX };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CPizza_ProgramDlg dialog




CPizza_ProgramDlg::CPizza_ProgramDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CPizza_ProgramDlg::IDD, pParent)
    , m_Name(_T(""))
    , m_Address(_T(""))
    , m_Phone(_T(""))
    , m_Summary(_T(""))
    , m_Extra(FALSE)
    , m_Pepperoni(FALSE)
    , m_Sausage(FALSE)
    , m_Peppers(FALSE)
    , m_Mushrooms(FALSE)
    , m_Onions(FALSE)
    , m_Eatin(0)
    , m_Medium(0)
    , m_Large(0)
    , m_Takeout(0)
    , m_Delivery(0)
    , m_Small(0)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPizza_ProgramDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT_NAME, m_Name);
    DDV_MaxChars(pDX, m_Name, 1000);
    DDX_Text(pDX, IDC_EDIT_ADDRESS, m_Address);
    DDV_MaxChars(pDX, m_Address, 1000);
    DDX_Text(pDX, IDC_EDIT_PHONE, m_Phone);
    DDV_MaxChars(pDX, m_Phone, 1000);
    DDX_Text(pDX, IDC_EDIT_SUMMARY, m_Summary);
    DDV_MaxChars(pDX, m_Summary, 1000);
    DDX_Check(pDX, IDC_CHECK_EXTRA, m_Extra);
    DDX_Check(pDX, IDC_CHECK_PEPPERONI, m_Pepperoni);
    DDX_Check(pDX, IDC_CHECK_SAUSAGE, m_Sausage);
    DDX_Check(pDX, IDC_CHECK_PEPPERS, m_Peppers);
    DDX_Check(pDX, IDC_CHECK_MUSHROOMS, m_Mushrooms);
    DDX_Check(pDX, IDC_CHECK_ONIONS, m_Onions);
}

BEGIN_MESSAGE_MAP(CPizza_ProgramDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_EN_CHANGE(IDC_EDIT_SUMMARY, &CPizza_ProgramDlg::OnEnChangeEditSummary)
    ON_BN_CLICKED(IDOK, &CPizza_ProgramDlg::OnBnClickedOk)
    ON_BN_CLICKED(IDC_BUTTON_PROCESS, &CPizza_ProgramDlg::OnBnClickedButtonProcess)
END_MESSAGE_MAP()


// CPizza_ProgramDlg message handlers

BOOL CPizza_ProgramDlg::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 CPizza_ProgramDlg::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 CPizza_ProgramDlg::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 CPizza_ProgramDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}


void CPizza_ProgramDlg::OnEnChangeEditSummary()
{

    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

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

void CPizza_ProgramDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    OnOK();
}

void CPizza_ProgramDlg::OnBnClickedButtonProcess()
{
    // TODO: Add your control notification handler code here

    UpdateData(TRUE);
    m_Summary += "Customer's Name: ";
    m_Summary += m_Name;
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Customer's Address: ";
    m_Summary += m_Address;
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Customer's Phone Number: ";
    m_Summary += m_Phone;


   if (m_Small==TRUE)
{
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Small Pizza $5.00";
}
   else if (m_Medium==TRUE)
{
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Medium Pizza $10.00";
}
   else if (m_Large==TRUE)
   { 
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Large Pizza $15.00";
   }

   if (m_Extra==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Extra Cheese +$1.00";
   }

      if (m_Pepperoni==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Pepperoni +$1.00";
   }

         if (m_Sausage==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Sausage +$1.00";
   }

        if (m_Peppers==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Peppers +$1.00";
   }

           if (m_Mushrooms==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Mushrooms +$1.00";
   }
              if (m_Onions==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Onions +$1.00";
   }

    UpdateData(FALSE);
}

I'm working on an MFC dialog application in Visual C++ 2005. My radio buttons are m_Small, m_Medium, and m_Large. None of them display what they are supposed to in my m_Summary edit box. What could be wrong?

Here's my code.

    // Pizza_ProgramDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Pizza_Program.h"
#include "Pizza_ProgramDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    enum { IDD = IDD_ABOUTBOX };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CPizza_ProgramDlg dialog




CPizza_ProgramDlg::CPizza_ProgramDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CPizza_ProgramDlg::IDD, pParent)
    , m_Name(_T(""))
    , m_Address(_T(""))
    , m_Phone(_T(""))
    , m_Summary(_T(""))
    , m_Extra(FALSE)
    , m_Pepperoni(FALSE)
    , m_Sausage(FALSE)
    , m_Peppers(FALSE)
    , m_Mushrooms(FALSE)
    , m_Onions(FALSE)
    , m_Eatin(0)
    , m_Medium(0)
    , m_Large(0)
    , m_Takeout(0)
    , m_Delivery(0)
    , m_Small(0)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPizza_ProgramDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT_NAME, m_Name);
    DDV_MaxChars(pDX, m_Name, 1000);
    DDX_Text(pDX, IDC_EDIT_ADDRESS, m_Address);
    DDV_MaxChars(pDX, m_Address, 1000);
    DDX_Text(pDX, IDC_EDIT_PHONE, m_Phone);
    DDV_MaxChars(pDX, m_Phone, 1000);
    DDX_Text(pDX, IDC_EDIT_SUMMARY, m_Summary);
    DDV_MaxChars(pDX, m_Summary, 1000);
    DDX_Check(pDX, IDC_CHECK_EXTRA, m_Extra);
    DDX_Check(pDX, IDC_CHECK_PEPPERONI, m_Pepperoni);
    DDX_Check(pDX, IDC_CHECK_SAUSAGE, m_Sausage);
    DDX_Check(pDX, IDC_CHECK_PEPPERS, m_Peppers);
    DDX_Check(pDX, IDC_CHECK_MUSHROOMS, m_Mushrooms);
    DDX_Check(pDX, IDC_CHECK_ONIONS, m_Onions);
}

BEGIN_MESSAGE_MAP(CPizza_ProgramDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_EN_CHANGE(IDC_EDIT_SUMMARY, &CPizza_ProgramDlg::OnEnChangeEditSummary)
    ON_BN_CLICKED(IDOK, &CPizza_ProgramDlg::OnBnClickedOk)
    ON_BN_CLICKED(IDC_BUTTON_PROCESS, &CPizza_ProgramDlg::OnBnClickedButtonProcess)
END_MESSAGE_MAP()


// CPizza_ProgramDlg message handlers

BOOL CPizza_ProgramDlg::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 CPizza_ProgramDlg::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 CPizza_ProgramDlg::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 CPizza_ProgramDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}


void CPizza_ProgramDlg::OnEnChangeEditSummary()
{

    // TODO:  If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

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

void CPizza_ProgramDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    OnOK();
}

void CPizza_ProgramDlg::OnBnClickedButtonProcess()
{
    // TODO: Add your control notification handler code here

    UpdateData(TRUE);
    m_Summary += "Customer's Name: ";
    m_Summary += m_Name;
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Customer's Address: ";
    m_Summary += m_Address;
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Customer's Phone Number: ";
    m_Summary += m_Phone;


   if (m_Small==TRUE)
{
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Small Pizza $5.00";
}
   else if (m_Medium==TRUE)
{
    m_Summary += char(13);
    m_Summary += char(10);
    m_Summary += "Medium Pizza $10.00";
}
   else if (m_Large==TRUE)
   { 
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Large Pizza $15.00";
   }

   if (m_Extra==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Extra Cheese +$1.00";
   }

      if (m_Pepperoni==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Pepperoni +$1.00";
   }

         if (m_Sausage==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Sausage +$1.00";
   }

        if (m_Peppers==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Peppers +$1.00";
   }

           if (m_Mushrooms==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Mushrooms +$1.00";
   }
              if (m_Onions==TRUE)
   {
       m_Summary += char(13);
       m_Summary += char(10);
       m_Summary += "Onions +$1.00";
   }

    UpdateData(FALSE);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-07-29 07:39:35

您需要添加对 DDX_Radio 在类的 DoDataExchange 函数中。

为此,您应该将 m_Smallm_Mediumm_Large 成员变量替换为单个整数:

int m_Size;

假设第一个单选按钮的 ID是 IDC_SMALL,那么 DDX_Radio 调用将为:(

DDX_Radio(pDX, IDC_SMALL, m_Size);

不幸的是,您不能将 m_Size 声明为 enum因为 DDX_Radio 需要一个 int。)

此外,如 Aiden Ryan 正确指出,在 Visual Studio 资源编辑器中确保单选按钮按对话框选项卡顺序按顺序出现。 第一个单选按钮的 Group 属性应设置为 True,其他单选按钮的 Group 属性应设置为 False。

You need to add a call to DDX_Radio in your class's DoDataExchange function.

For this to work you should replace your m_Small, m_Medium, and m_Large member variables with a single integer:

int m_Size;

Assuming the ID of the first radio button is IDC_SMALL, then the DDX_Radio call would be:

DDX_Radio(pDX, IDC_SMALL, m_Size);

(Unfortunately, you can't declare m_Size as an enum because DDX_Radio expects an int.)

Also, as Aiden Ryan correctly points out, in the Visual Studio resource editor ensure that the radio buttons appear sequentially in the dialog tab order. The first radio button should have the Group property set to True and the other radio buttons should have the Group property set to False.

小嗲 2024-07-29 07:39:35

单选按钮往往用于具有两个以上值的变量,因此不要使用三个变量:

BOOL m_Small;
BOOL m_Medium;
BOOL m_Large;

您将拥有一个枚举:

enum PizzaSize {Small, Medium, Large};

和此类型的变量:

PizzaSize m_Size;

单选按钮将设置此变量。

然后你的代码将变成:

switch (m_Size)
{
    case Small:
        m_Summary += char(13);
        m_Summary += char(10);
        m_Summary += "Small Pizza $5.00";
        break;
    case Medium:
        m_Summary += char(13);
        m_Summary += char(10);
        m_Summary += "Medium Pizza $10.00";
    case Large:
        m_Summary += char(13);
        m_Summary += char(10);
        m_Summary += "Large Pizza $15.00";
}

自从我完成 C++ & 以来已经有一段时间了。 MFC 因此将其视为起点而不是最终代码;)

Radio buttons tend to be used for variables that have more that two values, so rather than having three variables:

BOOL m_Small;
BOOL m_Medium;
BOOL m_Large;

You'd have an enum:

enum PizzaSize {Small, Medium, Large};

and a variable of this type:

PizzaSize m_Size;

The radio buttons would set this variable.

Your code would then become:

switch (m_Size)
{
    case Small:
        m_Summary += char(13);
        m_Summary += char(10);
        m_Summary += "Small Pizza $5.00";
        break;
    case Medium:
        m_Summary += char(13);
        m_Summary += char(10);
        m_Summary += "Medium Pizza $10.00";
    case Large:
        m_Summary += char(13);
        m_Summary += char(10);
        m_Summary += "Large Pizza $15.00";
}

It's a while since I've done C++ & MFC so treat this as a starting point rather than the definitive code ;)

宁愿没拥抱 2024-07-29 07:39:35

ChrisF的回答非常好,是在MFC中做单选按钮的正确方法。

一个非常重要的注意事项:确保单选按钮按照对话框选项卡顺序按枚举顺序排列。

ChrisF's answer is excellent, the right way to do radio buttons in MFC.

One very important note: be sure that the radio buttons are sequentially in the dialog tab order as in the enum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文