如何通过combo框中的下拉选项来控制edit框中的值,比如说下拉框中选择第一项后edit框中显示1,选择第二项后edit框中显示2。下拉框的选择函数第一个,初始化函数第二个 ,edit框的类型是value的int型
```c++
void CSetParaColDlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here
int m_nIndex;
m_nIndex = m_combo.GetCurSel();
m_combo.GetLBText(m_nIndex,m_string);
}
BOOL CSetParaColDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_combo.InsertString(0, _T("年/月/日 时:分:秒"));
m_combo.InsertString(1, _T("年,月,日,时,分,秒"));
m_combo.InsertString(2,_T("时:分:秒"));
OnSelchangeCombo1();
return TRUE;
}
```
找到m_combox更改text后触发的函数,在函数体内获取当前选中项,设置edit值
【以下回答由 GPT 生成】
问题分析: 根据给出的代码,可以看到combo框的选中项可以通过GetCurSel()
方法获取到索引,然后可以通过GetLBText()
方法获取到选中项的文本。但是目前的代码只是获取了选中项的文本并没有进行对应的操作来改变edit框中的值。所以需要在OnSelchangeCombo1()
方法中添加相应的代码来实现这个功能。
解决方案: 在OnSelchangeCombo1()
方法中,我们可以根据选中项的索引来进行判断,并根据判断结果来改变edit框中显示的值。具体的解决方案如下:
int m_nValue;
OnSelchangeCombo1()
方法中添加以下代码来根据选中项的索引来改变edit框中的值:void CSetParaColDlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here
int m_nIndex;
m_nIndex = m_combo.GetCurSel();
switch(m_nIndex)
{
case 0:
m_nValue = 1;
break;
case 1:
m_nValue = 2;
break;
case 2:
m_nValue = 3;
break;
// 根据需要添加更多的选项和对应的值
}
// 将值转换为字符串并在edit框中显示
CString strValue;
strValue.Format(_T("%d"), m_nValue);
m_edit.SetWindowText(strValue);
}
OnInitDialog()
方法中,将edit框的默认值设为0,并且将combo框的默认选项设为第一项。例如:BOOL CSetParaColDlg::OnInitDialog()
{
// ...
m_edit.SetWindowText(_T("0"));
m_combo.SetCurSel(0);
// ...
}
这样,在每次选择不同的combo框选项时,edit框中都会显示对应的值。
希望这个解决方案对您有所帮助。如果您还有任何问题,请随时提问。