• 1
  • 2
  • 3
  • 4

首页 / 行业

怎样在MicrosoftVisualStudio中创建一个简单的电话簿应用程序

2019-08-08 10:24:00

步骤1:在Microsoft Visual Studio中创建新项目

怎样在MicrosoftVisualStudio中创建一个简单的电话簿应用程序

启动Microsoft Visual Studio,并创建新项目,选择 Windows窗体应用程序记住项目类型为 Visual C#。您可以根据需要为项目命名,如果有必要,可以更改项目的位置。

步骤2:将所有内容添加到表单

现在,它是一个空表格。让我们在工具箱中添加一些组件,如图所示。它们是:DataGridView,SaveFileDioalog,OpenFileDialog和menuStrip

第3步:添加列

添加DataGridView之后,我们有空的空间,在其上单击鼠标右键,然后选择编辑列。

步骤4:编写代码之前

确保表单看起来像这样,DataGridView名为“GRID”。您可以在适当的窗口中设置它

步骤5:编写代码

在菜单的每个元素上单击两次,以创建事件,每次显示带代码的窗口时,请执行返回并使用所有(保存,打开,关闭)

我们需要的代码:

private void SaveToolStripMenuItem_Click(object sender,EventArgs e)

{

}

private void OpenToolStripMenuItem_Click (object sender,EventArgs e)

{

}

private void CloseToolStripMenuItem_Click(object sender,EventArgs e)

{

}

步骤6:代码代码。..。

以下是我们的应用程序的完整代码,其中包含“//”情况后的注释:

using System;

using System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.IO; //添加

使用System.Windows.Forms;

使用System.Runtime.Serialization.Formatters.Binary; //添加

使用System。 Runtime.Serialization; //添加

命名空间testowa //这是我的项目名称

{

公共部分类Form1:表单

public Form1()

{

InitializeComponent();

}

[Serializable] //它允许我们的类保存在文件中

公共类数据//我们的数据类

{

公共字符串名称;

公共字符串姓氏;

公共字符串城市;

公共字符串编号;

}

private void SaveToolStripMenuItem_Click(object sender,EventArgs e)

{

GRID.EndEdit();

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //创建文件保存对话框

saveFileDialog1.RestoreDirectory = true;

//读取并过滤原始数据

if(saveFileDialog1.ShowDialog( )== DialogResult.OK)

{

BinaryFormatter formatter = new BinaryFormatter();

FileStream output = new FileStream(saveFileDialog1.FileName,FileMode.OpenOrCreate,FileAccess.Write);

int n = GRID.RowCount;

data [] Person =新数据[n - 1]; //我们拥有尽可能多的行记录,自动添加行,所以我们总共有一行比我们需要的多,所以n是行数-1空行

for(int i = 0; i Person [i] = new data();

//GRID在“[]”中有两个数字第一个数字是一个列的索引,第二个是行’的idnex,索引总是从0开始

Person [i] .name = GRID [0,i] .Value.ToString();

Person [i] .surname = GRID [1,i] .Value.ToString();

Person [i] .city = GRID [2,i] .Value.ToString();

Person [i]。 number = GRID [3,i] .Value.ToString();

}

formatter.Serialize(output,Person);

output.Close();

}

}

private void OpenToolStripMenuItem_Click(object sender,EventArgs e)//读取文件并将数据添加到GRID

{

openFileDialog1 = new OpenFileDialog();

if(openFileDialog1.ShowDialog ()== DialogResult.OK)

{

BinaryFormatter reader = new BinaryFormatter();

FileStream input = new FileStream(openFileDialog1。 FileName,FileMode.Open,FileAccess.Read);

data [] Person =(data [])reader.Deserialize(input);

GRID.Rows.Clear();

for(int i = 0; i {

GRID.Rows.Add();

GRID [0,i] .Value = Person [i]。 name;

GRID [1,i] .Value = Person [i] .surname;

GRID [2,i] .Value = Person [i] .city;

GRID [3,i]。 Value = Person [i] .number;

}

private void CloseToolStripMenuItem_Click(object sender,EventArgs e)

{

Close(); //关闭应用

第7步:完成了。测试它

单击Visual Studio中的菜单Debug比启动调试它应该工作。尝试测试应用程序。我相信你会发现一些错误,我们的应用程序非常简单,没有任何证据,同时保存,打开文件,它只是演示如何做一个更大的有用的应用程序。 你可以改进它,制作你自己的版本! 添加一些新功能,随意更改。祝你好运!

应用程序步骤项目建新

  • 1
  • 2
  • 3
  • 4

最新内容

手机

相关内容

  • 1
  • 2
  • 3

猜你喜欢