Visual C++

Moderators: Lundin
Number of threads: 379
Number of posts: 695

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
InsertString CComboBox Problem Posted by laephy on 12 Apr 2012 at 9:06 AM
Hi, I am trying to understand the CComboBox and its different functions for that I do a small dialog based program which contains a combobox and I want to give the user the possibility to add a new item to the list.

I add two check controls (Old Color and New Color), a combobox (to select the color of the list if Old Color is selected) and two edit controls (to add a new color number if New Color is selected).

I have declared the following variables:
DDX_Control(pDX, IDC_COMBO1, m_ColorList);
	DDX_Text(pDX, IDC_IDCOLOR, m_IdColor);
	DDX_Text(pDX, IDC_COLORNAME, m_ColorName);
	DDX_Check(pDX, IDC_NEWCOLOR, m_NewColor);
	DDX_Check(pDX, IDC_OLDCOLOR, m_OldColor);


I want to assign a number automatically for the new color, for that I use the GetCount function and I initialize in the OnInitDialog. It works fine at the beginning.

The user can only add a new Color to the ColorList if he selects the NewColor Control and this can be added to the list only if the user clicks on Save Button. It works fine, the new color is added to the list, but the Color number is not updated.

I want also that these new color is store for the next time I open the program. But it does not work.

The code I use if the following:
BOOL CMyComboBoxAddingDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	..........
	
	// TODO: Add extra initialization here

	//The Save button should be activated only if New Material is selected.
	GetDlgItem(IDSAVE) -> EnableWindow(FALSE);

	//Definition of variable nNoOfEntries equal to the total number of options in the ColorList
	int nNoOfEntries = m_ColorList.GetCount();  
	m_IdColor = nNoOfEntries + 1;  // Assignment a number for the new Color

	
	UpdateData(FALSE);  // would update the data from control variables to control
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTubeMaterial::OnSelMaterial()                                     
{
	UpdateData(TRUE); //transfers the index to the variable.

	m_Material = m_MaterialList.GetCurSel();

	UpdateData(FALSE);
}


void CMyComboBoxAddingDlg::OnOK() 
{
	// TODO: Add extra validation here
	if(m_NewColor)
	{
		// Association of a pointer to the EditControl
		CEdit *pColorName = static_cast<CEdit*>(GetDlgItem(IDC_COLORNAME));

		// Checking if there is some text
		if (pColorName->LineLength() == 0)
			MessageBox("No entry in the entry field available!");
		else
		{
			// To read the text
			pColorName->GetWindowText(m_ColorName);
			// Inserting to the ComboBox
			m_ColorList.InsertString(-1,m_ColorName);
			// Deleting the text
			//pColorName->SetWindowText("");
		}    
	}
	
	CDialog::OnOK();
}


void CMyComboBoxAddingDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	
	if(m_NewColor)
	{
		// Association of a pointer to the EditControl
		CEdit *pColorName = static_cast<CEdit*>(GetDlgItem(IDC_COLORNAME));
		
		// Checking if there is some text
		if (pColorName->LineLength() == 0)
			MessageBox("No entry in the Color Name field available!");
		else
		{
			// To read the text
			pColorName->GetWindowText(m_ColorName);
			// Inserting to the ComboBox
			m_ColorList.InsertString(-1,m_ColorName);
			// Deleting the text
			//pColorName->SetWindowText("");
		}    
	}
}

Report
Re: InsertString CComboBox Problem Posted by laephy on 17 Apr 2012 at 1:34 PM
Any suggestions to update the CComboBox? and to update the m_IdColor variable?



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.