May 01, 2024, 01:48:23 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


help : dialog + int <-> string

Started by huniper, April 17, 2007, 01:58:46 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

huniper

dlg d1;
   d1.Create(0,0,300,202,0x80C80080,0,"Caption",0);
   d1.AddControl(CTEDIT,"Edit1",12,9,70,20,0x50800000,0x200,1);
   d1.AddControl(CTEDIT,"Edit2",17,52,70,20,0x50800000,0x200,2);
   d1.AddControl(CTEDIT,"Edit3",23,103,70,20,0x50800000,0x200,3);
------------------------------------------------------------------------------------------------------------------

Hi,

I produced this code with Aurora's dialog generator.
Edit1 and Edit2  : read a number (string format)
Edit3 :sum of  two numbers (string format)  from Edit1 and Edit2
(It seems that I forgot to make a button with a caption of "Add".)

Could you write complete code this for me, please?

I want to learn,
First, how to combine GUI and logic code.
Second, how to convert string to integer and vice versa.

Thanks in advance.

ExMember001

April 17, 2007, 04:12:34 PM #1 Last Edit: April 17, 2007, 04:47:25 PM by KrYpT
When you create a dialog with dialog editor choose Generate Skeletton Code and Open in a new window
the dialog editor will generate the code for you and you will see how the GUI and logic work (Also read the help files)
when the code is generated you can compile and see what's happen.

To convert string to integer use the function strtonum("yourstring");
To convert integer to string use the function numtostr(yournum);

ill try to do a little quick example in the next minutes.

ExMember001

April 17, 2007, 04:36:46 PM #2 Last Edit: April 17, 2007, 04:43:26 PM by KrYpT
heres the little quick example:
its not exactly how the code is generate by the dialog editor, because i dont like how the code is generated ;)
this method is the more simple and more usefull.


//Controls ID define as constant
#define EDIT_1 1
#define EDIT_2 2
#define EDIT_3 3
#define STATIC_4 4
#define STATIC_5 5
#define BUTTON_6 6

//The dialog class definitions
class Calc:CDialog
{
//CDialog Methods (Overridden)
declare virtual OnInitDialog(),int;
declare virtual OnClose(),int;
declare virtual OnControl(int nID, int nNotifyCode, unsigned int hControl),int;

//Calc Methods
declare OpenDialog();
declare CalculateSum();

//Define Controls Pointers
CEDIT *first,*second,*sum;
CBUTTON *C_But;
}

//Method to Open the dialog
Calc::OpenDialog()
{
Create(0,0,300,160,0x80CA0080,0,"Quick Calculator",0);
AddControl(CTEDIT,"",19,45,70,20,0x50800000,0x200,EDIT_1);
AddControl(CTEDIT,"",115,45,70,20,0x50800000,0x200,EDIT_2);
AddControl(CTEDIT,"",211,45,70,20,0x50800000,0x200,EDIT_3);
AddControl(CTSTATIC,"+",89,46,25,20,0x50000101,0x0,STATIC_4);
AddControl(CTSTATIC,"=",187,46,23,20,0x50000101,0x0,STATIC_5);
AddControl(CTDEFBUTTON,"Calculate",115,103,70,20,0x50010001,0x0,BUTTON_6);

    //Show dialog as Modal
DoModal();
return;
}

//Method to close the dialog
Calc::OnClose(),int
{
CloseDialog(1);
return true;
}

//Method when the dialog is initiated
Calc::OnInitDialog(),int
{
/* Initialize any controls here */

//Get the controls id into pointers to play with them later
//Edit Controls
first     = getcontrol(EDIT_1);
second   = getcontrol(EDIT_2);
sum   = getcontrol(EDIT_3);
//Buttons
C_But   = getcontrol(BUTTON_6);

CenterWindow();
return true;
}

//Method to do something with our controls
Calc::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
select nID
{
case BUTTON_6:
if(nNotifyCode = 0) //If button clicked
{
CalculateSum();
}
}
return true;
}

Calc::CalculateSum()
{
//get the text from the controls
string firstnum = first->Gettext();
string secondNum = second->Gettext();
if (firstnum <> "") & (secondNum <> "")
{
//calculte
int total = STRTONUM(firstnum)+STRTONUM(secondNum);
//Put sum total into the 3rd edit control
sum->Settext(NUMTOSTR(total));
}
else
{
messagebox(this,"Please Enter something in the first 2 fields","Error");
}
return;
}

//The main function is the first read by the program
global sub main()
{
Calc d1;
//Open the dialog
d1.OpenDialog();
//Wait until the dialog is closed
do {wait();} until !d1.IsValid();
//Close program
return 0;
}


John S

Quote from: KrYpT on April 17, 2007, 04:12:34 PM
...
To convert string to integer use the function strtonum("yourstring");
To convert integer to string use the function numtostr(yournum);
...

StrToNum can also convert floating point numbers as doubles:

double SomeDecimalNumber = StrToNum("1234.56789");
double pi = StrToNum("3.14159");
John Siino, Advanced Engineering Services and Software

huniper

Thanks, guys!

I tested it.
And it worked. :)

Let me try to analyze code more.