65.9K
CodeProject 正在变化。 阅读更多。
Home

如何创建简单的 Windows 窗体 C++ 数据库应用程序

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.64/5 (8投票s)

2010年5月10日

CPOL

2分钟阅读

viewsIcon

85703

downloadIcon

10858

如何创建简单的 Windows 窗体 C++ 数据库应用程序。

引言

在 Microsoft Visual C++ 2005 Express Edition 中设计应用程序非常简单直接。指这里,点那里,搞定!把它们放在窗体上。但说正经的,点击左侧工具栏中所需的组件(控件),然后在窗体上的适当位置绘制它。

窗体上的组件对齐到矩形网格,使您的应用程序看起来对称。这个简单的应用程序演示了创建数据库应用程序和在 Windows Forms 语法中与事件交互的简易性,全部使用 C++。

DataGridView

快进一点,我们创建了一个 Windows 窗体,在窗体上放置了一个 DataGridView,以及一些按钮和文本框。同时,我们在设计模式下将一个 .mdb 数据库文件连接到了 DataGridView

要使按钮响应鼠标点击,我们需要在其中放入一些代码。所以我们双击它,然后会呈现一个事件方法。我们所要做的就是在该控件的事件方法中放置一些代码。

让我们点击一个 DataGridView,并在其 dataGridView1_CellContentClick( ) 部分放置相关代码,以便处理一些事件。

dataGridView1_CellContentClick 事件

当鼠标左键按下并位于 dataGridView 单元格上方时,会调用 dataGridView1_CellContentClick 事件。

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();
} 

frmMain_Load() 方法

当 Windows 窗体加载时,会调用 frmMain_Load() 方法,其中包含用数据库中的数据填充 dataGridView1 单元格的指令。SQL 命令是

SELECT country, Population, growthrate, birthrate, deathrate FROM data 

连接字符串是

provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"dataDbl.mdb\"
private: System::Void frmMain_Load(System::Object^  sender, System::EventArgs^  e) {
	// TODO: This line of code loads data into the 'dataDblDataSet.data' table. 
         // You can move, or remove it, as needed.
	this->dataTableAdapter->Fill(this->dataDblDataSet->data);
	displayResults();
}  

calculatePopulation() 方法

调用 calculatePopulation() 方法来根据当前人口计算新人口。

void calculatePopulation()
{
   rateofNaturalIncrease=birthRate-deathRate;
   newPopulation = currentPopulation + (rateofNaturalIncrease*currentPopulation );
   populationDoubling=Math::Log(2)/rateofNaturalIncrease;
} 

displayResults() 方法

调用 displayResults() 方法来更新文本框。

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+ "";
} 

这就是使用 C++ 中的 Windows Forms 语法创建数据库应用程序和交互数据的简易程度。

感谢阅读。

历史

  • 2010-05-08 更新完成
  • 2006-11-21 代码完成
© . All rights reserved.