+ All Categories
Home > Technology > C++ Windows Forms L03 - Controls P2

C++ Windows Forms L03 - Controls P2

Date post: 25-Dec-2014
Category:
Upload: mohammad-shaker
View: 329 times
Download: 4 times
Share this document with a friend
Description:
C++ Windows Forms L03 - Controls P2 of C++ Windows Forms Light Course
57
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L03 – Controls Part 2
Transcript
Page 1: C++ Windows Forms L03 - Controls P2

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L03 – Controls Part 2

Page 2: C++ Windows Forms L03 - Controls P2

Switching Between Forms

Page 3: C++ Windows Forms L03 - Controls P2

It’s another form..

Page 4: C++ Windows Forms L03 - Controls P2

Switching between forms

Page 5: C++ Windows Forms L03 - Controls P2

Switching to a another forms

• Let’s consider that we want to have two forms and we want to switch between them – #1: First of all we add a new “second” form to out project

• Add > new item > form

– #2: include its header in first form header file – ( “Form1.h” file )

– #3: pointing to the other form

– #4: and start playing with it :D

#include "Form2.h"

Form2 ^f= gcnew Form2;

f->Show();

Page 6: C++ Windows Forms L03 - Controls P2

Switching Back to Form1

• In “Form2.h”

public: Form1 ^FPtr;

namespace MyTestPro {

ref class Form1;

Form2(Form1 ^f)

{

InitializeComponent();

FPtr= f;

}

Page 7: C++ Windows Forms L03 - Controls P2

Switching Back to Form1

• In “Form2.cpp”#include "StdAfx.h"

#include "Form2.h"

#include "Form1.h”

namespace MyTestPro {

System::Void Form2::Form2_Load(System::Object^ sender,

System::EventArgs^ e)

{

FPtr->Text= “New Form1 Text!";

}

}

Page 8: C++ Windows Forms L03 - Controls P2

Peak on Drawing ClassPoint & Size

Page 9: C++ Windows Forms L03 - Controls P2

Peak on Drawing::Point

• What will happen now?

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

button1->Location= 30,120;

}

Page 10: C++ Windows Forms L03 - Controls P2

Peak on Drawing::Point

Page 11: C++ Windows Forms L03 - Controls P2

Peak on Drawing::Point

• What will happen now?

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

Drawing::Point P;

P.X= 2;

P.Y= 23;

button1->Location= P;

}

Page 12: C++ Windows Forms L03 - Controls P2

Drawing::Point

Page 13: C++ Windows Forms L03 - Controls P2

Helpers

Page 14: C++ Windows Forms L03 - Controls P2

Helpers

So, what should we do?

Page 15: C++ Windows Forms L03 - Controls P2

Point and Size

• Can we do this? Yes!private: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

Drawing::Size S;

S.Height= 200;

S.Width= 300;

this->Size= S;

}

Page 16: C++ Windows Forms L03 - Controls P2

Point and Size

• Can we do this? private: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

Drawing::Size ^S;

S->Height= 200;

S->Width= 300;

this->Size= S;

}

Compile error

Error 1 error C2664: 'void

System::Windows::Forms::Control::Size::set(System::Drawing::Size)':

cannot convert parameter 1 from 'System::Drawing::Size ^' to

'System::Drawing::Size' c:\users\zgtr\documents\visual studio

2008\projects\dotnet4\dotnet4\Form1.h 129 dotNet4

Page 17: C++ Windows Forms L03 - Controls P2

Helpers

Page 18: C++ Windows Forms L03 - Controls P2

Point and Size

private: System::Void button1_Click_1(System::Object^

sender, System::EventArgs^ e)

{

this->Size= Drawing::Size(200,300);

}

Works!

Page 19: C++ Windows Forms L03 - Controls P2

Helpers

Page 20: C++ Windows Forms L03 - Controls P2

Point and Size

Page 21: C++ Windows Forms L03 - Controls P2

Label

Page 22: C++ Windows Forms L03 - Controls P2

Label

• Properties:Name, Text, Font, image, Location, Size, TabStop, TabIndex, Visible, BackColor

• Event:MouseClick, MouseDown, MouseLeave, Resize, SizeChange, TextChanged, VisibleChanged, KeyUp, KeyDown, DragDrop

Page 23: C++ Windows Forms L03 - Controls P2

TextBox

Page 24: C++ Windows Forms L03 - Controls P2

TextBox

• Properties:Name, Text, Font, image, Location, Size, TabStop, TabIndex, Visible, BackColor, MultiLine, ScrollBar

• Event:MouseClick, MouseDown, MouseLeave, Resize, SizeChange, TextChanged, VisibleChanged, KeyUp, KeyDown, DragDrop

Page 25: C++ Windows Forms L03 - Controls P2

Panel

Page 26: C++ Windows Forms L03 - Controls P2

Panel

• Properties:Name, Text, Font, image, Location, Size, TabStop, TabIndex, Visible, BackColor, Border Style, AutoSize

• Event:MouseClick, MouseDown, MouseLeave, Resize, SizeChange, TextChanged, VisibleChanged, KeyUp, KeyDown, DragDrop

Page 27: C++ Windows Forms L03 - Controls P2

TrackBar

Page 28: C++ Windows Forms L03 - Controls P2

TrackBar

Page 29: C++ Windows Forms L03 - Controls P2

TrackBar

Page 30: C++ Windows Forms L03 - Controls P2

TrackBar

Keyboard keys (Arrows)

position

Number of ticks between tick marks

Page 31: C++ Windows Forms L03 - Controls P2

TrackBar

• What will happen now?

private: System::Void trackBar1_Scroll(System::Object^ sender,

System::EventArgs^ e)

{

textBox1->Text= trackBar1->Value.ToString();

}

Page 32: C++ Windows Forms L03 - Controls P2

TrackBar

Page 33: C++ Windows Forms L03 - Controls P2

ProgressBar

Page 34: C++ Windows Forms L03 - Controls P2

ProgressBar

Page 35: C++ Windows Forms L03 - Controls P2

ProgressBar

Page 36: C++ Windows Forms L03 - Controls P2

ProgressBar

Page 37: C++ Windows Forms L03 - Controls P2

ProgressBar

• Increment the progressBar and perform the “performStep()” • What will happen when pressing the button?

private: System::Void button1_Click_4(System::Object^ sender,

System::EventArgs^ e)

{

progressBar1->PerformStep();

}

Page 38: C++ Windows Forms L03 - Controls P2

ProgressBar

• What will happen now when pressing the button, repeatedly?

private: System::Void button1_Click_4(System::Object^ sender,

System::EventArgs^ e)

{

progressBar1->PerformStep();

textBox1->Text= progressBar1->Value.ToString();

}

Page 39: C++ Windows Forms L03 - Controls P2

ProgressBar After clicking button1 for 1st time

After clicking button1 for 2nd time

Before clicking button1

Page 40: C++ Windows Forms L03 - Controls P2

ProgressBar

• Another example:– The following code example uses a ProgressBar control to display the

progress of a file copy operation. The example uses the Minimum and Maximum properties to specify a range for the ProgressBar that is equivalent to the number of files to copy. The code also uses the Step property with the PerformStep method to increment the value of theProgressBar as a file is copied. This example requires that you have a ProgressBar control created called pBar1 that is created within a Form and that there is a method created called CopyFile (that returns a Boolean value indicating the file copy operation was completed successfully) that performs the file copy operation. The code also requires that an array of strings containing the files to copy is created and passed to the CopyWithProgress method defined in the example and that the method is called from another method or event in the Form.

Page 41: C++ Windows Forms L03 - Controls P2

private:void CopyWithProgress( array<String^>^filenames ){

// Display the ProgressBar control.pBar1->Visible= true;

// Set Minimum to 1 to represent the first file being copied.pBar1->Minimum= 1;

// Set Maximum to the total number of files to copy.pBar1->Maximum= filenames->Length;

// Set the initial value of the ProgressBar.pBar1->Value= 1;

// Set the Step property to a value of 1 to represent each file being copied.

pBar1->Step= 1;

// Loop through all files to copy.for ( int x= 1; x <= filenames->Length; x++ ){

// Copy the file and increment the ProgressBar if successful.if ( CopyFile( filenames[ x - 1 ] )== true ){

// Perform the increment on the ProgressBar.pBar1->PerformStep();

}}

}

Page 42: C++ Windows Forms L03 - Controls P2

NumericUpDown

Page 43: C++ Windows Forms L03 - Controls P2

NumericUpDown properties

The interval

Starting value

Page 44: C++ Windows Forms L03 - Controls P2

NumericUpDown - coding

• Text property

private: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

textBox1->Text= numericUpDown1->Text;

}

Page 45: C++ Windows Forms L03 - Controls P2

NumericUpDown - coding

• Everything ok?

private: System::Void textBox1_TextChanged(System::Object^ sender,

System::EventArgs^ e)

{

numericUpDown1->Value= textBox1->Text;

}

Compiler error. String assigned to int

Page 46: C++ Windows Forms L03 - Controls P2

NumericUpDown - coding

• As always, we can change anything at runtime

private: System::Void textBox1_TextChanged(System::Object^ sender,

System::EventArgs^ e)

{

numericUpDown1->Value= int::Parse(textBox1->Text);

}

Page 47: C++ Windows Forms L03 - Controls P2

NumericUpDown - coding

• As always, we can change anything at runtime

private: System::Void textBox1_TextChanged(System::Object^ sender,

System::EventArgs^ e)

{

int i =int::TryParse(textBox1->Text, numericUpDown1->Value);

}

Page 48: C++ Windows Forms L03 - Controls P2

NumericUpDown - coding

• As always, we can change anything at runtime

private: System::Void textBox1_TextChanged(System::Object^ sender,

System::EventArgs^ e)

{

numericUpDown1->Text= textBox1->Text;

}

Page 49: C++ Windows Forms L03 - Controls P2

PictureBox

Page 50: C++ Windows Forms L03 - Controls P2

PictureBox

• Very important for drawing!

Page 51: C++ Windows Forms L03 - Controls P2

PictureBox

Page 52: C++ Windows Forms L03 - Controls P2

PictureBox

Page 53: C++ Windows Forms L03 - Controls P2

PictureBox

Page 54: C++ Windows Forms L03 - Controls P2

PictureBox

Page 55: C++ Windows Forms L03 - Controls P2

PictureBox

Page 56: C++ Windows Forms L03 - Controls P2

PictureBox .gif

Page 57: C++ Windows Forms L03 - Controls P2

That’s it for today!


Recommended