Clearing textboxes with button click

Hi guys, I'm trying to make some textboxes be cleared in my Windows Form with a single button click, but I seem to have some problems. I tried the code below for the button but it keeps giving me a "error C2228: left of '.text' must have class/struct/union" problem.

1
2
3
4
void CzebraDlg::OnBnClickedClearimagedatabase()
{
	IDC_Box01.text=""; //The textbox ID is IDC_Box01
}


The editor code when I double click the textbox I want to clear is below

1
2
3
4
5
6
7
8
9
void CzebraDlg::OnEnChangeBox01()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}


I have about 20 or so boxes (IDC_Box01-20) to clear with the same button in a single click, so using the above format might be a little tedious, but I want to try and get the basic clearing of a textbox working in the first place before I complicate things.

I'm using MFC to build the form. Can anyone help? Thanks for any help in advance.
You should first create a variable for your textbox and you cannot access i using its resource ID. If you are using VC++, there is a wizard to do so. Hmmm, right click on the button in form design and you'll find it.
Hi there, I did as you said and used to wizard. It seems to have created a "CEdit m_Box01Ctl" in the header file linked to my Form dialog (code below).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public:
	CEdit m_Box01Ctl;
	CEdit m_Box02Ctl;
	CEdit m_Box03Ctl;
	CEdit m_Box04Ctl;
	CEdit m_Box05Ctl;
	CEdit m_Box06Ctl;
	CEdit m_Box07Ctl;
	CEdit m_Box08Ctl;
	CEdit m_Box09Ctl;
	CEdit m_Box10Ctl;
	CEdit m_Box11Ctl;
	CEdit m_Box12Ctl;
	CEdit m_Box13Ctl;
	CEdit m_Box14Ctl;
	CEdit m_Box15Ctl;
	CEdit m_Box16Ctl;
	CEdit m_Box17Ctl;
	CEdit m_Box18Ctl;
	CEdit m_Box19Ctl;
	CEdit m_Box20Ctl;


I tried using:
1
2
3
4
void CzebraDlg::OnBnClickedClearimagedatabase()
{
	m_Box01Ctl.text=""; //The textbox ID is IDC_Box01
}


But it gives me a "error C2039: 'text' : is not a member of 'CEdit'". Do you know the command I need to control the box and empty it?

Also, another problem I have, is that I want to make a radio button not selected by default when I start the program. I realise that the first of my radio buttons is always selected when I start the program but I want it to only be selected when I click it. Thanks a lot for the reply and any help in advance.
Usually when you type '.' after "m_Box01Ctl" IDE pops up a list of members which you can select one of them. Remember C++ is case sensitive so "text" is different from "Text". Use MSDN to study member variables.

In this case use SetWindowText() and GetWindowText().
Hi there, thank you very much. I've managed to solve the problem with what you told me and the MSDN help. I used:

1
2
3
4
5
void CzebraDlg::OnBnClickedClearimagedatabase()
{
	CWnd* pWnd = GetDlgItem(IDC_Box01);
pWnd->SetWindowText(_T(""));
}


and it works wonderfully. I will try a few methods for my radio button select as well. Any suggestions on where I should begin? Thanks again.
No its not the right way. Use m_Box01Ctl.SetWindowText(_T(""));
Hi, I used your code and it worked as well. Saved me a lot of space in my program. Thanks a lot!!
Topic archived. No new replies allowed.