Tuesday, May 29, 2007

Understanding VC++.NET 2nd Installment

I was a little busy in a training so was unable to post this program early.
This is a simple Windows application program.Again I would like to say the inspiration and guidance is taken from msdn.Although I have changed the program a little, just to suite my own satisfactions.I give all credit to msdn.
So here it comes...

Creating a Windows Application:


1. Create a new VS 2005 project in VC++
2. In the Project Types pane, select CLR in the Visual C++ node, then select Windows Forms Application in the Templates pane.
3. Enter the solution name and location and click OK.
4. Now drag three labels, a Datetimepicker and a button control from the tool box.
5. Set The name of the form by clicking the Text property of form as “Date chooser”
6. Set the text of the label as “Choose a date”, Visibility as true. Place this label just before the datetime picker.
7. Place the other two labels just below the first label and the datetime picker and set the visibility as false.
8. Set the text of button as “Done”.
9. Write the following code for the button click event:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Application::Exit();
}

10. Now, add a value cahnged event for the date time picker


private: System::Void dateTimePicker1_ValueChanged(System::Object^ sender, System::EventArgs^ e)
{
label2->Visible =true;
label3->Visible = true;
label3->Text = String::Format("new date : {0}",dateTimePicker1->Text);
}
11. Now Build the solution and run it without debugging.
12. The new Windows Form is displayed in the desired format.


Complete Program:


#pragma once

namespace FirstWindowsFormsApplication {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
///
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
///

public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
///
/// Clean up any resources being used.
///

~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
protected:
private: System::Windows::Forms::DateTimePicker^ dateTimePicker1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private:
///
/// Required designer variable.
///

System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->dateTimePicker1 = (gcnew System::Windows::Forms::DateTimePicker());
this->button1 = (gcnew System::Windows::Forms::Button());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast(0)));
this->label1->Location = System::Drawing::Point(1, 71);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(91, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"Choose a Date";
//
// dateTimePicker1
//
this->dateTimePicker1->Location = System::Drawing::Point(109, 67);
this->dateTimePicker1->Name = L"dateTimePicker1";
this->dateTimePicker1->Size = System::Drawing::Size(200, 20);
this->dateTimePicker1->TabIndex = 1;
this->dateTimePicker1->ValueChanged += gcnew System::EventHandler(this, &Form1::dateTimePicker1_ValueChanged);
//
// button1
//
this->button1->BackColor = System::Drawing::SystemColors::GradientInactiveCaption;
this->button1->Location = System::Drawing::Point(109, 165);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"Done";
this->button1->UseVisualStyleBackColor = false;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(12, 118);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(97, 13);
this->label2->TabIndex = 3;
this->label2->Text = L"You have choosen";
this->label2->Visible = false;
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(115, 118);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(35, 13);
this->label3->TabIndex = 4;
this->label3->Text = L"label3";
this->label3->Visible = false;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::Info;
this->ClientSize = System::Drawing::Size(318, 273);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->button1);
this->Controls->Add(this->dateTimePicker1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Date Time chooser";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Application::Exit();
}


private: System::Void dateTimePicker1_ValueChanged(System::Object^ sender, System::EventArgs^ e)
{
label2->Visible =true;
label3->Visible = true;
label3->Text = String::Format("new date : {0}",dateTimePicker1->Text);
}
};
}

Most of the code here is sytem generated.The code in italics is the only code written by me. :)

Declaration:
All credits to msdn.

mail to : archita.dash@rediffmail.com

No comments: