Sunday, December 18, 2011

1-Tutorial 1 : Simple Intro on Setting up a Project and Using Basic Controls in Visual C++.NET 2010, 2008

Tutorial 1 : Simple Intro on Setting up a Project and Using Basic Controls in Visual C++.NET 2010, 2008
        This project will create a simple VB like interface using Windows C++ application in Microsoft Studio.NET 2008 which  transfers text from a TextBox to a Label at the click of a button.  Click here for the source code to the Click Me Program in Visual C++.NET for the 2010 version or here for the 2008 version. First set up the project by going to:
       File > New > Project  > Visual C++ Projects  > Windows Forms Application
       Then click browse and navigate to the folder to where you want to put your application and then click OK.  Name your project something simple like myInterface.  Go to the toolbox and add a button, a textbox and a label to your form, and make your form look something like this: 
Basic Controls in a Visual C++ Application
        To Change the caption on the button click on the button, and go to the buttons Property Window and select caption and type “Press Me”.   Click on the label, go to autosize under properties and set autosize to false. Still with the the label selected, set the borderstyle property to fixed3D. Go to text property for for the label remove all the text. Next double click on the button which will bring up the code window.     Add the one line of code between the brace below to the buton1_Click function:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
       {
         label1->Text=textBox1->Text;
       }
        Notice the code you add is almost identical to what would be added in Studio.NET Visual Basic, except an arrow is used instead of  a period and the usual C++ semicolon and braces.

2-Tutorial 2: Windows Basic Form Controls in C++ and Their Properties Using Visual C++.NET 2010, 2008

Tutorial 2: Windows Basic Form Controls in C++ and Their Properties Using Visual C++.NET 2010, 2008
        This program will show how to transfer an image to a pictureBox and do conversions from Strings to numerical values and numbers to String. Click here for the source code to the Basic Tools and Conversions Program in Visual C++.NET for the 2010 version or here to download the 2008 version. Set up a form that looks like this:    
Basic Controls in a Microsoft Visual C++ Program
        Set picturebox1 to a sizemode of stretchImage. Then within your project add a file called "heads.jpg" having a picture of a penny to your folder containing your .cpp source file and then add the following lines of code to the appropriate buttons functions:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
       {
         textBox1->Text="hi";
       }
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
     {
       label1->Text=textBox1->Text;
     }
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
     {
       pictureBox1->Image=Image::FromFile("heads.jpg");
     }
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
     {
       int num1;
       int num2;
       int answer;
       String ^strNum1;
       String ^strNum2;
       String ^strAnswer;
       strNum1=textBox2->Text;
       strNum2=textBox3->Text;
       num1=Int32::Parse(strNum1);
       num2=Int32::Parse(strNum2);
       answer=num1+num2;
       strAnswer=answer.ToString();
       label4->Text=strAnswer;
     }

3-Tutorial 3 : Windows Forms and Useful ListBox Properties Using Visual C++.NET 2010, 2008

Tutorial 3 : Windows Forms and Useful ListBox Properties Using Visual C++.NET 2010, 2008   
      This program will show how to use listboxes, and some of their more useful properties. Click here for the source code to the ListBox Demo Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. First create a form which looks as follows, including the one listBox shown:
Listboxes in a Visual Microsoft C++ Program
      Code the buttons as followings:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
       {
         listBox1->Items ->Add(textBox1->Text );
textBox1->Text="";
       }
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
     {
       int numListBoxItems;
numListBoxItems=listBox1->Items ->Count;
label2->Text=numListBoxItems.ToString() ;
     }
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
     {
       int position;
String ^myString;
myString=textBox2->Text;
position=listBox1->Items->IndexOf(myString);
label4->Text=position.ToString();
     }
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
     {
       String ^newWord;
int position;
newWord=textBox3->Text;
position=Int32::Parse(textBox4->Text);
listBox1->Items->Insert(position, newWord);
     }
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
     {
       listBox1->Sorted=true;
     }
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e)
     {
       String ^myString;
myString=textBox5->Text;
listBox1->Items->Remove(myString);
     }
private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e)
     {
       int position;
String ^myString;
myString=textBox6->Text;
position=Int32::Parse(myString);
listBox1->Items->RemoveAt(position);
     }
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
     }
      Note: If you lose the design window (which often happens when you open the “sln”  file using explorer) try the following: 
      Navigate to File > New > Project  > Window Form Application
       To make a design window appear which shows the forms go to view > designer

4-Tutorial 4: Simple Animation, Which Shows the Use of the Timer and Location Commands Using Visual C++.NET 2010, 2008

Tutorial 4: Simple Animation, Which Shows the Use of the Timer and Location Commands Using Visual C++.NET 2010, 2008
       This tutorial will show how to move a control button around the form employing the timer.  It also shows something about how placement works and the coordinate system of the form. Click here for the source code to the Simple Animation Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Make the form size 286, 314. Begin by designing a form which looks as follows:       
Basic animation with timers in a Microsoft Visual C++ Program
Set the timer's enabled property to false. Then code the following to the buttons:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
       {
         button1->Visible=false;
button2->Visible=true;
timer1->Enabled=true;
timer1->Interval=30;
       }
   private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
       {
         timer1->Enabled=false;
button1->Visible=true;
       }
   private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
       {
         static bool NE, NW, SW, SE;
         static int x, y;
         label1->Text=x.ToString();
         if (NE == false && NW == false && SW == false && SE== false)
         {
           x=8;
           y=64;
           SE=true;
         }
         if (NE)
         {
           x=x+4;
           y=y-4;
           if (x>=220)
           {
             SW=false;
             NE=false;
             NW=true;
             SE=false;
           }
           if (y<=0)
           {
             SW=false;
             NE=false;
             NW=false;
             SE=true;
           }
          }
 
         //*********************
         if (SE)
         {
           x=x+4;
           y=y+4;
           if (x>=220)
           {
             SW=true;
             NE=false;
             NW=false;
             SE=false;
           }
           if (y>=250)
           {
             SW=false;
             NE=true;
             NW=false;
             SE=false;
           }
         }
         //**********************
         if (SW)
         {
           x=x-4;
           y=y+4;
           if (x<=0)
           {
             SW=false;
             NE=false;
             NW=false;
             SE=true;
           }
           if (y>=250)
           {
             SW=false;
             NE=false;
             NW=true;
             SE=false;
           }
         }
         //************************
         if (NW)
         {
           x=x-4;
           y=y-4;
           if (x<=0)
           {
             SW=false;
             NE=true;
             NW=false;
             SE=false;
           }
           if (y<=0)
           {
             SW=true;
             NE=false;
             NW=false;
             SE=false;
           }
         }
         button2->Location=System::Drawing::Point(x,y);
       }
     private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
         {
         }

   (Back)

5-Tutorial 5 : Menus Using Visual C++.NET 2010, 2008

Tutorial 5 : Menus Using Visual C++.NET 2010, 2008
        In this tutorial you will add a Menu to make button and textbox appear. Click here for the source code to the Menus Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. To add a menu to your form go to the toolBox and double Click on the the mainMenu tool. This will add a menu to your form. Then type "&File" where it says "Type Here", which add File to the Menu. Then in the box below "File" type "&Make Button Appear" and then "M&ake TextBox Appear", then a lone hyphen (-), and then E&xit. The form should look something like this:   
Menus and Visibility
        Then add the following code to the appropriate item in the menu by double clicking on this item to get to its Code Window. To add code to specific menu items, double click on the menu item to bring up the code window.:
private: System::Void makeButtonAppearToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
     {
       button1->Visible=true;
     }
private: System::Void makeToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
     {
       textBox1->Visible=true;
     }
private: System::Void exitToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
     {
       System::Environment::Exit(System::Environment::ExitCode);
     }
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
     {
       button1->Visible=false;
       textBox1->Visible=false;
     }

6-Tutorial 6 : File Saving, Opening and Using Common Dialog Boxes Using Visual C++.NET 2010, 2008

Tutorial 6 : File Saving, Opening and Using Common Dialog Boxes Using Visual C++.NET 2010, 2008
        This program will show you how to save to file, open from file and how to use dialog boxes to perform these tasks. Click here for the source code to the File Saving, Opening Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Begin by placing a text file call "myfile.txt" in your inner-most debug directory with a few lines of text placed in it of your choosing. Make this file in Visual Studio by going to >File>New>File>General>Text File. Place this file in the directory with the source (.cpp) you are creating. Then design a form which looks as follow with a listbox to the left and a textBox to the right with multiline property selected. Add a openFileDialog and a saveFileDialog tool to your form. To the right of the listbox is a textbox with its multiline property selected.   
File Opening and Saving Common Dialog Boxes
        Add to the namespace region:
    using namespace System::IO;
        Add the following code to the relevant buttons:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
     {
       try
         {
           FileStream ^fileInput = gcnew FileStream("myfile.txt", FileMode::OpenOrCreate, FileAccess::ReadWrite);
           StreamReader ^streamIn = gcnew StreamReader(fileInput);
           while(!streamIn->EndOfStream)
           {
             listBox1->Items->Add(streamIn->ReadLine());
           };
           streamIn->Close();
         }
         catch(IOException ^)
         {
           textBox1->Text="something went wrong";
         }
     }
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
     {
       try
       {
         FileStream ^outFile = gcnew FileStream("mynewfile.txt", FileMode::Create, FileAccess::Write);
         StreamWriter ^streamOut = gcnew StreamWriter(outFile);
         streamOut->WriteLine(textBox1->Text);
         streamOut->Close();
       }
       catch(IOException ^)
       {
         textBox1->Text="something went wrong";
       }
     }
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
     {
       String ^myfile = "";
       openFileDialog1->ShowDialog() ;
       myfile = openFileDialog1->FileName;
       try
       {
         FileStream ^fileInput = gcnew FileStream(myfile, FileMode::OpenOrCreate, FileAccess::ReadWrite);
         StreamReader ^streamIn = gcnew StreamReader(fileInput);
         while(!streamIn->EndOfStream)
         {
           listBox1->Items->Add(streamIn->ReadLine());
         };
         streamIn->Close();
       }
       catch(IOException ^)
       {
         textBox1->Text="something went wrong";
       }
     }
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
     {
       String ^myfile = "";
       saveFileDialog1->ShowDialog() ;
       myfile = saveFileDialog1->FileName;
       try
       {
         FileStream ^outFile = gcnew FileStream(myfile, FileMode::Create, FileAccess::Write);
         StreamWriter ^streamOut = gcnew StreamWriter(outFile);
         streamOut->WriteLine(textBox1->Text);
         streamOut->Close();
       }
       catch(IOException ^)
       {
         textBox1->Text="something went wrong";
       }
     }
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
     {
       try
       {
         FileStream ^outFile = gcnew FileStream("myfile.txt", FileMode::Append, FileAccess::Write);
         StreamWriter ^streamOut = gcnew StreamWriter(outFile);
         streamOut->WriteLine(textBox1->Text);
         streamOut->Close();
       }
       catch(IOException ^)
       {
         textBox1->Text="something went wrong";
       }
     }

7-Tutorial 7: Concatenating Strings Together in the Windows C++ Environment using Visual C++.NET 2010, 2008

Tutorial 7: Concatenating Strings Together in the Windows C++ Environment using Visual C++.NET 2010, 2008:
        This program will show how to put two strings together to create a combination of the two. This called concatenation. Click here for the source code to the Concatenating Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Below the button is a label with its autosize property set to false. Make your form look something like this:   
Concatenating strings in Visual Microsoft Programs
Add the following code to the button:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
       {
         String ^strCat;
         String ^something=textBox1->Text;
         String ^somethingElse=textBox2->Text;
         strCat=something+" "+somethingElse;
         label3->Text=strCat;
       }