Introduction
Designing apps in Microsoft Visual C++ 2005 Express Edition is pretty straight forward. Point this, click that, presto! place it on form. But seriously, click on the desired component (control), located at the left toolbar of Visual C++ 2005 Express Edition, and draw it at an appropriate location on the form.
Components on the form align to rectangular grids, giving your apps a symmetric look. This simple app demonstrates the ease of creating database applications and interacting with events in Windows Forms syntaxes, all in C++.
DataGridView
Fast forwarding a bit, we've created a Windows Form, placed a DataGridView
on the form, plus some buttons and textboxes. Also we have connected an .mdb database file to DataGridView
while in design mode.
To have the button respond to mouse clicks, we have to put some code into it. So we double-click on it and we are presented with an event method. All we have to do is place some code in that control's event method.
Let's click on a DataGridView
and place relevant code in its dataGridView1_CellContentClick( )
section, to have it handle some events.
The dataGridView1_CellContentClick Event
The dataGridView1_CellContentClick
event is called upon when the left button is pressed down while above a dataGridView
cell.
private: System::Void dataGridView1_CellContentClick
(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e)
{
textBox1->Text=dataGridView1->Rows[dataGridView1->
CurrentCell->RowIndex]->Cells[0]->Value->ToString();
textBox2->Text=dataGridView1->Rows[dataGridView1->
CurrentCell->RowIndex]->Cells[1]->Value->ToString();
textBox3->Text=dataGridView1->Rows[dataGridView1->
CurrentCell->RowIndex]->Cells[2]->Value->ToString();
textBox4->Text=dataGridView1->Rows[dataGridView1->
CurrentCell->RowIndex]->Cells[3]->Value->ToString();
textBox5->Text=dataGridView1->Rows[dataGridView1->
CurrentCell->RowIndex]->Cells[4]->Value->ToString();
country_=textBox1->Text;
currentPopulation=( System::Convert::ToDouble(textBox2->Text));
growthRatePercent=( System::Convert::ToDouble(textBox3->Text))/100;
birthRate= ((System::Convert::ToDouble( textBox4->Text))/100)/1000;
deathRate= ((System::Convert::ToDouble( textBox5->Text))/100)/1000;
displayResults();
}
The frmMain_Load() Method
When the Windows Form is loading, the frmMain_Load()
method is called upon, in it are instructions to fill dataGridView1
's cell with data from the database itself. The SQL command is:
SELECT country, Population, growthrate, birthrate, deathrate FROM data
and the connectionString is:
provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"dataDbl.mdb\"
private: System::Void frmMain_Load(System::Object^ sender, System::EventArgs^ e) {
this->dataTableAdapter->Fill(this->dataDblDataSet->data);
displayResults();
}
The calculatePopulation() Method
The calculatePopulation()
method is called upon to calculate the new population from the current one.
void calculatePopulation()
{
rateofNaturalIncrease=birthRate-deathRate;
newPopulation = currentPopulation + (rateofNaturalIncrease*currentPopulation );
populationDoubling=Math::Log(2)/rateofNaturalIncrease;
}
The displayResults() Method
The displayResults()
method is called upon to update the textboxes.
void displayResults()
{
calculatePopulation();
txtPopDoubling->Text=Math::Abs(populationDoubling) +"" ;
txtNewPop->Text= newPopulation +"" ;
txtPopGrowth->Text= Math::Abs(rateofNaturalIncrease*currentPopulation) +"" ;
txtOldPopulation->Text=
currentPopulation-(rateofNaturalIncrease*currentPopulation) +"" ;
if ((birthRate*currentPopulation)-(deathRate*currentPopulation)>0)
txtDiff->Text= (birthRate*currentPopulation)-(deathRate*currentPopulation) +"" ;
else txtDiff->Text= (deathRate*currentPopulation)-
(birthRate*currentPopulation) +"" ;
txtDeaths->Text= deathRate*currentPopulation +"";
txtBirths->Text= birthRate*currentPopulation +"";
txtRateofNaturalIncrease->Text= rateofNaturalIncrease +"" ;
txtDeathRate->Text= deathRate +" ( "+deathRate*1000 +" per 1000)";
txtBirthRate->Text= birthRate +" ( "+birthRate*1000 +" per 1000)";
txtcurPopulation->Text= currentPopulation +"";
txtCountry->Text= country_ +"";
txtGrowthRatePercent->Text=growthRatePercent+"";
txtPopulation->Text= currentPopulation+ "";
}
And that is how easy it is to create a database application and interacting data using Windows Forms syntaxes in C++.
Thanks for reading.
History
- 2010-05-08 Updates made
- 2006-11-21 Code complete