CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2015
    Posts
    500

    MFC combox, user typed value

    Hello,

    Ive a legacy combobox of CBS_DROPDOWN type.

    And somehow, user typed value is not getting updated.

    I tried to write a small example code in VS2015, where, the event handler doesnot get called, on writting a text to combo box:
    Any suggestions on this is very helpful

    Code:
    
    // MFCApplication3Dlg.h : header file
    //
    
    #pragma once
    #include "afxwin.h"
    
    
    // CMFCApplication3Dlg dialog
    class CMFCApplication3Dlg : public CDialogEx
    {
    // Construction
    public:
    	CMFCApplication3Dlg(CWnd* pParent = NULL);	// standard constructor
    
    // Dialog Data
    #ifdef AFX_DESIGN_TIME
    	enum { IDD = IDD_MFCAPPLICATION3_DIALOG };
    #endif
    
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
    
    
    // Implementation
    protected:
    	HICON m_hIcon;
    
    	// Generated message map functions
    	virtual BOOL OnInitDialog();
    	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    	afx_msg void OnPaint();
    	afx_msg HCURSOR OnQueryDragIcon();
    	DECLARE_MESSAGE_MAP()
    public:
    	CComboBox m_sim;
    	CString m_val;
    	afx_msg void OnCbnSelchangeCombo1();
    	afx_msg void OnCbnSelchangeCombo2();
    };
    
    
    
    .cpp
    
    
    // MFCApplication3Dlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "MFCApplication3.h"
    #include "MFCApplication3Dlg.h"
    #include "afxdialogex.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // CAboutDlg dialog used for App About
    
    class CAboutDlg : public CDialogEx
    {
    public:
    	CAboutDlg();
    
    // Dialog Data
    #ifdef AFX_DESIGN_TIME
    	enum { IDD = IDD_ABOUTBOX };
    #endif
    
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    // Implementation
    protected:
    	DECLARE_MESSAGE_MAP()
    };
    
    CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
    {
    }
    
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialogEx::DoDataExchange(pDX);
    }
    
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
    END_MESSAGE_MAP()
    
    
    // CMFCApplication3Dlg dialog
    
    
    
    CMFCApplication3Dlg::CMFCApplication3Dlg(CWnd* pParent /*=NULL*/)
    	: CDialogEx(IDD_MFCAPPLICATION3_DIALOG, pParent)
    {
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CMFCApplication3Dlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialogEx::DoDataExchange(pDX);
    	DDX_Control(pDX, IDC_COMBO1, m_sim);
    }
    
    BEGIN_MESSAGE_MAP(CMFCApplication3Dlg, CDialogEx)
    	ON_WM_SYSCOMMAND()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_CBN_SELCHANGE(IDC_COMBO1, &CMFCApplication3Dlg::OnCbnSelchangeCombo1)
    	ON_CBN_SELCHANGE(IDC_COMBO2, &CMFCApplication3Dlg::OnCbnSelchangeCombo2)
    END_MESSAGE_MAP()
    
    
    // CMFCApplication3Dlg message handlers
    
    BOOL CMFCApplication3Dlg::OnInitDialog()
    {
    	CDialogEx::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)
    	{
    		BOOL bNameValid;
    		CString strAboutMenu;
    		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
    		ASSERT(bNameValid);
    		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 CMFCApplication3Dlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else
    	{
    		CDialogEx::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 CMFCApplication3Dlg::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
    	{
    		CDialogEx::OnPaint();
    	}
    }
    
    // The system calls this function to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CMFCApplication3Dlg::OnQueryDragIcon()
    {
    	return static_cast<HCURSOR>(m_hIcon);
    }
    
    
    
    
    void CMFCApplication3Dlg::OnCbnSelchangeCombo1()
    {
    	int item = m_sim.GetCurSel();
    
    	int sel = m_sim.GetCurSel();
    	if (sel == -1)
    	{
    		CString str;
    		m_sim.GetWindowTextW(str);
    	}
    	//if (sel == -1)
    	//{
    	//	int len = m_sim.GetWindowTextLength() + 1;
    	//	string tp = new char[len];
    	//	m_sim.GetWindowTextA(tp, len);
    	//	sel = m_sim.FindStringExact(-1, tp);
    	//	if (sel == -1)
    	//	{
    	//		sel = wnd.InsertString(0, tp);
    	//		wnd.SetCurSel(sel);
    	//	}
    	//	int cnt;
    	//	while ((cnt = wnd.GetCount()) >= 20)
    	//		wnd.DeleteString(cnt - 1);
    	//}
    	else
    	{
    		m_sim.GetLBText(m_sim.GetCurSel(), m_val);
    		UpdateData(FALSE);
    	}
    	//if (item != CB_ERR)
    	//{
    	//	CString text;
    	//	m_sim.GetLBText(item, text);
    	//}
    	// TODO: Add your control notification handler code here
    }
    
    void CMFCApplication3Dlg::OnCbnSelchangeCombo2()
    {
    	int i = 0;
    	// TODO: Add your control notification handler code here
    }

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,386

    Re: MFC combox, user typed value

    Try to handle CBN_EDITUPDATE or CBN_EDITCHANGE notification.
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured