/*-------------------------------------------------------------------------- I N T R O D U C T I ON T O D A T A E N T R Y P A R T 4 C++ Version Ron Kessler Created 6/10/2013 -------------------------------------------------------------------------- Updated 9/29/2014 Shows how to add listbox items in form_load NOTES: This program shows you how to fill a listbox with items and when they select one, it puts that item into a textbox. The listbox items are filled using the items properties at design time. Also note how I set the Tab Order and right-aligned the textboxes and labels. Also, I set the AcceptButton and CancelButton properties of the form so the OK button is the default and the ESC key works like clicking the end button. Pressing ENTER is like clicking OK button. */ #pragma once namespace DataEntryPart4 { 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 /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); txtPrice->Text="1.00"; //so it isn't blank this->CenterToScreen(); } protected: /// /// Clean up any resources being used. /// ~Form1() { if (components) { delete components; } } internal: System::Windows::Forms::Label^ lblBalDue; protected: internal: System::Windows::Forms::Label^ lblTax; internal: System::Windows::Forms::PictureBox^ PictureBox1; internal: System::Windows::Forms::Label^ Label6; internal: System::Windows::Forms::Label^ Label5; internal: System::Windows::Forms::TextBox^ txtPrice; internal: System::Windows::Forms::Label^ Label4; internal: System::Windows::Forms::TextBox^ txtItem; internal: System::Windows::Forms::Label^ Label3; internal: System::Windows::Forms::TextBox^ txtLastName; internal: System::Windows::Forms::Label^ Label2; internal: System::Windows::Forms::TextBox^ txtFirstName; internal: System::Windows::Forms::Label^ Label1; internal: System::Windows::Forms::ListBox^ lstProducts; internal: System::Windows::Forms::Button^ btnEnd; internal: System::Windows::Forms::Button^ btnClear; internal: System::Windows::Forms::Button^ btnOK; 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) { System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid)); this->lblBalDue = (gcnew System::Windows::Forms::Label()); this->lblTax = (gcnew System::Windows::Forms::Label()); this->PictureBox1 = (gcnew System::Windows::Forms::PictureBox()); this->Label6 = (gcnew System::Windows::Forms::Label()); this->Label5 = (gcnew System::Windows::Forms::Label()); this->txtPrice = (gcnew System::Windows::Forms::TextBox()); this->Label4 = (gcnew System::Windows::Forms::Label()); this->txtItem = (gcnew System::Windows::Forms::TextBox()); this->Label3 = (gcnew System::Windows::Forms::Label()); this->txtLastName = (gcnew System::Windows::Forms::TextBox()); this->Label2 = (gcnew System::Windows::Forms::Label()); this->txtFirstName = (gcnew System::Windows::Forms::TextBox()); this->Label1 = (gcnew System::Windows::Forms::Label()); this->lstProducts = (gcnew System::Windows::Forms::ListBox()); this->btnEnd = (gcnew System::Windows::Forms::Button()); this->btnClear = (gcnew System::Windows::Forms::Button()); this->btnOK = (gcnew System::Windows::Forms::Button()); (cli::safe_cast(this->PictureBox1))->BeginInit(); this->SuspendLayout(); // // lblBalDue // this->lblBalDue->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D; this->lblBalDue->Location = System::Drawing::Point(246, 317); this->lblBalDue->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->lblBalDue->Name = L"lblBalDue"; this->lblBalDue->Size = System::Drawing::Size(100, 25); this->lblBalDue->TabIndex = 52; this->lblBalDue->TextAlign = System::Drawing::ContentAlignment::MiddleRight; // // lblTax // this->lblTax->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D; this->lblTax->Location = System::Drawing::Point(246, 283); this->lblTax->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->lblTax->Name = L"lblTax"; this->lblTax->Size = System::Drawing::Size(100, 25); this->lblTax->TabIndex = 51; this->lblTax->TextAlign = System::Drawing::ContentAlignment::MiddleRight; // // PictureBox1 // this->PictureBox1->Image = (cli::safe_cast(resources->GetObject(L"PictureBox1.Image"))); this->PictureBox1->Location = System::Drawing::Point(80, 2); this->PictureBox1->Margin = System::Windows::Forms::Padding(4); this->PictureBox1->Name = L"PictureBox1"; this->PictureBox1->Size = System::Drawing::Size(591, 90); this->PictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->PictureBox1->TabIndex = 50; this->PictureBox1->TabStop = false; // // Label6 // this->Label6->AutoSize = true; this->Label6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->Label6->ForeColor = System::Drawing::Color::Navy; this->Label6->Location = System::Drawing::Point(42, 315); this->Label6->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->Label6->Name = L"Label6"; this->Label6->Size = System::Drawing::Size(106, 20); this->Label6->TabIndex = 49; this->Label6->Text = L"Balance Due"; // // Label5 // this->Label5->AutoSize = true; this->Label5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->Label5->ForeColor = System::Drawing::Color::Navy; this->Label5->Location = System::Drawing::Point(42, 282); this->Label5->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->Label5->Name = L"Label5"; this->Label5->Size = System::Drawing::Size(36, 20); this->Label5->TabIndex = 48; this->Label5->Text = L"Tax"; // // txtPrice // this->txtPrice->Location = System::Drawing::Point(246, 254); this->txtPrice->Margin = System::Windows::Forms::Padding(4); this->txtPrice->Name = L"txtPrice"; this->txtPrice->Size = System::Drawing::Size(99, 22); this->txtPrice->TabIndex = 4; this->txtPrice->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // Label4 // this->Label4->AutoSize = true; this->Label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->Label4->ForeColor = System::Drawing::Color::Navy; this->Label4->Location = System::Drawing::Point(42, 255); this->Label4->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->Label4->Name = L"Label4"; this->Label4->Size = System::Drawing::Size(48, 20); this->Label4->TabIndex = 47; this->Label4->Text = L"Price"; // // txtItem // this->txtItem->Location = System::Drawing::Point(156, 219); this->txtItem->Margin = System::Windows::Forms::Padding(4); this->txtItem->Name = L"txtItem"; this->txtItem->Size = System::Drawing::Size(188, 22); this->txtItem->TabIndex = 3; this->txtItem->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // Label3 // this->Label3->AutoSize = true; this->Label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->Label3->ForeColor = System::Drawing::Color::Navy; this->Label3->Location = System::Drawing::Point(42, 218); this->Label3->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->Label3->Name = L"Label3"; this->Label3->Size = System::Drawing::Size(46, 20); this->Label3->TabIndex = 46; this->Label3->Text = L"Item:"; // // txtLastName // this->txtLastName->Location = System::Drawing::Point(156, 187); this->txtLastName->Margin = System::Windows::Forms::Padding(4); this->txtLastName->Name = L"txtLastName"; this->txtLastName->Size = System::Drawing::Size(188, 22); this->txtLastName->TabIndex = 1; this->txtLastName->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // Label2 // this->Label2->AutoSize = true; this->Label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->Label2->ForeColor = System::Drawing::Color::Navy; this->Label2->Location = System::Drawing::Point(42, 187); this->Label2->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->Label2->Name = L"Label2"; this->Label2->Size = System::Drawing::Size(96, 20); this->Label2->TabIndex = 43; this->Label2->Text = L"Last Name:"; // // txtFirstName // this->txtFirstName->Location = System::Drawing::Point(156, 153); this->txtFirstName->Margin = System::Windows::Forms::Padding(4); this->txtFirstName->Name = L"txtFirstName"; this->txtFirstName->Size = System::Drawing::Size(188, 22); this->txtFirstName->TabIndex = 0; this->txtFirstName->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // Label1 // this->Label1->AutoSize = true; this->Label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(0))); this->Label1->ForeColor = System::Drawing::Color::Navy; this->Label1->Location = System::Drawing::Point(42, 153); this->Label1->Margin = System::Windows::Forms::Padding(4, 0, 4, 0); this->Label1->Name = L"Label1"; this->Label1->Size = System::Drawing::Size(97, 20); this->Label1->TabIndex = 36; this->Label1->Text = L"First Name:"; // // lstProducts // this->lstProducts->FormattingEnabled = true; this->lstProducts->ItemHeight = 16; this->lstProducts->Location = System::Drawing::Point(406, 153); this->lstProducts->Margin = System::Windows::Forms::Padding(4); this->lstProducts->Name = L"lstProducts"; this->lstProducts->Size = System::Drawing::Size(159, 148); this->lstProducts->Sorted = true; this->lstProducts->TabIndex = 2; this->lstProducts->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::lstProducts_SelectedIndexChanged); // // btnEnd // this->btnEnd->DialogResult = System::Windows::Forms::DialogResult::Cancel; this->btnEnd->Location = System::Drawing::Point(587, 275); this->btnEnd->Margin = System::Windows::Forms::Padding(4); this->btnEnd->Name = L"btnEnd"; this->btnEnd->Size = System::Drawing::Size(100, 28); this->btnEnd->TabIndex = 7; this->btnEnd->Text = L"&End"; this->btnEnd->UseVisualStyleBackColor = true; this->btnEnd->Click += gcnew System::EventHandler(this, &Form1::btnEnd_Click); // // btnClear // this->btnClear->Location = System::Drawing::Point(587, 211); this->btnClear->Margin = System::Windows::Forms::Padding(4); this->btnClear->Name = L"btnClear"; this->btnClear->Size = System::Drawing::Size(100, 28); this->btnClear->TabIndex = 6; this->btnClear->Text = L"&Clear"; this->btnClear->UseVisualStyleBackColor = true; this->btnClear->Click += gcnew System::EventHandler(this, &Form1::btnClear_Click); // // btnOK // this->btnOK->Location = System::Drawing::Point(587, 153); this->btnOK->Margin = System::Windows::Forms::Padding(4); this->btnOK->Name = L"btnOK"; this->btnOK->Size = System::Drawing::Size(100, 28); this->btnOK->TabIndex = 5; this->btnOK->Text = L"&OK"; this->btnOK->UseVisualStyleBackColor = true; this->btnOK->Click += gcnew System::EventHandler(this, &Form1::btnOK_Click); // // Form1 // this->AcceptButton = this->btnOK; this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->CancelButton = this->btnEnd; this->ClientSize = System::Drawing::Size(729, 361); this->Controls->Add(this->lblBalDue); this->Controls->Add(this->lblTax); this->Controls->Add(this->PictureBox1); this->Controls->Add(this->Label6); this->Controls->Add(this->Label5); this->Controls->Add(this->txtPrice); this->Controls->Add(this->Label4); this->Controls->Add(this->txtItem); this->Controls->Add(this->Label3); this->Controls->Add(this->txtLastName); this->Controls->Add(this->Label2); this->Controls->Add(this->txtFirstName); this->Controls->Add(this->Label1); this->Controls->Add(this->lstProducts); this->Controls->Add(this->btnEnd); this->Controls->Add(this->btnClear); this->Controls->Add(this->btnOK); this->Name = L"Form1"; this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen; this->Text = L"The Parts Bin Auto Parts (C++ version)"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); (cli::safe_cast(this->PictureBox1))->EndInit(); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion //****************START OF MY CODE******************* private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //---load items before form is shown on screen lstProducts->Items->Add("Air shocks"); lstProducts->Items->Add("Coolant"); lstProducts->Items->Add("Off-road Lights"); lstProducts->Items->Add("Oil Filter"); lstProducts->Items->Add("Performance Radiator"); lstProducts->Items->Add("Safety Harness"); lstProducts->Items->Add("Spark Plugs"); lstProducts->Items->Add("Thermostat"); lstProducts->Items->Add("Transmission cooler"); lstProducts->Items->Add("Valve Covers"); lstProducts->Items->Add("Water Pump"); //---sort them lstProducts->Sorted=true; } private: System::Void lstProducts_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { /*---when they click on an item, this code runs(executes) and takes the item chosen and puts it into the text property of the txtItem textbox. */ // txtItem->Text = Convert::ToString(lstProducts->SelectedItem); //or.... //txtItem->Text = (String^) lstProducts->SelectedItem; // or txtItem->Text = lstProducts->Text; } private: System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e) { double price = Convert::ToDouble(txtPrice->Text); double taxRate = .08; double salesTax = price * taxRate; double balanceDue = price + salesTax; //---now format as money without the $ showing. "n2" = number with 2 decimal places txtPrice->Text = price.ToString("n2"); lblTax->Text = salesTax.ToString("n2"); lblBalDue->Text = balanceDue.ToString("c2"); //with a $ sign } private: System::Void btnClear_Click(System::Object^ sender, System::EventArgs^ e) { //---clear everything and set cursor to name textbox txtFirstName->Clear(); txtLastName->Clear(); txtItem->Clear(); txtPrice->Text = "1.00"; //so the program doesn't blow if it is empty lblTax->Text = ""; lblBalDue->Text = ""; txtFirstName->Focus(); } private: System::Void btnEnd_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); } }; }