-
May 2nd, 2012, 12:59 PM
#1
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
-
May 3rd, 2012, 04:11 PM
#2
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...
Last edited by Eri523; May 3rd, 2012 at 04:16 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
May 5th, 2012, 04:36 AM
#3
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
-
May 5th, 2012, 06:35 AM
#4
Re: Reading in from a dialog box
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.
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);
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
May 5th, 2012, 02:51 PM
#5
Re: Reading in from a dialog box
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.
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.
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.
-
May 5th, 2012, 03:11 PM
#6
Re: Reading in from a dialog box
That did the trick!
Again, thanks!
Seán
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|