Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

InsertString CComboBox Problem

laephylaephy Posts: 3Member
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 as in the figure.

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:
[code]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);
[/code]

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. (see bild).

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:
[code]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(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(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("");
}
}
}
[/code]
Sign In or Register to comment.