Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro Visual C++-CLI And The .NET 2.0 Platform (2006) [eng]-1

.pdf
Скачиваний:
70
Добавлен:
16.08.2013
Размер:
24.18 Mб
Скачать

418 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

//miFileSubThis

this->miFileSubThis->Items->AddRange( gcnew cli::array< System::Object^>(3) {L"This", L"That", L"Other Thing"});

this->miFileSubThis->Name = L"miFileSubThis"; this->miFileSubThis->Size = System::Drawing::Size(121, 21);

//miFileSubCheck

//

this->miFileSubCheck->Checked = true; this->miFileSubCheck->CheckOnClick = true; this->miFileSubCheck->CheckState =

System::Windows::Forms::CheckState::Checked; this->miFileSubCheck->Name = L"miFileSubCheck"; this->miFileSubCheck->Size = System::Drawing::Size(181, 22); this->miFileSubCheck->Text = L"Check Me";

//

// miFileSubImage

//

this->miFileSubImage->Image = (cli::safe_cast<System::Drawing::Image^> (resources->GetObject(L"miFileSubImage.Image")));

this->miFileSubImage->Name = L"miFileSubImage"; this->miFileSubImage->Size = System::Drawing::Size(181, 22); this->miFileSubImage->Text = L"I have an image";

//

//miFileSubSayBoo

this->miFileSubSayBoo->Name = L"miFileSubSayBoo"; this->miFileSubSayBoo->ShortcutKeys =

static_cast<System::Windows::Forms::Keys> ((System::Windows::Forms::Keys::Control |

System::Windows::Forms::Keys::S)); this->miFileSubSayBoo->Size = System::Drawing::Size(181, 22);

this->miFileSubSayBoo->Text = L"Say Boo"; this->miFileSubSayBoo->Click +=

gcnew System::EventHandler(this,&Form1::miFileSubSayBoo_Click);

//miFileSep1

//

this->miFileSep1->Name = L"miFileSep1"; this->miFileSep1->Size = System::Drawing::Size(149, 6);

//

//miFileExit

this->miFileExit->Name = L"miFileExit"; this->miFileExit->Size = System::Drawing::Size(152, 22);

this->miFileExit->Text = L"E&xit"; this->miFileExit->Click +=

gcnew System::EventHandler(this, &Form1::miFileExit_Click);

//miHelp

//

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

419

this->miHelp->DropDownItems->AddRange(

gcnew cli::array< System::Windows::Forms::ToolStripItem^>(1) {this->miHelpAbout});

this->miHelp->Name = L"miHelp";

this->miHelp->Size = System::Drawing::Size(40, 20); this->miHelp->Text = L"&Help";

//

//miHelpAbout

this->miHelpAbout->Name = L"miHelpAbout"; this->miHelpAbout->Size = System::Drawing::Size(152, 22);

this->miHelpAbout->Text = L"About"; this->miHelpAbout->Click +=

gcnew System::EventHandler(this, &Form1::miHelpAbout_Click);

//Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 273); this->Controls->Add(this->toolStripContainer1); this->MainMenuStrip = this->mainMenuStrip;

this->Name = L"Form1"; this->Text = L"Simple Menu";

this->toolStripContainer1->TopToolStripPanel->ResumeLayout(false); this->toolStripContainer1->TopToolStripPanel->PerformLayout(); this->toolStripContainer1->ResumeLayout(false); this->toolStripContainer1->PerformLayout(); this->mainMenuStrip->ResumeLayout(false); this->mainMenuStrip->PerformLayout();

this->ResumeLayout(false);

}

#pragma endregion

private:

System::Void miFileExit_Click(System::Object^ sender, System::EventArgs^ e)

{

Application::Exit();

}

System::Void miHelpAbout_Click(System::Object^ sender, System::EventArgs^ e)

{

MessageBox::Show("Simple Menu v.1.0.0.0");

}

System::Void miFileSubSayBoo_Click(System::Object^ sender, System::EventArgs^ e)

{

MessageBox::Show("BOO");

}

};

}

420 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

Figure 10-9 shows what SimpleMenu.exe looks like when you execute it.

Figure 10-9. A simple menu

Bells and Whistles Controls

You’ll finish off looking at Win Form controls by exploring some fun controls that you may not use that often, but that can occasionally come in handy.

PictureBox

The PictureBox is a handy little control for displaying an existing image file. What makes it really cool is that it has built-in support for bitmaps, metafiles, and icons, and .jpg, .gif, and .png files. You implement all of them the same way:

1.Drag and drop the PictureBox to your Win Form.

2.Update the Image property in the PictureBox’s Properties view with the location of your file using the provided Open dialog box.

Like all controls, PictureBox provides properties to manipulate itself. In most cases you will only have to worry about the following:

BorderStyle is a BorderStyle enum that represents the border to surround your image. Three borders are available: Fixed3D, FixedSingle, and the default None.

Image is an Image object that represents the image to be displayed. The Image object supports bitmaps, metafiles, and icons, and .jpg, .gif, and .png files.

Size is a Size object that represents the height and width of the control. If the SizeMode is set to StretchImage, then the images inside will stretch or shrink to fit this size.

SizeMode is a PictureBoxSizeMode that represents how the image will be displayed. The four modes are AutoSize, which forces the control to be the same size as the image; CenterImage, which centers the image within the control (the image will be clipped if the control is too small); the default Normal, which aligns the picture with the upper-left corner; and StretchImage, which makes the image the same size as the control.

The code in Listing 10-8 shows a picture of my daughter in a StretchImage mode PictureBox.

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

421

Listing 10-8. PictureBox of Shaina

namespace

PictureBoxEx

{

 

using

namespace System;

using

namespace System::ComponentModel;

using

namespace System::Collections;

using

namespace System::Windows::Forms;

using

namespace System::Data;

using

namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

}

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private:

System::Windows::Forms::PictureBox^ pictureBox1; System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

void InitializeComponent(void)

{

System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));

this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); (cli::safe_cast<System::ComponentModel::ISupportInitialize^>

(this->pictureBox1))->BeginInit(); this->SuspendLayout();

//

// pictureBox1

//

this->pictureBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>

((((System::Windows::Forms::AnchorStyles::Top

| System::Windows::Forms::AnchorStyles::Bottom) | System::Windows::Forms::AnchorStyles::Left)

| System::Windows::Forms::AnchorStyles::Right)); this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^>

(resources->GetObject(L"pictureBox1.Image"))); this->pictureBox1->Location = System::Drawing::Point(12, 12); this->pictureBox1->Name = L"pictureBox1";

422 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

this->pictureBox1->Size = System::Drawing::Size(369, 287); this->pictureBox1->SizeMode =

System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->pictureBox1->TabIndex = 0;

this->pictureBox1->TabStop = false;

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(393, 311); this->Controls->Add(this->pictureBox1);

this->Name = L"Form1"; this->Text = L"Shaina Shoshana";

(cli::safe_cast<System::ComponentModel::ISupportInitialize^> (this->pictureBox1))->EndInit();

this->ResumeLayout(false);

}

#pragma endregion };

}

You might want to note in the preceding code that Visual Studio 2005 creates a resource of the PictureBox’s image and places it within the assembly in a similar fashion to the ImageList, instead of referencing the file. If you don’t want the image placed in the assembly for some reason, then you’ll have to code the updating of the Image property manually with code similar to this:

this->pictureBox->Image = new Drawing::Bitmap(S"ShainaOk.jpg");

Figure 10-10 shows what PictureBoxEx.exe looks like when you execute it.

Figure 10-10. A PictureBox of Shaina

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

423

MonthCalendar

The MonthCalendar is a neat little control that provides the ability to display a month to the user and then allow the user to do things such as navigate from month to month and select a year, month, day, or range of days. Another feature of the MonthCalendar control is it allows the user to highlight specific dates on the control, either on an annual, monthly, or specific single-day basis.

Like all controls, you configure MonthCalendar using properties. Here are some of the most commonly used properties:

AnnuallyBoldedDates is an array of DateTime objects that represents which dates to bold every year.

BoldedDates is an array of DateTime objects that represents which specific dates to bold.

CalendarDimensions is a System::Drawing::Size that represents the number of rows and columns of months to be displayed within the control. The maximum number of months that can be displayed is 12.

MaxDate is a DateTime that represents the maximum date that can be shown in the control. The default is 12/31/9998.

MaxSelectionCount is an Int32 that represents the maximum number of dates that can be selected at one time. The default is 7.

MinDate is a DateTime that represents the minimum date that can be shown in the control. The default is 01/01/1753.

MonthlyBoldedDates is an array of DateTime objects that represents which dates to bold every month.

SelectionEnd is a DateTime that represents the end date of the selected date range. The default is SelectionEnd (equaling SelectionStart).

SelectionRange is a SelectionRange object that represents the selected range of dates within the control.

SelectionStart is a DateTime that represents the start date of the selected date range.

ShowToday is a Boolean that represents whether the date specified in the TodayDate property is shown at the bottom of the control.

ShowTodayCircle is a Boolean that represents whether the date specified in the TodayDate property is circled.

ShowWeekNumbers is a Boolean that represents whether the week number is displayed for each week.

TodayDate is a DateTime representing any date that you want to be set as today’s date. The default is the current system date.

TodayDateSet is a Boolean that represents whether the TodayDate property was explicitly set.

Something you might want to note about the MonthCalendar control is that you can’t select dates at random intervals. You can only select individual days or a range of days sequentially.

Listing 10-9 presents the MonthCalendar in action. The code simply shows a two-by-two MonthCalendar control that generates DateChanged events when clicked. It also has two additional labels to display the selected day or ranges of days.

424 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

Listing 10-9. The MonthCalendar Control

namespace

MonthCalendarEx

{

 

using

namespace System;

using

namespace System::ComponentModel;

using

namespace System::Collections;

using

namespace System::Windows::Forms;

using

namespace System::Data;

using

namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

}

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private:

System::Windows::Forms::Label^ End; System::Windows::Forms::Label^ Start; System::Windows::Forms::MonthCalendar^ monthCal; System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code void InitializeComponent(void)

{

this->End = (gcnew System::Windows::Forms::Label()); this->Start = (gcnew System::Windows::Forms::Label());

this->monthCal = (gcnew System::Windows::Forms::MonthCalendar()); this->SuspendLayout();

//

//End

this->End->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;

this->End->Location = System::Drawing::Point(230, 323); this->End->Name = L"End";

this->End->Size = System::Drawing::Size(83, 20); this->End->TabIndex = 5;

//Start

//

this->Start->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

425

this->Start->Location = System::Drawing::Point(122, 323); this->Start->Name = L"Start";

this->Start->Size = System::Drawing::Size(83, 20); this->Start->TabIndex = 4;

//

// monthCal

//

this->monthCal->AnnuallyBoldedDates =

gcnew cli::array< System::DateTime >(1) {System::DateTime(2004, 12, 31, 0, 0, 0, 0)};

this->monthCal->CalendarDimensions = System::Drawing::Size(2, 2); this->monthCal->Location = System::Drawing::Point(13, 11); this->monthCal->MaxSelectionCount = 365; this->monthCal->MonthlyBoldedDates =

gcnew cli::array< System::DateTime >(2) {System::DateTime(2004, 10, 1, 0, 0, 0, 0),

System::DateTime(2004, 10, 15, 0, 0, 0, 0)}; this->monthCal->Name = L"monthCal"; this->monthCal->ShowWeekNumbers = true; this->monthCal->Size = System::Drawing::Size(410, 297); this->monthCal->TabIndex = 3; this->monthCal->DateChanged +=

gcnew System::Windows::Forms::DateRangeEventHandler(this, &Form1::monthCal_DateChanged);

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(436, 355); this->Controls->Add(this->End);

this->Controls->Add(this->Start); this->Controls->Add(this->monthCal); this->Name = L"Form1";

this->Text = L"Month Calendar"; this->ResumeLayout(false);

}

#pragma endregion private:

System::Void monthCal_DateChanged(System::Object^ sender, System::Windows::Forms::DateRangeEventArgs^ e)

{

// Update start and end range labels when date changes Start->Text = e->Start.Date.ToShortDateString(); End->Text = e->End.Date.ToShortDateString();

}

};

}

The only thing unusual about the preceding code is that you need to remember that System::DateTime is a value type structure, and thus you don’t create it on the stack with the gcnew statement. Also, when you use System::DateTime in a statement, you use the operator . and not ->.

Figure 10-11 shows what MonthCalendarEx.exe looks like when you execute it.

426 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

Figure 10-11. The MonthCalendar control

ErrorProvider

The ErrorProvider control is a nice piece of eye candy, especially when it comes to form validation, as you can use it to provide visual attention to data entry errors on the form. It has the additional bonus of being able to tell the user the reason for the data entry error. It provides this functionality by placing an icon next to the control in error and then providing a ToolTip-like pop-up displaying the reason for the error when the mouse pauses over the icon. Actually, it displays any text that you provide to it. In theory, this text should be the reason for the error.

Another interesting feature of the ErrorProvider control is that you need only one for your entire form. Yet, at the same time, it provides a specific error message for each control in error.

To implement the ErrorProvider control, drag and drop it to your Design view from the Toolbox view. Then, when an error occurs in your validation process, place an error message along with a pointer to the control in error into the ErrorProvider.

To customize the look and feel of the ErrorProvider control, a few members are provided. These are the properties that you will most likely change:

BlinkRate is an Int32 that represents the flash rate of the icon in milliseconds. The default is 250 milliseconds.

BlinkStyle is an ErrorBlinkStyle enum that represents the style that the icon blinks. The possible values are AlwaysBlink, NeverBlink, and the default BlinkIfDifferentError.

Icon is an Icon object that represents the icon to be displayed on error. The default is a red circle with a white exclamation point inside.

SetError() is a method that sets the error for a specified control to display when the mouse pauses over the icon. When the message is an empty string, no icon or error is displayed.

SetIconAlignment() is a method that sets the icon’s location relative to a specified control. The default is MiddleRight.

SetIconIconPadding() is a method that specifies the number of pixels of padding to add between an icon and a specified control. Because many controls have white space surrounding them, this control is not used too often.

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

427

Listing 10-10 shows the ErrorProvider control in action. The code is the start of a login form that validates that a name and password have been entered. When either of these fields is blank, the ErrorProvider control is added after the control on the form. Just for grins and giggles, I show how to place the icon on the left side of the control when validating on the Button control.

Listing 10-10. The ErrorProvider Control

namespace

ErrorProviderEx

{

 

using

namespace System;

using

namespace System::ComponentModel;

using

namespace System::Collections;

using

namespace System::Windows::Forms;

using

namespace System::Data;

using

namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

}

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private:

System::Windows::Forms::TextBox^ tbPword; System::Windows::Forms::Label^ lbPword; System::Windows::Forms::Button^ bnLogin; System::Windows::Forms::TextBox^ tbName; System::Windows::Forms::Label^ lbName; System::Windows::Forms::ErrorProvider^ eProvider; System::ComponentModel::IContainer^ components;

#pragma region Windows Form Designer generated code void InitializeComponent(void)

{

this->components = (gcnew System::ComponentModel::Container()); this->tbPword = (gcnew System::Windows::Forms::TextBox()); this->lbPword = (gcnew System::Windows::Forms::Label()); this->bnLogin = (gcnew System::Windows::Forms::Button()); this->tbName = (gcnew System::Windows::Forms::TextBox()); this->lbName = (gcnew System::Windows::Forms::Label()); this->eProvider =

(gcnew System::Windows::Forms::ErrorProvider(this->components));