Pro Visual C++-CLI And The .NET 2.0 Platform (2006) [eng]-1
.pdf418 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");
}
};
}
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";
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));