Reading in from a dialog box
Hello,
I am just writing a silly little application to add two numbers together, but I am not sure how to read in from the edit control boxes.
I have been trying to do it this was
Code:
int var1, var2;
var1 = IDC_EDIT1->Text;
var2 = IDC_EDIT2->Text;
MessageBox::Show((var1+var2).ToString());
But I am getting errors. Hopefully someone can help me out. Or point me to where on the MS site I can get some help.
Thanks.
Seán
Re: Reading in from a dialog box
You need to explicitly convert the strings to type int before assigning them to the int variables. A common tool to use for that is the int::Parse() method.
EDIT: Errr, wait... IDC_EDIT1 looks like a control ID like they are used in native C++ with MFC or Win API. Do you actually use .NET types here, i.e. are the text boxes of type System::Windows::Forms::TextBox? Otherwise they most likely wouldn't have a Text property anyway...
Re: Reading in from a dialog box
Hello,
Thanks for the reply. I have tried a different approach, but got a new issue. Hopefully someone can help.
Here is the code
Code:
{
CString myEdit1;
char a;
int i;
const int KEY = 23;
this -> GetDlgItemText(IDC_EDIT1, myEdit1);
int length = myEdit1.GetLength();
char *p = myEdit1.GetBuffer();
for (i = 0; i < length; i++)
{
*(p + i) = (*(p + i) ^ KEY);
}
a = (char)*(p+1);
SetDlgItemText(IDC_EDIT2, a);
}
And I am getting an error "error C2664: 'CWnd::SetDlgItemTextA' : cannot convert parameter 2 from 'char' to 'LPCTSTR' ...Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"
I am not sure how to sort this out. Hopefully someone can help.
Thanks in advance.
Seán
Re: Reading in from a dialog box
Quote:
Originally Posted by
o.fithcheallaigh
I have tried a different approach [...].
Obviously. Now that defininitely is native C++ with MFC, so there's no such thing as a Text property or MessageBox::Show() or int::ToString() methods. Questions about that should better be asked in the Visual C++ section.
Quote:
And I am getting an error "error C2664: 'CWnd::SetDlgItemTextA' : cannot convert parameter 2 from 'char' to 'LPCTSTR' ...Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"
The error message is rather obvious, I'd say: The method expects an LPCTSTR, which is a synonym for either char * or wchar_t *, depending on whether it's an MBCS or Unicode build. A char isn't implicitly convertible into that (and it's definitely better that it is that way - comment by me, not the error message :)).
This could serve as a quick and dirty fix:
Code:
char a[2] = {0};
// ...
a[0] = (char)*(p+1);
SetDlgItemText(IDC_EDIT2, a);
Re: Reading in from a dialog box
Quote:
Now that defininitely
is native C++ with MFC, so there's no such thing as a
Text property or
MessageBox::Show() or
int::ToString() methods. Questions about that should better be asked in the
Visual C++ section.
I did start off asking my questions there, but I was told to come here.
Quote:
The error message is rather obvious
I am afraid it wasn't to me :) ...and that is why I have come here to try and get help, and learn.
Quote:
I'd say: The method expects an
LPCTSTR, which is a synonym for either
char * or
wchar_t *, depending on whether it's an MBCS or Unicode build. A
char isn't implicitly convertible into that (and it's definitely better that it is that way - comment by me, not the error message :)).
This could serve as a quick and dirty fix:
Code:
char a[2] = {0};
// ...
a[0] = (char)*(p+1);
SetDlgItemText(IDC_EDIT2, a);
Thanks a lot, I will give that a go.
Re: Reading in from a dialog box
That did the trick!
Again, thanks!
Seán