Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tutorial] How to Handle TextBox, Label, ComboBox and CheckBox Events in C++

Tuesday 25 August 2015
|
Read: Completed in minutes

[Tutorial] How to Handle TextBox, Label, ComboBox and CheckBox Events in C++

Learn C++ controls is very easy for newbie, This tutorial will be help you working with TextBox, Label, ComboBox, CheckBox Control.





Now Build your UI same here:

We have
- Two TextBox with below function
==> Enter a Message: Enter any message (ID = IDC_txtEnterAMessage)
==> Run a Program: Some Program running (ID = IDC_cmbRunAProgram, Data = Notepad++;Paint;Word;)

How to Handle TextBox, Label, ComboBox and CheckBox Events in C++






==> Browser: Browser some file path  (ID = IDC_txtPath)

- Six Button with below function
==> Show Message:  Show Message from textbox which you input (ID = IDC_btnShowMessage)
==> Default Message: Show default message (ID = IDC_btnDefaultMessage)
==> Clear Message: Clear All Message (ID = IDC_btnClearMessage)

How to Handle TextBox, Label, ComboBox and CheckBox Events in C++


==> Run Program: Run one program (ID = IDC_btnRunProgram)
==> Browser: Browser one (ID = IDC_btnBrowser)
==> Exit: Exit Program (ID = IDCANCEL)

- Four CheckBox
==> Enable Message Action (ID = IDC_chkEnableMessageAction)
==> Enable Program Action (ID = IDC_chkEnableProgramAction)
==> Show Message Action  (ID = IDC_chkShowMessageAction)
==> Show Program Action (ID = IDC_chkShowProgramAction)

How to Handle TextBox, Label, ComboBox and CheckBox Events in C++




Tips

MessageBox -> Show message on screen
MessageBox(LPCTSTR text, LPCTSTR caption, Uint Type) MessageBox(_T("open word")); MessageBox(_T("nothing"), _T("Learn Tech Tips"));
WinExec -> Run progress

WinExec(LPCTSTR, Unit) 
WinExec("mspaint.exe", SW_SHOW); 
WinExec("C:\\Program Files\\Microsoft Office 2010\\Office14\\WINWORD.EXE", SW_SHOW); 


Using below code to open file dialog

void CDays2_ControlsDlg::OnFileOpen()
{
    TCHAR szFilters[]= _T("All Files (*.*)|*.*||");

    CFileDialog fileDlg(TRUE, _T("*"), _T("*.*"), 
                OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

    // Display the file dialog. When user clicks OK, fileDlg.DoModal()  
    // returns IDOK. 
    if(fileDlg.DoModal() == IDOK)
    {      
 m_txtPath = fileDlg.GetFileTitle(); // don't have ext
 UpdateData(FALSE);      
    }
}


Using GetDlgItem to get Dialog Information

GetDlgItem(int nDlgID)
GetDlgItem(IDC_RUNPROGRAM)




void CDays2_ControlsDlg::GUIProgramChanging(bool flag)
{
    GetDlgItem(IDC_lblRunAProgram)->EnableWindow(flag);
    GetDlgItem(IDC_cmbRunAProgram)->EnableWindow(flag);
    GetDlgItem(IDC_btnRunProgram)->EnableWindow(flag);
}




Full source code Days2_ControlsDlg.cpp files


// Days2_ControlsDlg.cpp : implementation file
/* ---------------------------------------------------------------
 * Developer: huuvi168@gmail.com (VịLH / zidane)
 * Last Modified: 2015-08-08
 * --------------------------------------------------------------- */ 

#include "stdafx.h"
#include "Days2_Controls.h"
#include "Days2_ControlsDlg.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()
public:
 virtual HRESULT accDoDefaultAction(VARIANT varChild);
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CDays2_ControlsDlg dialog




CDays2_ControlsDlg::CDays2_ControlsDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CDays2_ControlsDlg::IDD, pParent)
 , m_strMessage(_T(""))
 , m_strProgToRun(_T(""))
 , m_bEnableMessageAction(FALSE)
 , m_bEnableProgramAction(FALSE)
 , m_bShowMessageAction(FALSE)
 , m_bShowProgramAction(FALSE)
 , m_txtPath(_T(""))
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDays2_ControlsDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 DDX_Text(pDX, IDC_txtEnterAMessage, m_strMessage);
 DDX_CBString(pDX, IDC_cmbRunAProgram, m_strProgToRun);
 DDX_Check(pDX, IDC_chkEnableMessageAction, m_bEnableMessageAction);
 DDX_Check(pDX, IDC_chkEnableProgramAction, m_bEnableProgramAction);
 DDX_Check(pDX, IDC_chkShowMessageAction, m_bShowMessageAction);
 DDX_Check(pDX, IDC_chkShowProgramAction, m_bShowProgramAction);
 DDX_Text(pDX, IDC_txtPath, m_txtPath);
}

BEGIN_MESSAGE_MAP(CDays2_ControlsDlg, CDialog)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 //}}AFX_MSG_MAP 
 ON_BN_CLICKED(IDC_btnShowMessage, &CDays2_ControlsDlg::OnBnClickedbtnshowmessage)
 ON_BN_CLICKED(IDC_btnDefaultMessage, &CDays2_ControlsDlg::OnBnClickedbtndefaultmessage)
 ON_BN_CLICKED(IDC_btnClearMessage, &CDays2_ControlsDlg::OnBnClickedbtnclearmessage)
 ON_BN_CLICKED(IDC_chkEnableMessageAction, &CDays2_ControlsDlg::OnBnClickedchkenablemessageaction)
 ON_BN_CLICKED(IDC_chkShowMessageAction, &CDays2_ControlsDlg::OnBnClickedchkshowmessageaction)
 ON_BN_CLICKED(IDC_chkEnableProgramAction, &CDays2_ControlsDlg::OnBnClickedchkenableprogramaction)
 ON_BN_CLICKED(IDC_btnRunProgram, &CDays2_ControlsDlg::OnBnClickedbtnrunprogram)
 ON_BN_CLICKED(IDC_btnBrowser, &CDays2_ControlsDlg::OnBnClickedbtnbrowser)
END_MESSAGE_MAP()


// CDays2_ControlsDlg message handlers

BOOL CDays2_ControlsDlg::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
 
 // ---------------------- VịLH Code here ----------------------
 m_strMessage = "Place a message here";

 // set all of check boxes to checked
 
 m_bEnableMessageAction = TRUE;
 m_bShowMessageAction = TRUE;
 m_bEnableProgramAction = TRUE;
 m_bShowProgramAction = TRUE;

 // Update the dialog with the values
 // updateData(FALSE): cập nhật thông tin các biến ra ngoài GUI
 // updateData(TRUE): cập nhật thông tin từ GUI gắn vào biến
 
 UpdateData(FALSE);

 // ---------------------- VịLH Code here ----------------------

 return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDays2_ControlsDlg::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 CDays2_ControlsDlg::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 CDays2_ControlsDlg::OnQueryDragIcon()
{
 return static_cast<hcursor>(m_hIcon);
}

void CDays2_ControlsDlg::OnBnClickedbtnshowmessage()
{
 // TODO: Add your control notification handler code here
 // Update the message variable with that the user entered
 UpdateData(TRUE);

 // Display the message for the user
 if (m_strMessage.IsEmpty() == false)
  MessageBox(m_strMessage, _T("Days 2 - Controls"));
 else
  MessageBox(_T("nothing"), _T("Days 2 - Controls"));
}

void CDays2_ControlsDlg::OnBnClickedbtndefaultmessage()
{
 // TODO: Add your control notification handler code here

 m_strMessage = _T("Good luck to you~");
 UpdateData(FALSE);
}



void CDays2_ControlsDlg::OnBnClickedbtnclearmessage()
{
 // TODO: Add your control notification handler code here
 m_strMessage = "";
 UpdateData(FALSE);
}

void CDays2_ControlsDlg::OnBnClickedchkenablemessageaction()
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);

  if(m_bEnableMessageAction == TRUE)
   GUIMessageChanging(TRUE);
  else
   GUIMessageChanging(FALSE);
}

void CDays2_ControlsDlg::OnBnClickedchkshowmessageaction()
{
 // TODO: Add your control notification handler code here
 /*if (m_bShowMessageAction == TRUE)
  GetDlgItem(IDC_lblRunAProgram)->ShowWindow(TRUE);
 else
  GetDlgItem(IDC_lblRunAProgram)->ShowWindow(FALSE);*/

}

void CDays2_ControlsDlg::OnBnClickedchkenableprogramaction()
{
 // TODO: Add your control notification handler code here
 
 UpdateData(TRUE);
 if (m_bEnableProgramAction == TRUE) // checked
  GUIProgramChanging(TRUE);
 else // unchecked
  GUIProgramChanging(FALSE);

}


void CDays2_ControlsDlg::GUIProgramChanging(bool flag)
{
 GetDlgItem(IDC_lblRunAProgram)->EnableWindow(flag);
 GetDlgItem(IDC_cmbRunAProgram)->EnableWindow(flag);
 GetDlgItem(IDC_btnRunProgram)->EnableWindow(flag);
}

void CDays2_ControlsDlg::GUIMessageChanging(bool flag)
{
 GetDlgItem(IDC_btnShowMessage)->EnableWindow(flag);
 GetDlgItem(IDC_txtEnterAMessage)->EnableWindow(flag);
 GetDlgItem(IDC_btnDefaultMessage)->EnableWindow(flag);
 GetDlgItem(IDC_btnClearMessage)->EnableWindow(flag);
 GetDlgItem(IDC_lblEnterAMessage)->EnableWindow(flag);
}

void CDays2_ControlsDlg::OnBnClickedbtnrunprogram()
{
 // TODO: Add your control notification handler code here

 // get the current values from the screen
 UpdateData(TRUE);

 // Declare a local variable for holding the program name
 CString strProgramName;

 // Copy the program name to the local variable
 strProgramName = m_strProgToRun;

 // make the program name all uppercase
 strProgramName.MakeUpper();


 // Notepad++;Paint;Word;
 DWORD dwError = 0; 

 // did the user select to run the paint Program?
 if (strProgramName == "PAINT") // run the paint program
 {
  WinExec("mspaint.exe", SW_SHOW);
  MessageBox(_T("open Paint"));
 }
 else if (strProgramName == "NOTEPAD++")
  WinExec("notepad.exe", SW_SHOW);
 else if (strProgramName == "WORD")
 {
  WinExec("C:\\Program Files\\Microsoft Office 2010\\Office14\\WINWORD.EXE", SW_SHOW);
  MessageBox(_T("open word"));
 }
 else
  dwError = GetLastError(); 
 
}

void CDays2_ControlsDlg::OnBnClickedbtnbrowser()
{
 
 OnFileOpen();
}

void CDays2_ControlsDlg::OnFileOpen()
{
 TCHAR szFilters[]= _T("All Files (*.*)|*.*||");

 CFileDialog fileDlg(TRUE, _T("*"), _T("*.*"),
      OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

   // Display the file dialog. When user clicks OK, fileDlg.DoModal()  
   // returns IDOK. 
   if(fileDlg.DoModal() == IDOK)
   {      
      m_txtPath = fileDlg.GetFileTitle(); // don't have ext
      m_txtPath = fileDlg.GetFileName(); // contains ext
   
   UpdateData(FALSE);      
   }
} 
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about How to Handle TextBox, Label, ComboBox and CheckBox Events in C++ , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips, all things Tech Tips for web development  - I am Zidane, See you next time soon ✋✋✋✋

🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com