GUI Forms: Input From and Output To a GUI Form

 

NOTE:

A Text Box holds a String (note the capital S. This is a different from a string!).

 

INPUT FROM TEXT BOX

 

Use TryParse to take an input String from a text box and convert to to whatever type you choose.

 

Say you want to input an integer num from textBox1, do it this way.

Int32::TryParse(textBox1->Text,num); // convert text to int & save in num

 

 
 

 

 

 


You can use the TryParse method to convert to many other types.  Here are some you can use:

 

Int16:: TryPars(textBox1->Text,num)        // convert to short int

Int32:: TryPars (textBox1->Text,num)                  // convert to regular int

Int64:: TryPars (textBox1->Text,num)                  // convert to long int

Double:: TryPars (textBox1->Text,num)                // convert to double

Char:: TryPars (textBox1->Text, achar)                // convert to char

Boolean:: TryPars (textBox1->Text, flag)              // convert true or false to boolean

 

OUTPUT TO TEXT BOX

To output to a Text Box you need to change whatever you want to show to a String.

So say you want to output a numeric variable (int, double, flost), use this for textBox2.

textBox2->Text = num.ToString ();

 

 
 

 

 


It’s much more complicated to convert a string to a String.  So if you have a string s you want to display in a Text Box, you should do it this way.

 

string s = "This is a string (small s)";

String ^systemstring = gcnew String(s.c_str());

systemstring += " (System::String)";

textBox2->Text = systemstring;

delete systemstring;

 
 

 

 

 


OTHER  GUI CONTROLS  (RADIO BUTTONS AND MESSAGE BOXES)

 

You can show a pop-up Message Box by putting what is shown below in your code.  When you design your Form,  you do not place this on your Form at all.   When you execute this line of code the Message Box will just pop-up in the middle of your screen.

 

MessageBox::Show ("Have a nice day") ;

 
 

 

 


If you put a radio button on your GUI, you can test it to see if checked or not checked (that means the user selected that button).    You test it by using

 

if (radioButton2->Checked == true)

{

      // calculate something or do something here

 

}

 
 

 

 

 

 


Let’s show an example where the radio button is tested and if checked you want to show a pop-up Message Box

if (radioButton2->Checked == true)

{

     MessageBox::Show ("You have selected button 2") ;

}